0
votes

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();
        }
3
Assuming you're talking about WinForms. This should get you started: docs.microsoft.com/en-us/dotnet/framework/winforms/… You basically need to write event handlers for a variety of events, check the values and then set the Enabled property for your textbook to true or false.Jason Armstrong

3 Answers

0
votes

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 :)

0
votes

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;
}
0
votes
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;

}