按Enter键并在C#中执行代码 [英] Press enter key and execute code in c#

查看:353
本文介绍了按Enter键并在C#中执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按键盘上的Enter键时如何获得代码提示,例如,当在文本框中输入密码然后按Enter时,应执行登录按钮的代码

How do you get to the code behing when pressing the enter key on the keyboard eg.when you enter password in the text box then press enter the code for the login button should be executed

推荐答案

好吧,假设具有相同形式的按钮会更好,然后在表单属性中将AcceptButton属性设置为将通过按Enter激活的按钮并且click事件中的代码将被执行.

实际上,解释它比做它更困难.在这里看看一个例子
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx [ ^ ]

干杯!
Well let''s say that if will be nicer to have a button in the same form, and then in the form properties set the AcceptButton property to a button that will be activated by pressing enter and the code in the click event will be executed.

Actually it is more difficult to explain it than do it. Take a look here for an example
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx[^]

Cheers!


添加代码来处理文本框的KeyPressed事件.在该处理程序中,添加类似以下内容的代码:

Add code to handle the KeyPressed event for your text box. In that handler, add code something like:

if (e.KeyChar == '\r')
{
    // add your code here

    // set the Handled flag to true
    e.Handled = true;
}



代码格式.



Code Formatted.


如果您想通过Enter键链接按钮,然后设置按钮控件的Accept Button属性...
:)
if you want to link button with Enter key press then set that Accept Button property of button control ...
:)


这篇关于按Enter键并在C#中执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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