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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a command button click event? 1

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
I'm confused...

In VB6 it was a simple matter to call the proc under a command button, just the control name and _Click (i.e., cmd1_Click).

I would like to do this in a VB.NET 2005 project I'm working on -- but the IDE won't let me. This is no olnger allowed? How do I get around it? I suppose I could create a separate proc with the code and call it from as many locations as I'd like, but I still like the old way -- What should I do?

Craig


CraigHartz
 
Ah, thank you. That was all? Couldn't find that in any of the three books I own. Go figure.



CraigHartz
 
It was tough to track down for me the first time I needed it too. Glad I could help.
 
Another way to do it:
Code:
Button1_Click(Button1, New System.EventArgs)

'or simply

Button1_Click(Nothing, Nothing)

This way, the procedure will trigger even if Button1 is Not Enabled or Not Visible.
 
The preferred way in .Net seems to be to add the control and event to the Handles clause of the event that you wish to use, ie, if you wish to have the Button2.Click, and the txtBox1.TextChanged events utilize the button1 click:

Private Sub button1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click (ADD THIS ->) , Button2.Click, txtBox1.TextChanged

I looked for something like the PerformAction, and hadn't found it either. It would be easier (it seems to me) to do it that way.
 
The prefered method in OO programming is to remove the logic from the GUI. So you would have:

Code:
private sub btnDoThing_click(sender as object, e as eventvariables) handles btnDoThing.Click
  DoThing
end sub

private sub DoThing
  'logic here
end sub

Then any time you need to execute the logic, you call DoThing instead of messing with the button. That way, if you ever need to replace the GUI (say switching to a Web interface, or from ASP to ASP.Net or to AJAX, or what ever...) you won't need to recode your logic.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Yeah... That sounds like good advice. I supposed that's one reason the made it harder to reference the object (or at least didn't make it easy...)

Thanks, I'll think about changing it. You've given me something to think about.

CraigHartz


CraigHartz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top