Button_click

  • Thread starter Thread starter Rado
  • Start date Start date
R

Rado

HEllo.
In Event Button1_click i have some code.
And I would like to call this event from other part of code.

For example

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click

call button1_click - BUT this is bad, how can I call It?

End Sub





Thank YOu.
 
In my opinion it is better to put the code that is in your 'Button1_Click'
event in it's own procedure and then call this procedure from the
'Button1_Click' event and also the 'MenuItem2_Click' event...


Private Sub MyProcedure()
'Do some processing
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MyProcedure()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
MyProcedure()
End Sub


Hope this helps.

Gary
 
Rado,
Do both handles match "exactly" or is there a common subset?

I normally handle both events with the same handler, especially when the
method signatures match.

Otherwise I do as Gary suggests.

Something like:

Private Sub MenuItem2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem2.Click,
Button1.Click

End Sub

Remember in VB.NET you can have a single event handler handle multiple
events (as above) and you can have a single event handled by multiple
handlers...

Private Sub Stuff1(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
End Sub

Private Sub Stuff2(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem2.Click
End Sub


Private Sub Stuff3(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem2.Click,
Button1.Click

End Sub

In the above if you click Button1: Stuff1 & Stuff3 will execute, if you
click MenuItem2: Stuff2 & Stuff3 will execute.

In addition to the Handles keyword, you can use AddHandler & RemoveHandler
to handle an event.

Hope this helps
Jay
 
You can always use:

Button1.PreformClick

That will run the code in Button1's click event from any place you use it.
james
 
I think we might need a
Microsoft.Public.Dotnet.Languages.Vb.HowDoIPerformAButtonClick
newsgroup?

;)

MyButton.PerformClick()

hth

Richard
 
heres another way for buttons to control other buttons.
if you click button2 it clicks button1 and button2
button1 only clicks button1. I allready tested it.

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

MsgBox("Button1 Click")

End Sub

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

End Sub
End Class
 
hello,

call MenuItem2_Click(sender,e)

Thanks,


Warm Regards,

Ayaz Ahmed
Software Engineer & Web Developer
Creative Chaos (Pvt.) Ltd.
"Managing Your Digital Risk"
http://www.csquareonline.com
Karachi, Pakistan
Mobile +92 300 2280950
Office +92 21 455 2414
 
* "Rado said:
In Event Button1_click i have some code.
And I would like to call this event from other part of code.

For example

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click

call button1_click - BUT this is bad, how can I call It?

Add the code to a separate procedure and then call this procedure from
the button's and the menuitem's 'Click' event handler.
 
Hi Gary,

When it is a program where even the name stays Button1, it will be in this
situation with me often button1_click(nothing, nothing)

When it is a real program and there are more events than

Private Sub DescribeWhatIwantToDo ((ByVal sender As System.Object, ByVal e
As
System.EventArgs)

In addition to Spamface, I am curious if Armin add also something

:-))

Cor
 
Cor Ligthert said:
Hi Gary,

When it is a program where even the name stays Button1, it will be in
this situation with me often button1_click(nothing, nothing)

When it is a real program and there are more events than

Private Sub DescribeWhatIwantToDo ((ByVal sender As System.Object,
ByVal e As
System.EventArgs)

In addition to Spamface, I am curious if Armin add also something

:-))

I'm confused. I see one Button-thread with 3 msgs (one from scs, two from
you). Then I see two other threads (one from you, one from scs), both about
button_click, both starting with "Re:", but none from "Gary".

Well, I agree concerning the missing "Handles" keyword. I don't agree in
manually calling an event handler because nothing has been clicked.
 
Armin,

It is real weird a lot of the message from 15-3

Than in my OE scs gives an answer at 3.11 today and it is on the position
from 15-4 with me.

I am also confused.

I think I will reload the newsgroup again after I have done some answers I
have to make.

Cor
 
Back
Top