Basics of the Windows Forms Library (Page 1 of 4 )
In this second article of a four-part series covering .NET's Windows Forms library, you will learn how to programmatically click a button, draw a control, and more. 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). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
4.6 Programmatically Clicking a Button
Problem
You want the Click event handler for a button to run, but you want to initiate this action from code instead of waiting for the user to click the button.
Solution
Call the button’s PerformClick() method:
Button1.PerformClick()
Discussion
While it’s nice that the Button control has aPerformClick()method to run itsClickevent handler in an object-oriented manner, most controls and most control events have no such related method. If you wish to call an event handler immediately through code, you have to call it like any other method, passing the correct arguments:
' ---- Call the text box control's GotFocus handler.
TextBox1_GotFocus(TextBox1, New System.EventArgs)
In this case, calling theTextBox1control’sGotFocus()event handler will run that handler’s code, but it will not cause the focus to move to the text box. An even better solution would be to write a shared routine that theGotFocus()event handler and your other code both call.
Next: 4.7 Drawing a Control >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
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.
|
|