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

Recommended Answers

All 6 Replies

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.

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?=)

Did'nt quite understand your question...

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.

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.

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;
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.