By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,918 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,918 IT Pros & Developers. It's quick & easy.

Enable/Diasble button w/ 2 conditions.

P: 8
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
Share this Question
Share on Google+
4 Replies


100+
P: 179
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

P: 8
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

P: 8
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
Expert 2.5K+
P: 3,525
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.