1

I am developing an application, in which there is a button in search box (like one in itunes). I want to enable cancel button whenever there is text in text box and disable it when text box is empty. I tried with text_changed event on textbox with the following code, but it jump over the if condition. Even sender sends me correct values but i am unable to put it into if else.

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(sender.ToString()))
        {
            btn_cancel.Visible = false;
        }
        else
        {
            btn_cancel.Visible = true;
        }
    }

Please help

7 Answers 7

5

Here is a simple solution.

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

Of course, you'll have to set the button.Enabled = false when the form initially loads since the textbox event won't fire on startup (true for all answers currently provided for your question).

Sign up to request clarification or add additional context in comments.

1 Comment

string.IsNullOrEmpty is another option depending on the .NET Framework version you are targeting. And obviously, either the Enabled or Visible property can be used here.
2
private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(textBox1.Text))
        btn_cancel.Visible = false;
    else
        btn_cancel.Visible = true;
}

Comments

1

Try this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var textbox = sender as TextBox;
    if (string.IsNullOrEmpty(textbox.Text))
    {
        btn_cancel.Visible = false;
    }
    else
    {
        btn_cancel.Visible = true;
    }
}

sender.ToString() will always return System.Windows.Forms.TextBox you need to cast sender as TextBox and use the Text value for your null or empty check

2 Comments

This is unnecessary unless the OP is subscribing more than one textbox to the same event. If he isn't (which I assume), then he knows that sender is textBox1..
I was just showing his mistake about sender.ToString() , I don't really care how he uses it.
0

try casting the sender to TextBox :

if (string.IsNullOrEmpty(((TextBox)sender).Text))

2 Comments

This is unnecessary unless the OP is subscribing more than one textbox to the same event. If he isn't (which I assume), then he knows that sender is textBox1..
@SimonWhitehead I think you missed the point, we're trying to show his mistake, as sa_ddam213 well said...
0

One liner:

btn_cancel.Visible = textBox1.Text.Length > 0;

Comments

0

This is how I'd do it:

private void textBox1_TextChanged(object sender, EventArgs e)
{        
    string text = ((sender as TextBox) == null ? string.Empty : (sender as TextBox).Text);
    this.button1.Enabled = (string.IsNullOrWhiteSpace(text) == false);
}

This doesn't assume the event source is a specific control and avoids an exception if by mistake it's attached to something that's not a TextBox.

Comments

0
private void textBox2_TextChanged(object sender, EventArgs e)
    { 
        if (textBox2.Text == "") 
        {
            button5.Enabled = false;
        }
        else
        {
            button5.Enabled = true;
        }
        
    }

1 Comment

Please add some description explaining what you believe was the problem, and why this solution fixes it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.