13 Replies - 5703 Views - Last Post: 30 January 2013 - 09:27 AM Rate Topic: *---- 1 Votes

#1 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Disable button until all textbox is not empty.

Posted 29 January 2013 - 05:58 PM

Hi, I tried to Disable a button while all the textfield is null or empty, but it only work with one text box. If I type something into any text box, the button enable. I want the button to be disable until all textbox are fill with something. Thank you.

Here is my original code from Visual Studio.

        private void Textbox_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(((TextBox)sender).Text))
                okButton.Enabled = false;
            else
                okButton.Enabled = true;
        }



I also tried to

        private void Textbox_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textbox1.text) && string.IsNullOrEmpty(textbox2.text) )// I added && to all the text box, but it still gave me the same result.
                okButton.Enabled = false;
            else
                okButton.Enabled = true;
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Disable button until all textbox is not empty.

#2 lukasz_ltt  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 29
  • Joined: 12-October 12

Re: Disable button until all textbox is not empty.

Posted 29 January 2013 - 06:01 PM

Your second example is almost good, try replace your third line with code:

if (string.IsNullOrEmpty(textbox1.text) || string.IsNullOrEmpty(textbox2.text) )


This post has been edited by lukasz_ltt: 29 January 2013 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: Disable button until all textbox is not empty.

Posted 29 January 2013 - 06:26 PM

View Postlukasz_ltt, on 29 January 2013 - 06:01 PM, said:

Your second example is almost good, try replace your third line with code:

if (string.IsNullOrEmpty(textbox1.text) || string.IsNullOrEmpty(textbox2.text) )



I though that the OR statement??? but it work. Weird

This post has been edited by DaiToBu: 29 January 2013 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 lukasz_ltt  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 29
  • Joined: 12-October 12

Re: Disable button until all textbox is not empty.

Posted 29 January 2013 - 06:29 PM

Exactly, if I good understand you (my english is poor), you want disable button when textbox1 is empty or textbox2 is empty, so when both are filled, then button will ennable.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3465
  • View blog
  • Posts: 11,753
  • Joined: 12-December 12

Re: Disable button until all textbox is not empty.

Posted 29 January 2013 - 06:31 PM

View PostDaiToBu, on 29 January 2013 - 06:26 PM, said:

View Postlukasz_ltt, on 29 January 2013 - 06:01 PM, said:

Your second example is almost good, try replace your third line with code:

if (string.IsNullOrEmpty(textbox1.text) || string.IsNullOrEmpty(textbox2.text) )



I though that the OR statement??? but it work. Weird

Yes, it is OR and you need this. You are disabling the button if any one of the textboxes is empty. [this one, or that one, or..]
Was This Post Helpful? 0
  • +
  • -

#6 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: Disable button until all textbox is not empty.

Posted 29 January 2013 - 06:33 PM

View Postlukasz_ltt, on 29 January 2013 - 06:29 PM, said:

Exactly, if I good understand you (my english is poor), you want disable button when textbox1 is empty or textbox2 is empty, so when both are filled, then button will ennable.


That weird. From what I understand is when AND(&&) mean when both textbox1 && textbox2 must be fill before it enable. OR(||) mean when NEITHER textbox fill, then it enable. Confusing.

This post has been edited by DaiToBu: 29 January 2013 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 lukasz_ltt  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 29
  • Joined: 12-October 12

Re: Disable button until all textbox is not empty.

Posted 29 January 2013 - 06:48 PM

Your undarstandig is good but you are checking in your if condition empty not filled. In this case you could write:

if (!string.IsNullOrEmpty(textbox1.text) && !string.IsNullOrEmpty(textbox2.text) )
    okButton.Enabled = true;
else
    okButton.Enabled = false;


Was This Post Helpful? 1
  • +
  • -

#8 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: Disable button until all textbox is not empty.

Posted 29 January 2013 - 06:52 PM

View Postlukasz_ltt, on 29 January 2013 - 06:48 PM, said:

Your undarstandig is good but you are checking in your if condition empty not filled. In this case you could write:

if (!string.IsNullOrEmpty(textbox1.text) && !string.IsNullOrEmpty(textbox2.text) )
    okButton.Enabled = true;
else
    okButton.Enabled = false;



I think NullOrEmpty backward or something. Anyway thank you, and it work.

This post has been edited by DaiToBu: 29 January 2013 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Disable button until all textbox is not empty.

Posted 30 January 2013 - 01:49 AM

Wouldn't it be better to loop through all textbox controls and see if they are empty, for example if you had 10 Textboxes you would have to do this
if (!string.IsNullOrEmpty(textbox1.text) && !string.IsNullOrEmpty(textbox2.text) && !string.IsNullOrEmpty(textbox3.text) ... !string.IsNullOrEmpty(textbox10.text) 


That would be a lot of repetition.
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,106
  • Joined: 05-May 12

Re: Disable button until all textbox is not empty.

Posted 30 January 2013 - 07:12 AM

Yes, Michael26 hit the nail on the head. A loop would be better if the form has a lot of fields.

Something like this pseudo-code:
bool isValid = true;
foreach(Control control in Controls)
{
    TextBox textBox = control as TextBox;
    if (textBox != null && String.IsNullOrEmpty(textBox.Text))
    {
        isValid = false;
        break;
    }
}
okButton.Enabled = isValid;


Was This Post Helpful? 0
  • +
  • -

#11 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Disable button until all textbox is not empty.

Posted 30 January 2013 - 07:17 AM

Glad i could help :)
Was This Post Helpful? 0
  • +
  • -

#12 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Disable button until all textbox is not empty.

Posted 30 January 2013 - 08:05 AM

View PostDaiToBu, on 29 January 2013 - 07:52 PM, said:

I think NullOrEmpty backward or something. Anyway thank you, and it work.


No, your understanding of '&&' is just a little off. When you use '&&' in that if statement, the code will only execute if BOTH conditions are true. If one of the conditions is false (in this case, if one of those textboxes ISN'T empty), then the code won't evaluate. What you're saying is "execute this code ONLY IF textbox1.text is empty AND textbox2.text is empty". What you really want to say is "execute this code if either textbox1.text OR textbox2.text is empty". You say that with the code that lukasz_ltt gave you:

Quote

if (string.IsNullOrEmpty(textbox1.text) || string.IsNullOrEmpty(textbox2.text) )


That being said, better ways of accomplishing the task have been presented. I just wanted to explain to you why using '||' actually says what you want the code to say.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: Disable button until all textbox is not empty.

Posted 30 January 2013 - 08:46 AM

You can do some thing like this

okButton.EnableIf(Function(tb) String.IsNullEmpty(tb) False, {tb1,tb2,tb3,tb4})


if you define the following extension method.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#14 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Disable button until all textbox is not empty.

Posted 30 January 2013 - 09:27 AM

I think it would be like this
public void EnableIf<T, U>(T Ctrl, Func<U, bool> fn, bool invert, params U[] OfThese) where T : Control where U : Control
{
	object result = OfThese.Any(fn);
	Ctrl.Enabled = invert ? !result : result;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1