Reputation: 9
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();
}
Upvotes: 0
Views: 1780
Reputation: 113
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:
In the loaded event of your form (Form1_Loaded
).
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 ♥
Upvotes: 1
Reputation: 1
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;
}
Upvotes: 0
Reputation: 1151
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;
}
Upvotes: 0
Reputation: 129
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 :)
Upvotes: 0