【问题标题】:Textbox will not accept more than 1 character without disappearing in C#文本框不会接受超过 1 个字符而不在 C# 中消失
【发布时间】:2015-01-04 16:25:52
【问题描述】:
private void btnClassNameA_Click(object sender, EventArgs e)
    {
        txtbClassNameA.Visible = true;
        txtbClassNameA.Focus();
    }

private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter) ;
        btnClassNameA.Text = txtbClassNameA.Text;
        txtbClassNameA.Visible = false;
    }

单击按钮后,会出现一个文本框。我不能让它一次接受超过 1 个字符而不消失。它应该通过按回车键消失。任何帮助将不胜感激!

【问题讨论】:

  • 你的 if 缺少大括号?
  • 你有if (e.KeyCode == Keys.Enter) ;,这意味着当按下回车键时什么都不执行。 ; 只会导致无操作。只要按下任何键,其余代码就会执行,因此文本框会消失。你可能打算用大括号把它们包起来?

标签: c# textbox enter keycode


【解决方案1】:

你当前的代码是这样的:

private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter) { } // does nothing, just evaluates the condition
    btnClassNameA.Text = txtbClassNameA.Text;
    txtbClassNameA.Visible = false;
}

你必须像这样改变它:

private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
       btnClassNameA.Text = txtbClassNameA.Text;
       txtbClassNameA.Visible = false;
    }
}

【讨论】:

    【解决方案2】:
        private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnClassNameA.Text = txtbClassNameA.Text;
                txtbClassNameA.Visible = false;
            }
        }
    

    如果那是您的实际代码,分号可能会让您失望。试试这个。

    【讨论】:

    • 新它一定是小东西;)非常感谢
    【解决方案3】:

    您的条件后面似乎有一个分号。

    现在它正在评估条件,然后继续更新文本并使框不可见。

    private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) 
            {
                btnClassNameA.Text = txtbClassNameA.Text;
                txtbClassNameA.Visible = false;
            }
        }
    

    可能会给你更好的结果。

    【讨论】:

      【解决方案4】:

      您的 if 语句格式不正确。试试这样:

      if (e.KeyCode == Keys.Enter)
      {
              btnClassNameA.Text = txtbClassNameA.Text;
              txtbClassNameA.Visible = false;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多