470,594 Members | 1,531 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Enable/Diasble button w/ 2 conditions.

Hi currently with 1 combobox,textbox and a OK button which the button will enabled once my textbox is not empty.
Expand|Select|Wrap|Line Numbers
  1. private void locacodeBox_TextChanged(object sender, EventArgs e)
  2.         {
  3.             TextBox textBox = sender as TextBox;
  4.             if (locacodeBox.Text.Length == 0)
  5.                 locacodeBox.Tag = false;
  6.             else
  7.                 locacodeBox.Tag = true;
  8.             ValidateButton();
  9.         }
  10.  
  11.         private void ValidateButton()
  12.         {
  13.             this.newlocaBtn.Enabled = (bool)locacodeBox.Tag;
  14.         }
But now i want to add the condition where both the combobox and textbox is also not empty then the button will enable...how can i do so?
Feb 13 '09 #1
4 3095
IanWright
179 100+
A few points:

1) TextBox textBox = sender as TextBox;

You have cast the sender to a TextBox yet you never use it.

2) That looks like a really bad way to use a Tag. Why not have something more like this:

2)

Expand|Select|Wrap|Line Numbers
  1. private void locacodeBox_TextChanged(object sender, EventArgs e)
  2. {
  3.    if((String.IsNullOrEmpty(this.textbox.Text) && String.IsNullOrEmpty(this.locacodeBox.Text) == false)
  4.    {
  5.        this.newlocaBtn.Enabled = true;
  6.    }
  7.    else
  8.    {
  9.       this.newlocaBtn.Enabled = false;
  10.    }
  11. }
  12.  
or

Expand|Select|Wrap|Line Numbers
  1. private void locacodeBox_TextChanged(object sender, EventArgs e)
  2. {
  3.        // Notice the ! to invert the result. If both are not empty...
  4.        this.newlocaBtn.Enabled = !(String.IsNullOrEmpty(this.textbox.Text) && String.IsNullOrEmpty(this.locacodeBox.Text);
  5. }
  6.  
Feb 13 '09 #2
Thanks IanWright for your suggestions.

But currently the button enable is still not based on when the comboBox and twxtBox is not empty.
Feb 16 '09 #3
I've accomplish my goal in this code but I'm sure there is a shorter and easier way to the solution.

Expand|Select|Wrap|Line Numbers
  1.         public Boolean validatetext;
  2.         public Boolean validacombobox;
  3.  
  4.  
  5. private void locacodeBox_TextChanged(object sender, EventArgs e) 
  6. {
  7.     if (textBox1.Text.Length == 0)
  8.         validatetext = false;
  9.     else
  10.         validatetext = true;
  11.     ValidateButton(); 
  12. }
  13.  
  14. private void comboBox1_TextChanged(object sender, EventArgs e)
  15. {
  16.     if (comboBox1.Text.Length == 0)
  17.         validacombobox = false;
  18.     else
  19.         validacombobox = true;
  20.     ValidateButton();
  21. }
  22.  
  23. private void ValidateButton()
  24. {
  25.     if (validacombobox == true && validatetext == true)
  26.         this.button2.Enabled = true; 
  27.     else
  28.         this.button2.Enabled = false; 
  29. }
Feb 16 '09 #4
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. button2.Enabled = comboBo1.SelectedItem > -1 && string.IsNullOrEmpty(TextBox1.Text);
Feb 16 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Gary Jefferson | last post: by
3 posts views Thread by =?Utf-8?B?UGxhdGVyaW90?= | last post: by
17 posts views Thread by govolsbaby | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.