我在窗體中有幾個控件,所以我閱讀並創建了一個窗體的事件,允許我按ENTER將焦點更改爲另一個控件。這裏是代碼:textbox將不會接受在winforms中輸入
private void frSeleccionEL_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Control nextControl = this.GetNextControl(ActiveControl, !e.Shift);
if (nextControl != null)
{
e.Handled = true;
if (!(nextControl is Label))
{
nextControl.Focus();
}
else //the next control is currently a label
{
nextControl = this.GetNextControl(ActiveControl, true);
while (nextControl != null)
{
nextControl.Focus();
if (!(nextControl is Label))
{
break;
}
nextControl = this.GetNextControl(ActiveControl, true);
}
}
}
}
}
在文本框中,它不工作,我有代碼只有數字。下面的代碼:
private void txtLocal_KeyPress(object sender, KeyPressEventArgs e)
{
//Para obligar a que sólo se introduzcan números
if (Char.IsDigit(e.KeyChar))
{
e.Handled = false;
}
else
{
if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso
{
e.Handled = false;
}
else
{
//el resto de teclas pulsadas se desactivan
e.Handled = true;
}
}
}
我的問題是,當我按下在該控制什麼也沒有發生一樣ENTER從未按下ENTER,因爲事件按鍵此控件不火。
我刪除了控件並重新制作了它,問題依然存在。
我不知道什麼是錯的。
您需要將Multiline屬性設置爲True。 – Eterm
如何將KeyPress EventHandler添加到文本框?你有2種方法中的1種,可以使用設計器或在代碼中將處理程序添加到文本框中。即txtLocal.KeyPress + = new KeyPressEventHandler(txtLocal_KeyPress); –
我添加了keyPress EventHandler與設計師,如果我按下任何鍵事件的工作,但如果我按下ENTER不要做任何事情 –