2

First of all sorry for my bad english. I'm beginner at C# and i made a Windows forms application but i can't disable one button if a textbox is empty. I tried some of the Enabled methods but they didn't work. Hope someone can help me fix this. Thank you very much

public partial class ModulusForm : Form
{
    public double nje;
    public double dy;
    public double pergjigja;
    public double rezultati;
    public ModulusForm()
    {

        InitializeComponent();
        Button btn = new Button();
        btn.Click += new EventHandler(butoniGjenero_Click); 
    }
    private void butoniPerfundo_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    private void butoniGjenero_Click(object sender, EventArgs e)
    {
        Random random = new Random();
        nje = random.Next(1, 100);
        dy = random.Next(1, 100);
        if (nje > dy)
        { textboxPyetja.Text = "X = " + nje + "    " + "dhe" + "    " + "Y = " + dy; }
        else if (nje > dy)
        {
            nje = random.Next(1, 100);
            dy = random.Next(1, 100);
        }
        rezultati = nje / dy;
    }
    private void butoniPastro_Click(object sender, EventArgs e)
    {
            textboxPyetja.Clear();
            textboxPergjigja.Clear();
            textboxPergjigjaSakt.Clear();
    }
    private void butoniVerteto_Click(object sender, EventArgs e)
    {
        try
        {
            pergjigja = double.Parse(textboxPergjigja.Text);
        }
        catch
        {
            var informim = MessageBox.Show("Rishiko fushat!", "Verejtje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        if (textboxPergjigja.Text == "")
        {
            //nothin' baby
        }
        else
        {
            if (textboxPyetja.Text == "")
            {
                var informim = MessageBox.Show("Fusha e pyetjes eshte null!", "Verejtje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (pergjigja == rezultati)
                {
                    textboxPergjigjaSakt.Text = "Pergjigja eshte e sakte";
                }
                else
                {
                    textboxPergjigjaSakt.Text = "Gabim." + " " + "Pergjigja e sakte eshte: " + "" + rezultati;
                }
                comboboxVargu.Items.Add(nje + " / " + dy + " = " + rezultati);
            }
        }
    }
}

}

2
  • 3
    Handle the textbox's TextChanged event. Inside of that event handler, check to see if the textbox's Text property is an empty string. If it's empty, disable the button: myButton.Enabled = !textBox.Text.IsNullOrEmpty(); Commented Jun 26, 2016 at 17:01
  • The elegant way to handle this sort of thing is to attach an event handler to the Application.Idle event and perform whatever status updates you need in there. Assuming you are using WinForms. Commented Jun 26, 2016 at 17:35

6 Answers 6

8

Credit to @Cody Gray for already suggesting this; I have just expanded it, so you can see how to implement and how it works

Overview
You can wire up an event handler for when your textboxPergjigja.Text's text has changed.

In the handler you create, you can then evaluate whether your button should be Enabled or not - using the string.IsNullOrWhiteSpace() check to set this.

First:
In your constructor for the form, subscribe to the textboxPergjigja.Text text box's TextChanged event.

Like this:

public ModulusForm()
{
    InitializeComponent();
    Button btn = new Button();
    btn.Click += new EventHandler(butoniGjenero_Click);

    // Add the subscription to the event:
    textboxPergjigja.TextChanged += textboxPergjigja_TextChanged;
}

Next:
Add a handler that matches the correct delegate signature for that event.

Like this:

public textboxPergjigja_TextChanged(object sender, TextChangedEventArgs e)
{
    // If the text box is not "empty", it will be enabled;
    // If the text is "empty", it will be disabled.
    butoniVerteto.Enabled = !string.IsNullOrWhiteSpace(textBoxPergjigja.Text);            
}

This way, whenever the text in the textBoxPergjigja text box is changed; the evaluation is run and your button will always be enabled/disabled correctly.

Hope this helps! :)

Additional Info
You can also use textBox.Text.IsNullOrEmpty(), and it will still work - as suggested by @Cody

I have used string.IsNullOrWhiteSpace(), as opposed to textBox.Text.IsNullOrEmpty() for the following reasons:

  • The .IsNullOrEmpty() method only checks if the textBox.Text is either null or the total amount of characters is equal to 0.

The problem this might pose is, if the user only enters a space in the textbox, it is no longer Empty or null; thus this check will return true. If the logic of the program requires that an actual value be entered into the textbox, this logic can be flawed.

  • On the other hand: The string.IsNullOrWhiteSpace() check will check on 3 conditions - if the input string is null, Empty and contains only whitespace characters (space, newline etc.), also.

I hope this adds a little bit of extra fluff to give you an informed decision for future.

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

2 Comments

I wonder if the condition is more than one textbox to be checked, how do you "bind" the text change event? Thanks.
@Stu_Dent - in that case you would need to keep track of all your textboxes, and have some logic that checks them all and asserts whether you want the button enabled; and then enable the button based on that. In terms of binding the textbox events, you would simply have a more "generic" name for your event handler (e.g. Textbox_TextChanged), which then runs the logic for whether the button is enabled. Then, to bind them, add the one event handler to each textbox - e.g. textbox1.TextChanged += Textbox_TextChanged; textbox2.TextChanged += Textbox_TextChanged;... etc.
6

Hope it work!

private void YourTextBox_TextChanged(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(YourTextBox.Text))
        YourButton.Enabled = false;
    else
        YourButton.Enabled = true;
}

Comments

2

Handle it on TextChanged event of your TextBox. (double click on your textbox control when in designed, which will automatically create the text changed event for you).

private void textboxPyetja_OnTextChanged(..blah blah)
{
    if(String.IsNullOrWhitespace(txtTargetTextbox.Text)
    {
        //disable your control
    }
    else
    {
        //enable your control
    }
}

Edit after 4 years - for some reason: And here's a one-liner version, some people just love them...

private void textboxPyetja_OnTextChanged(..blah blah)
{
    btnILoveButtons.Enabled = string.IsNullOrWhitespace(txtTargetTextbox.Text);
{

Comments

1

Try this:

if (String.IsNullOrEmpty(textboxPergjigja.Text))
    butoniVerteto.Enabled = false; 
else 
    butoniVerteto.Enabled = true;

4 Comments

I just tried, it's disabling button. But when textbox is not empty the button is not changing, it stays disabled.
@Dren Where did you place this if ?
butoniVerteto_Click
And well, you need a way to check this if when the user interacts with the textbox I guess, I'll try to think of some other way maybe.
0

add an event for edit text in the txtbox. when the text changes, enable the button

Comments

0

Change textBox1 for your textbox name then change button1 name for you button name

if (textBox1.Text == "")
  {
    button1.Enabled = false;
  }
else
  button1.Enabled = true;

Comments

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.