user1108948
user1108948

Reputation:

Validate a textbox in a windows form

Windows form application(.Net 3.5) I have a textbox and a button on the form. I want to disable the button once the textbox is empty. I don't want to use this method. Because the button is still enabled.

Thanks.

Upvotes: 0

Views: 4571

Answers (4)

Rui Corveira
Rui Corveira

Reputation: 1

Make button1.Enabled = false;

and add EventHandler to textbox1.TextChanged = new System.EventHandler(SearchBoxTextChanged);

private void textbox1_TextChanged(object sender, EventArgs e)
    {
        button1.Enabled = (textBox1.Text.Trim() != string.Empty);
    }

Upvotes: 0

Ignacio Gómez
Ignacio Gómez

Reputation: 1637

if you want to disable the textbox, then using textChanged:

if (textbox.Text == ""){
   button.Enabled = false;
}

Hope it helps

Upvotes: 0

Micah Armantrout
Micah Armantrout

Reputation: 7001

Handle the on text change event

check and see what textbox.text is like this

  if(string.IsNullOrEmpty(textbox1.text))
 {
  Button1.enabled = false;
 }

Upvotes: 0

George Johnston
George Johnston

Reputation: 32278

In the event handler for TextChanged, simply determine if the text box contains any data. If it does, enable it. Otherwise, disable it. Add your event handler and then implement something like the following,

private void textBox1_TextChanged(object sender, EventArgs e)
{
   button1.Enabled = !(textBox1.Text == String.Empty);
}

Upvotes: 3

Related Questions