[英]Focus changes even after setting e.Handled = true
我將KeyPreview = true;
設置KeyPreview = true;
為我的Form
。 我基本上想使用箭頭鍵轉到下一個和上一個圖像,而不是將焦點更改為其他控件。 我已將Handled
屬性設置為true
但是焦點仍然在箭頭鍵按下時更改。
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
// Do stuff
}
else if (e.KeyCode == Keys.Left)
{
// Do stuff
e.Handled = true;
}
else if (e.KeyCode == Keys.Right)
{
// Do stuff
e.Handled = true;
}
}
編輯
我要實現的行為如下。
Left Arrow Key -> Previous Image
Right Arrow Key -> Next Image
現在,我的Form
上還有一些TextBox
,因此如果這些Textbox
處於焦點,我就不想轉到下一張和上一張圖像,因為那樣的話,它應該在文本中導航。
這對我有用。
KeyPreview = true;
設置KeyPreview = true;
的Form
。 如果任何TextBox
沒有焦點,則覆蓋ProcessCmdKey
並根據需要進行處理。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (textBox1.ContainsFocus || textBox2.ContainsFocus || textBox3.ContainsFocus) { return base.ProcessCmdKey(ref msg, keyData); } if (keyData == Keys.Delete) { removeRect(); return true; } else if (keyData == Keys.Left) { previousImg(); return true; } else if (keyData == Keys.Right) { nextImg(); return true; } else { return base.ProcessCmdKey(ref msg, keyData); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.