2015-02-24 97 views
0

什么是使表单中的标签可见的最佳方式。C#Textbox textchange属性事件

如果您看到下面的代码。

public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     lblgrpTwoFirst.Visible = false; 
     lblgrpTwoSecond.Visible = false; 
     lblgrpTwoThird.Visible = false; 
     lblgrpTwoFourt.Visible = false; 
    } 

    private void txtboxOne_TextChanged(object sender, EventArgs e) 
    { 
     if (txtboxOne.Text == "z") 
     { 
      MessageBox.Show("The Goose Eat the Beans"); 
     } 

     else if (txtboxTwo.Text == "x") 
     { 
      lblgrpTwoSecond.Visible = true; 
     } 

为什么该标签不显示?但如果尝试制作一个消息框。弹出一个消息框。

+2

也许是因为你已经使用了'else if(txtboxTwo.Text ==“x”)'而不是'else if(txtboxOne.Text ==“x”)'?! – 2015-02-24 13:52:18

+0

如果txtboxOne.Text ==“z”,那么txtboxTwo的文本里面什么都没有关系,如果这就是你的意思...在这种情况下,从其他人删除else if – 2015-02-24 13:56:04

+0

@TimSchmelter感谢提姆通知我..我没有看到它。我的变量声明有时令人困惑.. btw谢谢! – 2015-02-24 13:56:08

回答

0

您正在检查TextChanged事件txtboxOnetxtboxTwo的值。

这就是为什么messagebox块的作品,后面的块没有。

将其更改为:

private void txtboxOne_TextChanged(object sender, EventArgs e) 
{ 
    if (txtboxOne.Text == "x") 
    { 
     lblgrpTwoSecond.Visible = true; 
    } 
} 
0

,如果你真的想检查txtboxTwo.Text不使用否则如果使用if:

private void txtboxOne_TextChanged(object sender, EventArgs e) 
    { 
     if (txtboxOne.Text == "z") 
     { 
      MessageBox.Show("The Goose Eat the Beans"); 
     } 

     if (txtboxTwo.Text == "x") 
     { 
      lblgrpTwoSecond.Visible = true; 
     } 
    } 
0

检查条件

lblgrpTwoSecond.Visible = txtboxTwo.Text == "x" ? true : false; 
+1

哇,哇,等一下。在这种情况下不要使用'?:'运算符。 – dymanoid 2015-02-24 14:02:55

+0

@dymanoid为什么?正在谈论与先前的条件的困惑虽然执行MSIL代码的CLR? – 2015-02-24 14:08:42

+1

只是因为这里没有必要。 'lblgrpTwoSecond.Visible = txtboxTwo.Text ==“x”'就够了。 – dymanoid 2015-02-24 14:14:44