Trackbar 一直在偷走我的注意力
Trackbar keeps stealing my focus
已被问过几次,但我无法使用任何答案。
我的问题是,每次我想更改轨迹栏值时,即使我单击 window 的其他部分,也会保持焦点。当我想使用按键时,它们只在 trackbarbox 中起作用。
我尝试了什么?:
-我尝试将CausesValidation
/TabStop
/Topmost
设置为false
/true
-我尝试使用 MouseLeave
/FocusEnter
事件通过 this.Focus()
将焦点重新设置到我的表单上
-我试着把
protected override bool IsInputKey(Keys keyData)
{
return true;
}
and/or
protected override bool ShowWithoutActivation
{
get { return true; }
}
进入主代码
这里是程序的屏幕截图以了解我的问题:
It's german but that doesn't matter. I want to press Enter while I'm drawing the line but the trackbar keeps focused and blocks it
通常的做法是在设置KeyPreview = true
后覆盖OnKeyDown
事件:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
// your code here..
Text = "Testing: KeyCode" + e.KeyCode;
}
但您也可以使用 PreviewKeyDown
事件。确保将表单的 KeyPreview
属性 设置为 true,并向可能 steal/receive 聚焦的所有控件添加一个公共事件!
由于控件的 PreviewKeyDown
事件使用不同的参数,您需要将事件路由到表单的 KeyDown
事件:
private void CommonPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Form1_KeyDown(this, new KeyEventArgs(e.KeyCode));
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// your code here..
Text = "Testing: KeyCode" + e.KeyCode;
}
您可能想在代码中连接句柄:
void routeKeys(Control container)
{
foreach (Control ctl in container.Controls)
if (ctl.CanFocus) ctl.PreviewKeyDown += CommonPreviewKeyDown;
}
这样称呼它:
public Form1()
{
InitializeComponent();
routeKeys(this);
}
当然你可能想要添加过滤器来防止路由你的表单不会处理的键..
这两种技术之间的区别 是当您覆盖 Form.OnKeyDown
时,您将从任何地方接收按键事件;这将包括例如您的角色和编辑键都被路由到表单的文本框。
如果您不想这样,则需要向事件添加过滤器:
if (tb_notes.Focused) return;
if (tb_moreNotes.Focused) return;
if (rtb_edit.Focused) return;
第二种方式让您决定路由中应包含或排除哪些控件..:[=23=]
if (ctl.CanFocus && !(ctl is TextBox || ctl is RichTextBox))
ctl.PreviewKeyDown += CommonPreviewKeyDown;
已被问过几次,但我无法使用任何答案。 我的问题是,每次我想更改轨迹栏值时,即使我单击 window 的其他部分,也会保持焦点。当我想使用按键时,它们只在 trackbarbox 中起作用。
我尝试了什么?:
-我尝试将CausesValidation
/TabStop
/Topmost
设置为false
/true
-我尝试使用 MouseLeave
/FocusEnter
事件通过 this.Focus()
-我试着把
protected override bool IsInputKey(Keys keyData)
{
return true;
}
and/or
protected override bool ShowWithoutActivation
{
get { return true; }
}
进入主代码
这里是程序的屏幕截图以了解我的问题: It's german but that doesn't matter. I want to press Enter while I'm drawing the line but the trackbar keeps focused and blocks it
通常的做法是在设置KeyPreview = true
后覆盖OnKeyDown
事件:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
// your code here..
Text = "Testing: KeyCode" + e.KeyCode;
}
但您也可以使用 PreviewKeyDown
事件。确保将表单的 KeyPreview
属性 设置为 true,并向可能 steal/receive 聚焦的所有控件添加一个公共事件!
由于控件的 PreviewKeyDown
事件使用不同的参数,您需要将事件路由到表单的 KeyDown
事件:
private void CommonPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Form1_KeyDown(this, new KeyEventArgs(e.KeyCode));
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// your code here..
Text = "Testing: KeyCode" + e.KeyCode;
}
您可能想在代码中连接句柄:
void routeKeys(Control container)
{
foreach (Control ctl in container.Controls)
if (ctl.CanFocus) ctl.PreviewKeyDown += CommonPreviewKeyDown;
}
这样称呼它:
public Form1()
{
InitializeComponent();
routeKeys(this);
}
当然你可能想要添加过滤器来防止路由你的表单不会处理的键..
这两种技术之间的区别 是当您覆盖 Form.OnKeyDown
时,您将从任何地方接收按键事件;这将包括例如您的角色和编辑键都被路由到表单的文本框。
如果您不想这样,则需要向事件添加过滤器:
if (tb_notes.Focused) return;
if (tb_moreNotes.Focused) return;
if (rtb_edit.Focused) return;
第二种方式让您决定路由中应包含或排除哪些控件..:[=23=]
if (ctl.CanFocus && !(ctl is TextBox || ctl is RichTextBox))
ctl.PreviewKeyDown += CommonPreviewKeyDown;