C# 如何确定表单上的哪个控件具有焦点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/512296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine which control on form has focus?
提问by G-.
I've read elsewhere on here that to capture "Enter" key stroke in a text box and use it as if pushing a button I should set the KeyPreview property of the form to true and check the value of KeyDown.
我在这里的其他地方读过,要在文本框中捕获“Enter”键击并将其用作按下按钮,我应该将表单的 KeyPreview 属性设置为 true 并检查 KeyDown 的值。
I want to be able to use this functionality on several TextBox controls which each are associated with a different Button.
我希望能够在多个 TextBox 控件上使用此功能,每个控件都与不同的 Button 相关联。
My question is how do I know which control caused the KeyPress event? The sender is listed as the form itself.
我的问题是我怎么知道哪个控件导致了 KeyPress 事件?发件人被列为表单本身。
G
G
采纳答案by G-.
I've found a solution which appears to be working.
我找到了一个似乎有效的解决方案。
private void DeviceForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13 && tstxtDeviceFilter.Focused)
{
filterByDeviceSN();
}
}
I can't help but think there must be a better way though!
我不禁认为一定有更好的方法!
--EDIT--EDIT--EDIT--EDIT--EDIT--
--编辑--编辑--编辑--编辑--编辑--
Well, after looking at the suggestions below (thank you) I've found a 'better' way for me in this circumstance.
好吧,在看了下面的建议(谢谢)之后,我在这种情况下为我找到了一种“更好”的方法。
this.tstxtDeviceFilter.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tstxtDeviceFilter_KeyDown);
private void tstxtDeviceFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
filterByDeviceSN();
}
}
Obviously by trapping the event on the textbox itself rather than the form I don't need to worry about focus. Once again I feel dumb for not thinking of that for so long!
显然,通过将事件捕获在文本框本身而不是表单上,我不需要担心焦点。这么久没有想到这个,我又一次感到愚蠢!
回答by Binary Worrier
Each form has a property for an "Accept" button & "Cancel" button, these are the buttons that get "clicked" when the user presses enter and escape respectively.
每个表单都有一个“接受”按钮和“取消”按钮的属性,当用户分别按下 Enter 和 Esc 键时,这些按钮会被“点击”。
You can change the default button as each control gets the focus (you can have one got focus event hander per button, and share it with a set of text boxes)
您可以在每个控件获得焦点时更改默认按钮(您可以为每个按钮设置一个获得焦点事件处理程序,并与一组文本框共享)
If you do this then the apperance of the buttons change giving the user a visual cue telling them which button is the default.
如果你这样做,按钮的外观就会改变,给用户一个视觉提示,告诉他们哪个按钮是默认的。
Alternatively, if you don't want to do that, you can use the "ActiveControl" property, and test to see which of the sets of text boxes it belongs to.
或者,如果您不想这样做,您可以使用“ActiveControl”属性,并测试它属于哪一组文本框。
Have you asked yourself, what should the default button be if it's not one of thse text boxes?
你有没有问过自己,如果默认按钮不是这些文本框之一,它应该是什么?
回答by Mike Marshall
Have you tried Form.ActiveControl?
你试过Form.ActiveControl吗?
回答by HardCode
You could use this code as a starting point to capture the key down events of the form. The ActiveControl is one that has the focus. In this example, it's flexible for adding other actions on "Enter" when you are in different TextBoxes on the form. It's VB.NET, but you should be able to easily convert to C#.
您可以使用此代码作为起点来捕获表单的按键按下事件。ActiveControl 是一个具有焦点的控件。在此示例中,当您位于表单上的不同文本框时,可以灵活地在“Enter”上添加其他操作。它是 VB.NET,但您应该能够轻松转换为 C#。
Private Sub MyForm_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
If Me.ActiveControl.Name = Me.TextBox1.Name Then
' This is the TextBox we want to be active to run filterByDeviceSN()
filterByDeviceSN()
ElseIf Me.ActiveControl.Name = Me.TextBox2.Name Then
foo()
End If
End If
End Sub