简体   繁体   English

Control.Select()和Control.Focus()之间有什么区别?

[英]What's the difference between Control.Select() and Control.Focus()?

In WinForms, to set focus to a specific control, I always seem to wind up calling Control.Select() and Control.Focus() to get it to work. 在WinForms中,为了将焦点设置为特定的控件,我似乎总是调用Control.Select() Control.Focus()来使其工作。

What is the difference, and is this the correct approach? 有什么区别,这是正确的方法吗?

Focus() is the low level function that actually sets the focus. Focus()是实际设置焦点的低级功能。

Select() is a higer-level method. Select()是一种高级方法。 It first looks iteratively upward in the control's parent hierarchy until it finds a container control. 它首先在控件的父层次结构中以迭代方式向上查找,直到找到容器控件。 Then it sets that container's ActiveControl property (to the called control). 然后它设置该容器的ActiveControl属性(到被调用的控件)。 The logic in those methods is not straightforward however, and there is special handling for UserControl containers. 但是,这些方法中的逻辑并不简单,并且对UserControl容器有特殊处理。

Focus is a low-level method intended primarily for custom control authors. Focus是一种低级方法,主要用于自定义控件作者。 Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms. 相反,应用程序员应该对子控件使用Select方法或ActiveControl属性,或者对表单使用Activate方法。

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx

For an example of how they are different, if you are trying to set a control for a Forms App to default focus to when you open it, only Select() will work when called in the constructor after InitializeComponent(). 有关它们如何不同的示例,如果您尝试将窗体应用程序的控件设置为默认焦点时打开它,则只有Select()在InitializeComponent()之后在构造函数中调用时才会起作用。 Focus() will not. 焦点()不会。

Just to add to this thread I found that when writing a user control that moved other controls from one form to another (newly created form). 只是为了添加到这个线程,我发现在编写一个用户控件时,将其他控件从一个窗体移动到另一个窗体(新创建的窗体)。 The original form could no longer select the control but using focus allowed it to do so. 原始表单无法再选择控件,但使用焦点允许它这样做。 I think this emphasises the answers about the levels these methods work at. 我认为这强调了这些方法工作水平的答案。 But it also means it is not simple enough to say use Select at the higher level since select no longer worked as expected on the orginal form (not that it should being I placed it into a different form - I accept that) 但这也意味着说使用选择在较高级别并不简单,因为选择不再按照预期在原始形式上工作(并不是因为它应该将它放入不同的形式 - 我接受)

Focus(), in some situations, can cause a window owning the control to gain focus if it didn't have focus. 在某些情况下,Focus()会导致拥有控件的窗口在没有焦点的情况下获得焦点。 Select() does not cause a focus grab by the window. Select()不会导致窗口聚焦。

From personal experience I wrote a user control inheriting the Windows ComboBox. 根据个人经验,我编写了一个继承Windows ComboBox的用户控件。 I had to write code to override the OnEnter event and I had a statement in there saying 我不得不编写代码来覆盖OnEnter事件,我在那里有一个声明说

If Me.Focused Then ... Else ...

However, unfortunately it returned the unexpected result. 然而,不幸的是它返回了意想不到的结果。 If I called MyCustomerComboControl.Select (in either Load, Shown or Activated events) it called the OnEnter method but failed to register it had the focus (ie Focused was False) but if I called Focus it worked. 如果我调用了MyCustomerComboControl.Select (在Load,Shown或Activated事件中)它调用了OnEnter方法但是没有注册它有焦点(即Focused为False)但是如果我调用Focus它有效。 Furthermore Select worked if the form was open ie if I selected another control then re-selected the original control all was fine. 此外,如果表单打开,则Select有效,即如果我选择了另一个控件,则重新选择原始控件,一切都很好。 So in any other circumstances other than my scenario, use Select because it says so above. 因此,除了我的场景之外的任何其他情况,请使用Select因为它如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM