How to disable button if textbox is empty

2019-09-12 04:43发布

问题:

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);
            }
        }
    }
}

}

回答1:

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.



回答2:

Hope it work!

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


回答3:

Try this:

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


回答4:

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
    }
}


回答5:

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