【问题标题】:Suppress the display of the input into the TextBox禁止在 TextBox 中显示输入
【发布时间】:2016-07-14 04:48:51
【问题描述】:

我有一个包含RichTextBox 的 winforms 程序。 用户将文本输入到 RichTextBox。 我希望通过键盘事件而不是通过 textBox1.Text 属性接收输入,验证字符串并稍后仅在 RichTextBox 中显示。 即使用户将文本输入到 RichTextBox 中,如何防止 RichTextBox 显示用户输入的文本? RichTextBox 被选中并具有焦点。 我很抱歉。我只是想简化问题,因此我忽略了它不是 TextBox 而是 RichTextBox。事实证明这很重要,因为建议的解决方案基于 PassowrdChar 属性,而 RichTextBox 本身并不支持该属性。我不希望为甚至没有被用作这样的属性创建继承类,只是为了抑制在输入时显示用户输入。

【问题讨论】:

  • 为什么不想显示字符串?是不是像密码字符串********?
  • 设置TextBox的PasswordChar Property可以隐藏文字。
  • 您的问题解决了吗?

标签: c# winforms textbox


【解决方案1】:

你可以用这个:

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char c = e.KeyChar;
    // ..
    // handle the character as needed
    // ..

    e.Handled = true;  // suppress the RTB from receiving it
}

请注意,您可能希望也可能不希望将鼠标事件(如鼠标右键单击)处理为通过鼠标控制插入..

【讨论】:

    【解决方案2】:

    您实际上可以使用 KeyDown 事件。通过这样做,您可以验证用户输入。

    Tutorial

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        //
        // Detect the KeyEventArg's key enumerated constant.
        //
        if (e.KeyCode == Keys.Enter)
        {
        MessageBox.Show("You pressed enter! Good job!");
        }
        else if (e.KeyCode == Keys.Escape)
        {
        MessageBox.Show("You pressed escape! What's wrong?");
        }
    }
    

    话虽如此,您必须将用户输入存储在字符串变量中,通过事件对其进行验证,然后才能在文本框中设置变量值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-23
      • 2012-06-11
      • 2021-03-17
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      相关资源
      最近更新 更多