Forms, Controls, and Other Useful Objects - 4.5 Determining If a Control Can Take the Focus (Page 5 of 5 )
Problem
You need to move the current focus to a specific control, but you want to avoid conditions where the focus-setting action would fail.
Solution Use the control’s CanFocus() method to determine whether the application can take the focus or not:
If (SomeControl.CanFocus() = True) Then_
SomeControl.Focus()
Discussion 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 theEnabledflag 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 isTrue. Using theCanFocus()method provides a more accurate method of determining when it is safe to call theFocus()method.
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Check it out today at your favorite bookstore. Buy this book now.
|
|