Execute code from other objects/actions

Sthafrican

New member
Joined
May 25, 2005
Messages
4
Programming Experience
Beginner
Hi..i am curious to know and will help me out simplifying my code:)

Is it possible to execute the code of another object/action(ex:button1_click) when performing the code of current object/action(ex:button2_click).

In other words

button1_click(....)
label1.enable=true : label2.enable=true : label3.enable=true
End sub

button2_click(....)
textbox1.clear()
'this part is the same code as the button1_click
label1.enable=true : label2.enable=true : label3.enable=true
End sub
...............................

A function would work..but its too much work

thanks
 
Last edited:
execute code

SthAfrican,

Yes you can do it. try dropping two buttons on a form and have one start the other and using a msgbox to test it.


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

--run some code here

Button2_Click

-returns to code here
End Sub

Button2_Click()
MSGBOX "Hi Im Button 2
end sub

 
Some alternatives:
You can have two procedures that handle the same event:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
  Handles Button1.Click, Button2.Click
    Label1.Enabled = True : Label2.Enabled = True : Label3.Enabled = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button2.Click
    TextBox1.Clear()
End Sub
You can also use the PerformClick procedure of a Button to run that buttons code:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
  Handles Button1.Click
    Label1.Enabled = True : Label2.Enabled = True : Label3.Enabled = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button2.Click
    TextBox1.Clear()
    Button1.PerformClick()
End Sub
 
Back
Top