textBox值和按钮单击计数 - 在文本框值上执行代码depedant

时间:2012-10-24 04:32:10

标签: c# winforms

我目前正在处理点击计数和textBox值。 Button1目的是根据texbox7具有的值执行特定功能。当我尝试触发按钮点击事件时,我没有得到任何结果。有人建议/帮忙吗?

代码

    private List<string> messages = new List<string>() { "Option1", "Option2", "Option3", "Option4" };
  private void button1_Click(object sender, EventArgs e)
  {

    if (textBox7.ToString() == "Option1")
                {
                    int min = max;
                    int n = 0;
                    string s = "";

                    sw.Start();


                }
                else if (textBox7.ToString() == "Option2")
                {
                }
                else if (textBox7.ToString() == "Option3")
                {
                }
                else if (textBox7.ToString() == "Option4")
                {
                }
                else if (textBox7.ToString() == "")
                {
                    MessageBox.Show("Please input information");
                }
       }

3 个答案:

答案 0 :(得分:2)

而不是

if (textBox7.ToString() == "Option1")

应该是

if (textBox7.Text == "Option1")

您应该与TextBox中的值进行比较,并且您可以使用文本框的Text属性来获取该值。

您的textBox7.ToString()会为您提供类似System.Windows.Forms.TextBox, Text: text的内容。因此,你没有进行任何检查。将您的值与Text属性进行比较,它应该有效。

答案 1 :(得分:1)

检查TextBox.Text声明中的switch-case属性:

private void button1_Click(object sender, EventArgs e)
{
    switch (textBox7.Text)
    {
        case "Option1":
            //do something
        case "Option2":
            //do something
        case "Option3":
            //do some thing
        case "Option4":
            //do something
            break;
        // If the value of switch-Expression is not 1, 2, 3 or 4 the 
        // default case is executed.
        default:
            MessageBox.Show("Please input information");
            break;
    }
}

答案 2 :(得分:0)

简单使用.Text属性在文件后面的代码中获取textBox的文本值 textBox7.Text属性而非 testBox7.ToString()进一步模式使用if-else语句或switch语句,因为它们都是条件语句和基于条件的执行。

振作起来!