Enable/Diasble button w/ 2 conditions.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • csharp learner
    New Member
    • Feb 2009
    • 8

    #1

    Enable/Diasble button w/ 2 conditions.

    Hi currently with 1 combobox,textbo x and a OK button which the button will enabled once my textbox is not empty.
    Code:
    private void locacodeBox_TextChanged(object sender, EventArgs e)
            {
                TextBox textBox = sender as TextBox;
                if (locacodeBox.Text.Length == 0)
                    locacodeBox.Tag = false;
                else
                    locacodeBox.Tag = true;
                ValidateButton();
            }
    
            private void ValidateButton()
            {
                this.newlocaBtn.Enabled = (bool)locacodeBox.Tag;
            }
    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?
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    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)

    Code:
    private void locacodeBox_TextChanged(object sender, EventArgs e)
    {
       if((String.IsNullOrEmpty(this.textbox.Text) && String.IsNullOrEmpty(this.locacodeBox.Text) == false)
       {
           this.newlocaBtn.Enabled = true;
       }
       else
       {
          this.newlocaBtn.Enabled = false;
       }
    }
    or

    Code:
    private void locacodeBox_TextChanged(object sender, EventArgs e)
    {
           // Notice the ! to invert the result. If both are not empty...
           this.newlocaBtn.Enabled = !(String.IsNullOrEmpty(this.textbox.Text) && String.IsNullOrEmpty(this.locacodeBox.Text);
    }

    Comment

    • csharp learner
      New Member
      • Feb 2009
      • 8

      #3
      Thanks IanWright for your suggestions.

      But currently the button enable is still not based on when the comboBox and twxtBox is not empty.

      Comment

      • csharp learner
        New Member
        • Feb 2009
        • 8

        #4
        I've accomplish my goal in this code but I'm sure there is a shorter and easier way to the solution.

        Code:
                public Boolean validatetext;
                public Boolean validacombobox;
        
        
        private void locacodeBox_TextChanged(object sender, EventArgs e) 
        {
            if (textBox1.Text.Length == 0)
                validatetext = false;
            else
                validatetext = true;
            ValidateButton(); 
        }
        
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text.Length == 0)
                validacombobox = false;
            else
                validacombobox = true;
            ValidateButton();
        }
        
        private void ValidateButton()
        {
            if (validacombobox == true && validatetext == true)
                this.button2.Enabled = true; 
            else
                this.button2.Enabled = false; 
        }

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Code:
          button2.Enabled = comboBo1.SelectedItem > -1 && string.IsNullOrEmpty(TextBox1.Text);

          Comment

          Working...