0

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

3 回答 3

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;

}
于 2019-11-14T10:16:23.647 回答
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;

}

希望这可以帮助!

技术7 :)

于 2018-11-07T16:30:20.410 回答
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;
}
于 2018-11-07T15:53:50.233 回答