如何找出在Windows 窗体中具有焦点的控件?
Jeff
问问题
67096 次
7 回答
31
Form.ActiveControl
可能是你想要的。
于 2009-03-18T21:26:05.293 回答
13
请注意,在使用层次结构时,一次调用 ActiveControl 是不够的。想象:
Form
TableLayoutPanel
FlowLayoutPanel
TextBox (focused)
(formInstance).ActiveControl
将返回对 的引用TableLayoutPanel
,而不是TextBox
所以使用这个(完全披露:改编自这个 C# 答案)
Function FindFocussedControl(ByVal ctr As Control) As Control
Dim container As ContainerControl = TryCast(ctr, ContainerControl)
Do While (container IsNot Nothing)
ctr = container.ActiveControl
container = TryCast(ctr, ContainerControl)
Loop
Return ctr
End Function
于 2012-03-09T12:52:50.633 回答
3
在 C# 中,我这样做:
if (txtModelPN != this.ActiveControl)
txtModelPN.BackColor = Color.White;
txtModelPN 是一个文本框,我在 enter 和 mouseEnter 上突出显示,在 Leave、MouseLeave 上取消突出显示。除非它是当前控件,否则我不会将背景设置回白色。
VB等价物是这样的
IF txtModelPN <> Me.ActiveControl Then
txtModelPN.BackColor = Color.White
End If
于 2011-04-08T19:10:44.090 回答
3
您可以使用窗体的 ActiveControl 属性并可以使用该控件。
me.ActiveControl
或者
Form.ActiveControl
于 2013-08-27T11:40:30.877 回答
1
您可以使用它通过 Control Name 查找。
If DataGridView1.Name = Me.ActiveControl.Name Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
于 2013-01-21T20:25:56.430 回答
0
我使用了以下内容:
Private bFocus = False
Private Sub txtUrl_MouseEnter(sender As Object, e As EventArgs) Handles txtUrl.MouseEnter
If Me.ActiveControl.Name <> txtUrl.Name Then
bFocus = True
End If
End Sub
Private Sub txtUrl_MouseUp(sender As Object, e As MouseEventArgs) Handles txtUrl.MouseUp
If bFocus Then
bFocus = False
txtUrl.SelectAll()
End If
End Sub
我只在 MouseEnter 上设置变量以提高魔力
于 2018-08-09T19:16:50.913 回答
-1
这些方面的东西:
Protected Function GetFocusControl() As Control
Dim focusControl As Control = Nothing
' Use this to get the Focused Control:
Dim focusHandle As IntPtr = GetFocus()
If IntPtr.Zero.Equals(focusHandle) Then
focusControl = Control.FromHandle(focusHandle)
End If
' Note that it returns NOTHING if there is not a .NET control with focus
Return focusControl
End Function
我认为这段代码来自 windowsclient.net,但已经有一段时间了......
于 2009-03-18T21:36:43.473 回答