【问题标题】:How to disable buttons when TextBox is empty?TextBox为空时如何禁用按钮?
【发布时间】:2018-11-07 15:37:11
【问题描述】:

我想禁用按钮,直到 TextBox 中有文本。我该怎么做? 我是初学者,我什么都不知道,所以我应该添加的代码很棒。 我的代码:
私人无效按钮1_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();
        }

【问题讨论】:

标签: c# button textbox disable


【解决方案1】:

我会这样做! 步骤 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】:

    为此,您需要为文本框添加一个事件处理程序。离开或 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;
    }
    

    【讨论】:

    • 这是第一部分,但现在我遇到了 double gotowka 错误。
    • 你只需要对其他号码做同样的事情。 "bool isParsed = double.TryParse(textBox2.Text, out gotowka); if (!isParsed)..."
    • 我也将检查添加到另一个变量。
    • 啊,我不知道为什么它不起作用,但感谢您的帮助
    • 我添加了一些“错误处理”。你能试试上面的代码吗?另外,它不起作用是什么意思?
    【解决方案3】:
    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 2020-02-18
      • 2015-07-23
      • 2016-05-13
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      相关资源
      最近更新 更多