Recipe 4.5. Determining If a Control Can Take the Focus


You need to move the current focus to a specific control, but you want to avoid conditions where the focus-setting action would fail.


Use the control's CanFocus() method to determine whether the application can take the focus or not:

 If (SomeControl.CanFocus() = True) Then _    SomeControl.Focus() 


Event-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.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: