Buttons

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey guys ...

quick question ... i have two buttons (cmdButton1, cmdButton2) .... how do i use the click event in cmdButton1 from cmdButton2 ?

for example .. if you click cmdButton1 .. a massage box pops up saying "hello world" .. i wont to pop up this message box by click on cmdButton2 ..

I hope i make some sense !

NEED CODE PLEASE
THANX IN ADVANCE
 
Oh my...

You probably have the followinf line of code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Add to the end of it the following: ,Button2.Click

(please do not forget the comma)

Telmo Sampaio

Will said:
hey guys ...

quick question ... i have two buttons (cmdButton1, cmdButton2) .... how do
i use the click event in cmdButton1 from cmdButton2 ?
for example .. if you click cmdButton1 .. a massage box pops up saying
"hello world" .. i wont to pop up this message box by click on cmdButton2 ..
 
* "=?Utf-8?B?V2lsbA==?= said:
quick question ... i have two buttons (cmdButton1, cmdButton2) .... how do i use the click event in cmdButton1 from cmdButton2 ?

for example .. if you click cmdButton1 .. a massage box pops up saying "hello world" .. i wont to pop up this message box by click on cmdButton2 ..

I am not sure if I understand your question. Do you want to create a
'Click' event handler that is used by both buttons?

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

We understood you very clear. If you use the same procedure for handling
both events you can easily reproduce what you want. If you do not understand
the sender and e arguments you should do some research related to them and
to object oriented programming.

Here are some interesting links:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWindowsFormsControlClassClickTopic.asp

Telmo Sampaio

Will said:
i dont think you guys understood me ... what i mean is if i click
cmdButton1, a message pops up ("Hello World") ..
no if i try to call the me.cmdButton1_click event in cmdButton2,
me.cmdbutton1_click(Sender as Object,e as System.eventArgs) how do i use
Sender as Object and the e as System.eventArgs ? what do they mean ?
 
no if i try to call the me.cmdButton1_click event in cmdButton2

IMHO an event should fire *because it happened*. You should not be the one
calling cmdButton1_click. This is just bad conception...
 
Example: Use Sender
Private Sub LableLeaveEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblBuyerBroker.MouseLeave
Dim strLabname As String = System.Convert.ToString(sender.Name())
Select Case strLabname
Case "lblBuyerBroker"
lblBuyerBroker.Cursor = Cursors.Default
End Select
End sub

Example: Use e
Private Sub NumbValidation_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUnitQty.KeyPress
If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Or Char.IsPunctuation(e.KeyChar) Then
If e.KeyChar = Char.Parse("-") Or e.KeyChar = Char.Parse(".") Then
e.Handled = False
Else
e.Handled = True
End If
End If
End Sub
 
You can wire both click events to one procedure or not, but I would separate
out your logic from the button event itself. The benefit to this is that
you can call DoSomething from anywhere (not just these events).

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

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

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

Private Sub DoSomething()
msgbox("Hello World")
End Sub



HTH,
Greg

Will said:
i dont think you guys understood me ... what i mean is if i click
cmdButton1, a message pops up ("Hello World") ..
no if i try to call the me.cmdButton1_click event in cmdButton2,
me.cmdbutton1_click(Sender as Object,e as System.eventArgs) how do i use
Sender as Object and the e as System.eventArgs ? what do they mean ?
 
Will said:
i dont think you guys understood me ... what i mean is if i click
cmdButton1, a message pops up ("Hello World") ..
no if i try to call the me.cmdButton1_click event in cmdButton2,
me.cmdbutton1_click(Sender as Object,e as System.eventArgs) how do i use
Sender as Object and the e as System.eventArgs ? what do they mean ?
THANX IN ADVANCE GUYS

Are you taking the Intro to VB.NET course on About.com by any chance?
 
Back
Top