【问题标题】:Canceling textboxes if I type in other textbox如果我输入其他文本框,则取消文本框
【发布时间】:2014-11-07 03:19:20
【问题描述】:

我的表单中有几个文本框。我想输入一个文本框,其他文本框将被自动取消(取消,因此用户将无法再在其他文本框中输入)。 如果用户删除了他在文本框中输入的单词(或者换句话说:如果文本框为空),所有其他文本框将再次启用。

这是我目前得到的:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Trim().Length > 0)
        {
            // Disable other textboxes (I have no idea how can I do that).
        }
        else if (textBox1.Text.Trim().Length == 0)
        {
            // Enable other textboxes
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Trim().Length > 0)
        {
            // Keep this comboBox enabled while typing in textBox1.
        }
    }

如何在 priticillar 文本框中输入内容时禁用其他文本框? (同时保持组合框处于启用状态)。

注意:我想在 textBox2 上做同样的事情(当我输入 textBox2 时,文本框 1 和 3 将被禁用)和 textBox3。

【问题讨论】:

  • 顺便说一句,这听起来非常糟糕的用户体验。请不要这样做。
  • 您也没有注意您使用的 UI 技术。如果您使用的是 WPF,则执行此操作的方式与使用 WinForm 不同,如果您在 WebForm 或 MVC 中执行此操作,您实际上可以使用 Javascript 来实现。

标签: c# textbox


【解决方案1】:

你的'else if'不需要条件,'else'就可以了,好像长度大于0,唯一的另一种可能性是0。你也可以使用发件人而不是硬编码控件名称。

然后为要禁用的文本框设置启用属性。您可以遍历表单上的所有文本框,不包括您正在输入的文本框,或者只是手动列出它们。 SImpler 将文本框放在一个 groupbox 中,然后如果您禁用 groupbox,它将禁用其中的控件。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var senderTextBox    = (TextBox)sender;
    var textBoxesEnabled = senderTextBox.Text.Trim().Length == 0;

    textBox2.Enabled = textBoxesEnabled;
    textBox3.Enabled = textBoxesEnabled;

    // OR

    groupBox1.Enabled = textBoxesEnabled;
}

回复编辑:您可以链接文本框,比如其中 4 个,禁用最后 3 个,然后:

void TextBox1TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox2.Enabled = !isTextEmpty;
}
void TextBox2TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox1.Enabled = isTextEmpty;
    textBox3.Enabled = !isTextEmpty;
}
void TextBox3TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox2.Enabled = isTextEmpty;
    textBox4.Enabled = !isTextEmpty;
}
void TextBox4TextChanged(object sender, System.EventArgs e)
{
    var isTextEmpty  = ((TextBox)sender).Text.Trim() == "";
    textBox3.Enabled = isTextEmpty;
}

但是对于大量的文本框,另一种选择是让多个文本框共享相同的 TextChanged 事件。您需要单击每个 TextBox 控件,进入事件列表并手动选择 TextChanged 的​​方法。方法如下:

private void TextBoxGroup_TextChanged(object sender, EventArgs e)
{
    var groupOrder    = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4 };
    var senderTextBox = (TextBox)sender;
    var senderIndex   = groupOrder.IndexOf(senderTextBox);

    var isTextEmpty = senderTextBox.Text.Trim() == "";
    if (senderIndex != 0) groupOrder[senderIndex - 1].Enabled = isTextEmpty;
    if (senderIndex != groupOrder.Count - 1) groupOrder[senderIndex + 1].Enabled = !isTextEmpty;
}

【讨论】:

  • 它有效,但假设我想使用 textbox2 并禁用 1 和 3,我需要那个 if 语句.. 我怎样才能让你的代码适合 if 语句?
  • 更新了我的答案,这可能会有所帮助。
  • 再次更新,回头看看我的答案,意识到 TextBox 控件列表可能是一个更清洁的解决方案。
【解决方案2】:

使用Controls 集合和一些LINQ

if (textBox1.Text.Trim().Length > 0)
{ 
     var textboxes = this.Controls.OfType<TextBox>().Where(x => x.Name != "textBox1");
     foreach(var tBox in textboxes)
         tBox.Enabled = false;
}

【讨论】:

  • 谢谢你,虽然你忘了添加 tBox.Enabled = true;空 if 语句上的代码。
  • 我用 else if 在 textBox2 上做同样的事情(并且 textBox1 和 3 将被禁用)但它没有工作,为什么?
  • 因为条件x.Name != "textBox1"
  • 那我该如何解决呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多