按下输入时无法检测到。 [英] Can't Detect When Enter Is Pressed.

查看:73
本文介绍了按下输入时无法检测到。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看我文本框的这个keydown事件:

Hi, take a look at this keydown event of my textbox:

private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                txtInput.Clear();
            }
        }




简单否?好吧,我在"钥匙"下面都有波浪形的红线。话。我从msdn文档中获取了这些信息,因此无论出于何种原因,他们都给出了错误的语法。

Simple no? Well, I have wavy red lines beneath both "key" words. I got that information from the msdn docs, so they, for whatever reason, are giving the wrong syntax.

我只想在输入被按下时清除文本框。有人可以帮帮我吗?

I just want the textbox to clear when enter is pressed. Could someone help me out?

谢谢

编辑:错误是:" KeyEventArgs确实不包含Key的定义。"

The error is: "KeyEventArgs does not contain a definition for Key."

第二个(对于第二个"key"字)说"当前上下文中不存在名称键"。

The second one (for the second "key" word) says "The name key does not exist in the current context".

推荐答案

祝你有所帮助

你的代码在WPF中有效

Your code is valid in WPF

 private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                txtInput.Clear();
            }
        }




对于Windows窗体文本框,您应该使用KeyCode或KeyValue


For Windows Forms Textbox you should use KeyCode or KeyValue

 private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                txtInput.Clear();
            }
        }




Or

 private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtInput.Clear();
            }
        }


最好的问候

Vikram Manjare

请记住点击"标记为答案" ;解决您的问题的回复,并点击"取消标记为答案"如果不。这对阅读此主题的其他社区成员有益。




Best Regards
Vikram Manjare
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


这篇关于按下输入时无法检测到。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆