VB-> Button #1 do the same as #2

  • Thread starter Thread starter Hareth
  • Start date Start date
H

Hareth

I want to make button1_click do the same as button2_click WITHOUT wrtiting
the exact code all over....

I was thinking of something like (button2 = button1)....obviously that
doesnt work...so....can anyone help???
 
Hareth,

This one is simple, in sub from the button2_click event you can add at the
end
handles button1.click, button2.click

I hope this helps,

Cor
 
Reply: If you want to associate a different Event handler with the same
event use RemoveHandler Statement to disassociate the event with the current
event handler and use the AddHandler statement to associate it with a
different event handler. In my example I used a button control that was used
to start and stop a process. The bulk of the code was in the StopProcess
event:

TextBox1.Text = "Process Complete"
btnStopStart.Text = "Start"
RemoveHandler btnStopStart.Click.AddressOf StopProcess
AddHandler btnStopStart.Click, AddressOf StartProcess

The other code consisted of one line in the Form1 and the StartProcess events.
 
Why not just create a subroutine that is called by both button clicks ?

sub buttion1_click ()
dosomething ()
end sub

sub button2_click ()
dosomething ()
end sub

sub dosomething ()

end sub

HTH
Jerry
 
The "Handles" keyword lets you wire one or more events to a method. In the
code below, the Button_Click sub will be called when the user clicks Button1
or Button2.


Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click

End Sub

- Scott Swigart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top