C#如何实现登陆时回车响应??
使用窗口的Form.AcceptButton 属性,把窗口AcceptButton 属性设置成登录按钮,当用户输入回车时,就会执行登录按钮相应事件。
代码示例:
public void CreateMyForm()
{
// Create a new instance of the form.
Form form1 = new Form();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (10, 10);
// Set the text of button2 to "Cancel".
button2.Text = "Cancel";
// Set the position of the button based on the location of button1.
button2.Location
= new Point (button1.Left, button1.Height + button1.Top + 10);
// Set the caption bar text of the form.
form1.Text = "My Dialog Box";
// Display a help button on the form.
form1.HelpButton = true;
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
}
MSDN相关说明:
Form.AcceptButton 属性
获取或设置当用户按 Enter 键时所单击的窗体上的按钮。
命名空间: System.Windows.Forms
程序集: System.Windows.Forms(System.Windows.Forms.dll 中)
语法
public IButtonControl AcceptButton { get; set; }
属性值
Type: System.Windows.Forms.IButtonControl
IButtonControl,表示要用作窗体的“接受”按钮的按钮。
备注
此属性可以指定用户在您的应用程序中按 ENTER 键时所需进行的默认操作。必须是分配给此属性的按钮 IButtonControl ,是在当前的窗体上还是位于当前窗体上容器中。
可以使用此属性以允许用户可快速定位一个简单窗体使他们能够在他们结束而不是手动单击接受按钮使用其鼠标时只需按 ENTER 键。
如果当前选定的控件在窗体上截获 ENTER 键,并处理它,则可能不激活接受按钮。例如,多行文本框控件允许选择要在控件中插入新行符时要按下 ENTER 键。
2013-08-22
2013-08-22
2013-08-22
if(e.KeyChar==(Char)10)
2.如蛋蛋所说