当TextBox为空时如何禁用按钮?

时间:2018-11-07 15:37:11

标签: c# button textbox disable

我想禁用按钮,直到TextBox中没有文本。我该怎么做? 我是新手,我什么都不知道,因此仅添加一个代码是很棒的。 我的代码:
          private void button1_Click(对象发送者,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 个答案:

答案 0 :(得分:0)

为此,您需要为文本框添加一个事件处理程序。休假或TextChanged。在那里您可以启用和禁用按钮。

另一方面,如果文本框为空,那么解析是否会引发异常,是否就是您想要这样做?即使不为空,它也可以包含任何无法转换为双精度的文本。

更好的解决方案是更改

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

收件人

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

答案 1 :(得分:0)

这就是我要怎么做! 步骤1.通过在Windows窗体设计器中双击您的文本框来添加TextChanged事件。 步骤2.在事件中输入此代码,将MyTextBox替换为文本框的名称,并将MyButton替换为按钮的名称!

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;

}

希望这会有所帮助!

Techcraft7:)

答案 2 :(得分: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;

}