ProblemYou need to move the current focus to a specific control, but you want to avoid conditions where the focus-setting action would fail. SolutionUse the control's CanFocus() method to determine whether the application can take the focus or not: If (SomeControl.CanFocus() = True) Then _ SomeControl.Focus() DiscussionEvent-driven programming can lead to many runtime surprises based on timing. Depending on how you write your code, it's possible that an event handler will be temporarily interrupted so that another event handler can run instead. Or, more commonly, unrelated event handlers may fire in an order you did not anticipate because of some interesting input action by the user. If you have an event handler that disables and enables a specific control, and another handler that sets the focus to that control, some situations may arise in which the focus action faisl because the control is disabled. While you could check the Enabled flag before setting the focus, there are other conditions (such as the presence of a separate modal dialog) that can also stop a control from receiving the focus, even when the Enabled flag is true. Using the CanFocus() method provides a more accurate method of determining when it is safe to call the Focus() method. |