0

I want to disable button until there is text in TextBox. How can I do it? I'm beginner and I don't know anything so just a code that I should add is great. My code:
private void button1_Click(object sender, EventArgs e) {

        double wiek = double.Parse(textBox1.Text);
        double gotowka = double.Parse(textBox2.Text);

        if (wiek >= 15 && gotowka >= 30 || gotowka >= 130)
        {
            MessageBox.Show("Możesz wejść!");
        }
        else
        {
            MessageBox.Show("Nie możesz wejść!");
        }

        if (wiek >= 15 && gotowka >= 30)
        {
            double reszta = gotowka - 30;
            textBox3.Text = reszta.ToString();
        }

        if (wiek < 15 && gotowka >= 130)
        {
            double reszta2 = gotowka - 130;
            textBox3.Text = reszta2.ToString();

        }

        if (wiek < 15 && gotowka >= 30)
        {
            double reszta3 = gotowka;
            textBox3.Text = reszta3.ToString();
        }

        if (wiek >=15 && gotowka < 30)
        {
            double reszta4 = gotowka;
            textBox3.Text = reszta4.ToString();
        }
        if (wiek >= 15 && gotowka >= 130)
        {
            double reszta5 = gotowka - 30;
            textBox3.Text = reszta5.ToString();
        }
        if (wiek < 15 && gotowka >= 130)
        {
            double reszta6 = gotowka - 130;
            textBox3.Text = reszta6.ToString();
        }
2

4 Answers 4

1

Your code isn't relevant to what you asked, but anyway

If you want to check every case where your textbox is empty, you should check weather it's empty or not in two places:

  1. In the loaded event of your form (Form1_Loaded).

  2. In the TextChanged event of the textBox

As following :

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

If you're too beginner to understand what I wrote, tell me in a comment to walk you through it ♥

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

Comments

0

Here's how I would do it! Step 1. add a TextChanged event by double clicking your textbox in the windows forms designer. Step 2. enter this code into the event, replace MyTextBox with the name of your text box, and MyButton with the name of your button!

if (MyTextBox.Text == "")
{
    //(if you would like to make the button disappear, do this)
    MyButton.Visible = false;
    //(if you would like to make the button gray out, do this)
    MyButton.Enabled = false;
}
else
{

    //(if you would like to make the button disappear, do this)
    MyButton.Visible = true;
    //(if you would like to make the button gray out, do this)
    MyButton.Enabled = true;

}

Hope this helps!

Techcraft7 :)

Comments

0

To do this you would need to add an event handler for the text box. Either on Leave or TextChanged. There you could enable and disable the button.

On the other hand, can it be that you want this just because the parse throws an exception if the text box is empty? Even if it is not empty it can contain any text that could not be converted to a double.

A better solution could be to change the

double wiek = double.Parse(textBox1.Text);
double gotowka = double.Parse(textBox2.Text);

To

double wiek;
double gotowka;

bool isParsed = double.TryParse(textBox1.Text, out wiek);
if (!isParsed)
{
   //TODO: some error handling, telling the user it is not a number
   MessageBox.Show("Nie numer!");
   return;
}

isParsed = double.TryParse(textBox2.Text, out gotowka);
if (!isParsed)
{
   //TODO: some error handling, telling the user it is not a number
   MessageBox.Show("Nie numer!");
   return;
}

5 Comments

That does the first part, but now I'm getting error with double gotowka .
you just need to do the same with the other number as well. "bool isParsed = double.TryParse(textBox2.Text, out gotowka); if (!isParsed)..."
I have added the check to the other variable too.
Agh I don't know why it's not working, but thanks for the help
I have added some "error handling". Could you please try the code above? Also, what do you mean by it is not working?
0
if (MyTextBox.Text == "")
{
    //(if you would like to make the button disappear, do this)
    MyButton.Visible = false;
    //(if you would like to make the button gray out, do this)
    MyButton.Enabled = false;
}
else
{

    //(if you would like to make the button disappear, do this)
    Button.Visible = true;
    //(if you would like to make the button gray out, do this)
    Button.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.