【问题标题】:Textbox equal to any other Textbox文本框等于任何其他文本框
【发布时间】:2026-02-15 06:10:01
【问题描述】:

我希望这样,如果我在第 2 行中的任何 TextBox 与第 2 行中的任何其他 TextBox 具有相同的文本,它们都将背景颜色变为红色。这是我到目前为止所做的:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
    {
        if (c is TextBox && c.Name.StartsWith("textBox2"))
        {
            ((TextBox)c).TextChanged += textBox_TC;
        }
    }
}

private void textBox_TC(object sender, EventArgs e)
{            
    TextBox textBox = (TextBox)sender;           
    if(textBox.Text == textBox.Text && textBox.Text.Length == 1)
    {
        textBox.BackColor = System.Drawing.Color.Red;
    }
    if (textBox.Text.Length == 0)
    {
        textBox.BackColor = System.Drawing.Color.White;
    }
}

而不是如果 textBox.Text == textBox.Text。我希望它类似于 if textBox.Text == anyother.textBox.Text 的名称以 textBox2 开头。 这是可能的还是我必须以其他方式解决这个问题?

【问题讨论】:

  • 你有多少textBox2s?
  • 9,我在做数独游戏

标签: c# .net winforms


【解决方案1】:

开始构建一个List<TextBox>,文本框具有相同的起始名称

List<TextBox> box2;

private void Form1_Load(object sender, EventArgs e)
{
    // Using LINQ to extract all the controls of type TextBox 
    // having a name starting with the characters textBox2 
    // BE AWARE - Is case sensitive -
    box2 = this.Controls.OfType<TextBox>()
                        .Where(x => x.Name.StartsWith("textBox2")).ToList();

    // Set to each textbox in the list the event handler
    foreach(TextBox t in box2)
        t.TextChanged += textBox_TC;

}

现在您可以在 TextChanged 事件中编写

private void textBox_TC(object sender, EventArgs e)
{            
    TextBox textBox = (TextBox)sender;
    if(textBox.Text.Length == 1)
    {
        // Check if Any text box has the same text has the one 
        // firing the event (excluding the firing textbox itself)
        bool sameText = box2.Any(x => x.Text == textBox.Text && 
                                     !x.Equals(textBox));

        // Got one textbox with the same text?
        if(sameText) 
           textBox.BackColor = System.Drawing.Color.Red;
    }
    else if (textBox.Text.Length == 0)
    {
        textBox.BackColor = System.Drawing.Color.White;
    }
}

编辑
根据您在下面的评论,您可以确保以这种方式重置背景颜色

警告未测试:只需跟踪即可。

private void textBox_TC(object sender, EventArgs e)
{            
    TextBox textBox = (TextBox)sender;
    if(textBox.Text.Length == 1)
    {
        foreach(TextBox t in box2)
            t.BackColor = Color.White;

        // Get all textboxes with the same text 
        var sameText = box2.Where(x => x.Text == textBox.Text);
        if(sameText.Count() > 1) 
        {
            foreach(TextBox t in sameText)
                t.BackColor = Color.Red;
        }
    }
    else if (textBox.Text.Length == 0)
    {
        textBox.BackColor = System.Drawing.Color.White;
    }
}

【讨论】:

  • 我复制了您的代码,但在此处出现错误“x.Name.StartsWith("textbox2").ToList();”。 “.Where”前面应该有什么东西吗?我以前没见过那个代码。
  • 我在初稿中忘记了括号
  • 这只会设置触发事件的 TextBox 的样式,我认为 OP 也想要匹配的。
  • @Crowcoder 从一开始就是我的意图,但没那么重要
  • @Crowcoder 这会有点复杂。主要是因为您需要重置所有背景颜色以防不再匹配(假设您有两个以红色着色的文本框,然后在其中一个中退格,当 TextChanged 触发时,您需要重新检查所有内容以删除背景颜色)跨度>