こんにちは、特定のフォームにいくつかのテキストボックスが入力された C# winform アプリケーションがあります。右矢印キーを押すと、タブキーを押すのと同じ動作を模倣するようにしたいと思います。これを行う方法がよくわかりません。
タブキーの動作をまったく変更したくありません。そのフォームで右矢印キーを使用して同じことを行うだけです。
誰でも提案できますか?
これを行うには、フォームの OnKeyUp メソッドをオーバーライドする必要があります...
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
Control activeControl = this.ActiveControl;
if(activeControl == null)
{
activeControl = this;
}
this.SelectNextControl(activeControl, true, true, true, true);
e.Handled = true;
}
base.OnKeyUp(e);
}
これはあなたが求めていることを達成すると思います:
private void form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
Control activeControl = form1.ActiveControl;
// may need to check for null activeControl
form1.SelectNextControl(activeControl, true, true, true, true);
}
}
フォームで KeyDown イベントを使用してキー ストロークをトラップし、必要なアクションを実行できます。例えば:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Right)
{
this.SelectNextControl(....);
e.Handled = true;
}
}
フォームの KeyPreview プロパティを True に設定することを忘れないでください。