简体   繁体   English

C#Textbox textchange属性事件

[英]C# Textbox textchange property event

What is the best possible way to enable the visibility of a label inside a form. 什么是启用表单内标签可见性的最佳方法。

If you see the code below . 如果你看到下面的代码。

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

Why does that label doesn't show up? 为什么这个标签没有出现? But if try to make a messagebox . 但如果试图制作一个消息框。 a messagebox pops up. 弹出一个消息框。

You are checking the value of txtboxTwo in the TextChanged event of txtboxOne . 您正在检查txtboxTwoTextChanged事件中txtboxOne

That is why messagebox block works and the later block does not. 这就是messagebox块工作的原因,后面的块没有。

Change it to: 将其更改为:

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

if you indeed want to check txtboxTwo.Text dont use else if, use if: 如果你确实想检查txtboxTwo.Text不要使用else if,请使用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;
        }
    }

检查你的条件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM