Textchanged事件调用自定义方法,但自定义方法报告文本框文本为空

本文关键字:自定义方法 文本 报告 事件 调用 Textchanged | 更新日期: 2023-09-27 18:13:02

我已经有一段时间没有接触c#了,我试图帮助我的一个新程序员朋友,被以下内容彻底难住了:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        activateEnterButton();
        TextBox t = (TextBox)sender;
        string theText = t.Text;
        MessageBox.Show("text: " +theText);
    }
    private void activateEnterButton()
    {
       bool allGood = true;
        foreach (Control control in Controls )
        {
            if (control is TextBox)
            {
                string test = ((TextBox)control).Text;
                if (test.Length == 0)
                {
                    MessageBox.Show("text: " +test);
                    allGood = false;
                    break;
                }
            }
        }
        btnEnter.Enabled = allGood; 
    }

我们的目标非常简单:我们有5个文本框,每个文本框都需要在按钮启用之前有一些文本。当每个都有文字时,按钮被启用。

当我在调试过程中遍历代码时,一切都被称为ok,但无论我在文本框中放入多少文本,activateEnterButton都不会知道它在那里。两个messagebox也显示了不同的输出:activateEnterButton中的messagebox从来没有任何输出,而事件处理程序中的messagebox总是有输出。

任何帮助都将非常感激。谢谢你。

我已经删除了对activateEnterButton()的调用,我已经将该代码的核心放在textBox5的事件处理程序中,但按钮仍然没有被启用。

我接受的答案没有给我我想要的功能(在textbox5中输入数据将使按钮活动),下面的代码给了我所有我想要的功能。最后,我的错误的原因是因为A) foreach从最后一个控件迭代到第一个,B)我在表单上的最后一个文本框控件是一个只读文本框控件,它的文本总是",因此我总是从我以前的代码中被转储。无论如何,新代码:

    private void checkMe()
    {
        bool allGood = true;
        foreach (Control control in Controls)
        {
            // Make sure the ReadOnly textbox doesn't cause false
            if (control.Name.Equals("ReadOnlyTextBox"))
            {
               // MessageBox.Show("hidden textbox: " + ((TextBox)control).Text);
                allGood = true;
            }
            else if (control is TextBox)
            {
                string test = ((TextBox)control).Text;
                //MessageBox.Show("test: " + test);
                if (test.Length < 1)
                {
                    allGood = false;
                   // MessageBox.Show("All textboxes need input");
                    break;
                }
                else
                {
                    allGood = true;
                }
            }
        }
        btnEnter.Enabled = allGood; 
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        checkMe();
    }
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        checkMe();
    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        checkMe();
    }
    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        checkMe();
    }
    private void textBox5_TextChanged(object sender, EventArgs e)
    {
        checkMe();
    }

Textchanged事件调用自定义方法,但自定义方法报告文本框文本为空

在你的activateEnterButton()方法你循环通过控件,如果控件是文本框;检查是否有文本

说如果textbox1已经触发textchanged事件;这如何保证其他文本框中有文本呢?

你说在你的帖子The two MessageBoxes show different output as well: ..应该是。

textbox1已经触发了textchanged事件,在textchanged事件中,您在消息框中显示了文本,但是在方法activateEnterButton()中,您正在循环形式中的所有控件,没有像textbox1 .. 5那样的顺序保证(在该顺序循环中将检查它们),并且一旦它没有文本,您就会跳出循环。在你的方法中你不会在messagebox中看到任何文本。

最好的方法是这样做(考虑你有TextBox 1..5;只修改TextBox5上的文本)

private void textBox5_TextChanged(object sender, EventArgs e)
{
   bool allGood = false;
    foreach (Control control in Controls )
    {
        if (control is TextBox)
        {
            string test = ((TextBox)control).Text;
            if (test.Length > 0)
            {
                allGood = true;
            }
            else
            {
                MessageBox.Show("Fill all textbox first");
                break;
            }
        }
    }
    btnEnter.Enabled = allGood; 
}