下面這段程式碼讓按下Enter鍵,也可以focus到下一個Control。
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.SelectNextControl(this.ActiveControl, true, true, true, true);
}
}
另外你也可以讓她只有Focus在TextBox上面,或是其他的Control上面
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
Keys keyPressed = (Keys)msg.WParam.ToInt32();
switch (keyPressed)
{
case Keys.Enter:
case Keys.Tab:
Control ctrl = this.GetNextControl(this.ActiveControl, true);
while (ctrl is TextBox == false)
{
ctrl = this.GetNextControl(ctrl, true);
}
ctrl.Focus();
return true;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
}
沒有留言:
張貼留言