Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically Call a Method Usually Triggered by an Event 2

Status
Not open for further replies.

Headwinds

Programmer
Feb 6, 2001
38
0
0
US
Suppose I have a Windows Form containing a button SetValues, and I have written code for the SetValues.Click method that assigns values to a number of variables. However, I would like the SetValues.Click code to execute when a user performs some other action as well--say, when she clicks in TextBox1. Is there any way to "call" the SetValues.Click--that is, to raise the SetValues Click event--programmatically? Or must I create a user-defined method containing the code that I want to execute, and call that method both from the SetValues.Click and TextBox1.Click?
 
I usually put the code into a separate SUB that can be executed from both places but if you really need to simulate a Click then try
SetValues.PerformClick
in your textbox event.
It will work only if the button lah Enabled=True. I do not know if it must also have Visble=True.
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks, John! I just wish there were a few more "Perform" possibilities. I'm coming to VB.NET from Visual FoxPro, in which one can call any method from any other method.
 
Well, you can check out the ONxxx methods provided for some classes. There is an OnClick for a button but you must pass a System.eventargs arugment. I have not had need to use these ONxxx methods so I can not comment on the necessary setup before calling them. It seems they are present for Inheritors. Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
You can call the even procedure, you just have to supply the sender and event arguments yourself. The sender is easy as it is the control whose event you are simulating; the event argumenents can be a little more tricky but with the IntlliSense provided by the IDE is not to difficult.

Code:
TrAttributes_BeforeExpand(TrAttributes, New TreeViewCancelEventArgs(DropNode, False, TreeViewAction.Expand))

TrAttibutes as a treeview control.
DropNode is the node to be expanded.

Regards
John
 
Or, to call the Button Click event:

Where cmdExit = Button

Call cmdExit_Click(cmdExit, New EventArgs())
 
Unless I am missing your intention, why not create your procedure and definr the events you want to handle.

private sub SetValues('your params depending on tthe type of event) Handles cmdSetValues.Click, text1.enter

 
You'd still have to pass the same/necessary eventargs to call that function/event through code but I agree it's nice to be able to combine multiple click events in to one sub.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top