如何找出 .NET Windows 窗体中哪个控件具有焦点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/660173/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:21:38  来源:igfitidea点击:

How do I find out which control has focus in .NET Windows Forms?

.netvb.netwinformscontrolsfocus

提问by Jeff

How do I find out which control has focus in Windows Forms?

如何找出Windows 窗体中哪个控件具有焦点?

回答by Ken Browning

Form.ActiveControlmay be what you want.

Form.ActiveControl可能是你想要的。

回答by MarkJ

Note that a single call to ActiveControl is not enough when hierarchies are used. Imagine:

请注意,使用层次结构时,对 ActiveControl 的单个调用是不够的。想象:

Form
    TableLayoutPanel
        FlowLayoutPanel
            TextBox (focused)

(formInstance).ActiveControlwill return reference to TableLayoutPanel, not the TextBox

(formInstance).ActiveControl将返回对 的引用TableLayoutPanel,而不是TextBox

So use this (full disclosure: adapted from this C# answer)

所以使用这个(完全披露:改编自这个 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

回答by user2721444

You can use the ActiveControl propert of the form and can use that control.

您可以使用窗体的 ActiveControl 属性并可以使用该控件。

me.ActiveControl

Or

或者

Form.ActiveControl

回答by xDJR1875

In C# I do this:

在 C# 中,我这样做:

        if (txtModelPN != this.ActiveControl)
            txtModelPN.BackColor = Color.White;

txtModelPN is a textbox that I am highlighting on enter and mouseEnter and de-highlighting on Leave,MouseLeave. Except if it is the current control I don't set the background back to white.

txtModelPN 是一个文本框,我在 enter 和 mouseEnter 上突出显示并在 Leave,MouseLeave 上取消突出显示。除非它是当前控件,否则我不会将背景设置回白色。

The VB equivalent would be like this

VB 等价物是这样的

IF txtModelPN <> Me.ActiveControl Then
   txtModelPN.BackColor = Color.White
End If

回答by KHALID

You can use this to find by Control Name .

您可以使用它来按 Control Name 查找。

    If DataGridView1.Name = Me.ActiveControl.Name Then
        TextBox1.Visible = True
    Else
        TextBox1.Visible = False
    End If

回答by user9553193

I used following:

我使用了以下内容:

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

I set the Variable only on MouseEnter to improve the magic

我只在 MouseEnter 上设置变量以提高魔法

回答by Stephen Wrighton

Something along these lines:

沿着这些路线的东西:

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

I think this code came from windowsclient.net, but it's been a while so...

我认为这段代码来自 windowsclient.net,但已经有一段时间了...