简体   繁体   English

即使设置e.Handled = true,焦点也会改变

[英]Focus changes even after setting e.Handled = true

I've set KeyPreview = true; 我将KeyPreview = true;设置KeyPreview = true; for my Form . 为我的Form I basically want to use the arrow keys to go to the next and previous images instead of changing focus to different controls. 我基本上想使用箭头键转到下一个和上一个图像,而不是将焦点更改为其他控件。 I've set the Handled property to true but still the focus changes on arrow key press. 我已将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;
    }
}

EDIT 编辑

The behavior I want to achieve is as follows. 我要实现的行为如下。

Left Arrow Key -> Previous Image
Right Arrow Key -> Next Image

Now, I also have a few TextBox es on my Form and I therefore do not want to go to next and previous images if those Textbox es are in focus because then it should navigate through the text instead. 现在,我的Form上还有一些TextBox ,因此如果这些Textbox处于焦点,我就不想转到下一张和上一张图像,因为那样的话,它应该在文本中导航。

This worked for me. 这对我有用。

  1. Do not set KeyPreview = true; 不要将KeyPreview = true;设置KeyPreview = true; for the Form . Form
  2. Override ProcessCmdKey and process as needed if any of the TextBox es do not have focus. 如果任何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.

相关问题 如何检测何时其他代码设置e.Handled = true? - How To Detect When Other Code Setting e.Handled = true? 是否可以在RelayCommand中设置e.Handled = true? - Is it possible to set e.Handled = true in a RelayCommand? 为什么 e.Handled = true 不起作用? - Why e.Handled = true not working? 将UserControl气泡中的MouseDoubleClick事件中的e.handled = true设置为父控件 - Setting e.handled = true in the MouseDoubleClick event in UserControl bubbles to Parent Control 为什么在这种情况下 e.Handled = true 不会停止密钥处理? - Why does e.Handled = true not stop key processing in this case? Maskedtextbox忽略e.Handled = true /如何防止输入 - Maskedtextbox ignores e.Handled = true /How to prevent input 自定义WF4活动:为什么DoubleClick中的e.Handled = true不会停止冒泡 - Custom WF4 Activity: Why e.Handled = true in DoubleClick don't stops bubbling 如何在不使用e.Handled = true的情况下防止两次调用Closing事件? - How to prevent the twice-call of Closing event without using e.Handled=true? 异步以延迟在KeyDown事件中处理 - async to delay e.handled in a KeyDown event E.Handled在ShowDialog中无法通过TouchEventArgs工作 - E.Handled Not Work From TouchEventArgs in ShowDialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM