954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Enabled of Button when data is change/ insert in runtime

I basically new to C# and i need some help=)

I have 3 combo box, 5 textbox and 3 buttons.

One of the button[BTNAPPLY] is disabled and when we type in detail in textbox or change the selection in the combo boxx in runtime, it will be enabled.

How do i go about doing coding for that.
sorry if u don't understand what i'm trying to say. please feel free to ask. thank you

facadie
Junior Poster in Training
57 posts since Sep 2009
Reputation Points: 1
Solved Threads: 0
 

Just use the following statement:

BTNAPPLY.Enabled = true;

inside the TextChange Event handler of the text box and the SelectedIndexChanged event of the combo box.

for the TextChange Event handler:

if (textBox.Text == "your required text")
      BTNAPPLY.Enabled = true;


for the SelectedIndexChanged Event Handler

if (cmbBox.SelectedItem == "your required seleciton")
        BTNAPPLY.Enabled = true;


Assuming that the button is disabled when the application starts.

jatin24
Junior Poster in Training
75 posts since Aug 2009
Reputation Points: 31
Solved Threads: 21
 

Hi there=) thanks for info. i manage to make it when u start typing then it'll become enabled. but what if there is alot of text box to put the code is there other simplifications so that we do not have to go to every even handler to put the code?=)

facadie
Junior Poster in Training
57 posts since Sep 2009
Reputation Points: 1
Solved Threads: 0
 

Did'nt quite understand your question...

jatin24
Junior Poster in Training
75 posts since Aug 2009
Reputation Points: 31
Solved Threads: 21
 

Okiex=)

Example. I have 10 Text Box and i want the BTNApply to be enabled when any one of the textbox is entered.

and if i used the code that you just give me. I have to go to each text change event handler to insert the code.. Is there a simplified way to insert all 10 at once or we have to do it in a way by inserting the code one by one.

facadie
Junior Poster in Training
57 posts since Sep 2009
Reputation Points: 1
Solved Threads: 0
 

Well you can put the code i gave you in a function, and call that function in each textBox TextChanged Event.

You can use textBox Leave Event as well if you like, if you dont want the function to be called after each key stroke.

jatin24
Junior Poster in Training
75 posts since Aug 2009
Reputation Points: 31
Solved Threads: 21
 

You can do it in code with all the text boxes. In this case I have textBox1 thru textBox10:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.textboxes
{
  public partial class Form1 : Form
  {
    private TextBox[] _textBoxCollection;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      //Find our 10 boxes for future reference
      List<TextBox> lst = new List<TextBox>();
      for (int i1 = 1; i1 <= 10; i1++)
      {
        
        Control[] ctrls = this.Controls.Find("textBox" + i1.ToString("F0"), true);
        if ((ctrls.Length > 0) && (ctrls[0] is TextBox))
          lst.Add(ctrls[0] as TextBox);
      }
      _textBoxCollection = lst.ToArray();

      WireUpEvents(); //wire up our text changed events
      DoTextChanged(); //set the initial button status
    }

    
    private void WireUpEvents()
    {
      foreach (TextBox tb in _textBoxCollection)
        tb.TextChanged += new EventHandler(textBox_TextChanged);
    }
    private void UnwireEvents()
    {
      foreach (TextBox tb in _textBoxCollection)
        tb.TextChanged -= new EventHandler(textBox_TextChanged);
    }

    private void DoTextChanged()
    {
      bool enabled = false;
      foreach (TextBox tb in _textBoxCollection)
      {
        if (!string.IsNullOrEmpty(tb.Text))
        {
          enabled = true;
          break;
        }
      }
      btnApply.Enabled = enabled;
    }

    void textBox_TextChanged(object sender, EventArgs e)
    {
      if (!this.Visible)
        return;
      DoTextChanged();
    }

    /// <summary>
    /// Dont want to leak memory!
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
      UnwireEvents();
      _textBoxCollection = null;
    }
  }
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: