【问题标题】:Visual Studio c# Key press enter not workingVisual Studio c#按键输入不起作用
【发布时间】:2016-07-15 18:30:36
【问题描述】:

大家好,我一直在为我的代码苦苦挣扎!我做了一些研究,但我不明白为什么我的代码不起作用......请帮助!

private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)13)
        {
            Debug.WriteLine("It's working!");
            enterKey = true;
        }
        else
        {
            enterKey = false;
        }
    }

    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        Debug.WriteLine("The text is changing");

        if (enterKey == true)
        {
            encryptKey = encryptInTextBox.Text;
            Debug.WriteLine("The key is " + encryptKey);
        }
    }

显然,由于文本框的创建方式,我无法更改“TextChangedEvenArgs”,每当我更改它时,都会出现错误。 所以,我决定这样做,谢谢你的帮助!

【问题讨论】:

    标签: c# winforms keypress


    【解决方案1】:

    为什么不直接在文本框KEYPRESS或KEYUP事件中检查ENTER键?

    【讨论】:

    • 是的,但我必须将“TextChangedEvenArgs”更改为“System.Windows.Forms.KeyPressEventArgs”对吗?
    • 是的。您可以在编辑器顶部的“方法”组合框中选择 KeyPress 或 KeyUp 事件。将鼠标放在 TextChanged 事件中,然后查看可以设置 KeyPress/KeyUp 事件的内容。并将您的 char(13) 评论家放在该事件中。我的意思是,您现在使用的两个事件将合二为一。
    • 谢谢! “它起作用了!” ;P 我遇到了一些小问题,但后来解决了,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多