Link to home
Start Free TrialLog in
Avatar of jazjef
jazjef

asked on

How do I click a button in VB.NET, and tell a second button to click at the same time the first button is clicked?

How do I click a button in VB.NET, and tell a second button to click at the same time the first button is clicked?

I have a button called 'A', and a button called 'B', and when I click on 'A' I want 'B' to click also... (or at least have the button 'B' code execute).

Seems simple... but then again, it's the simple things that are always the major headache in my life..... thanks.

Avatar of Harisha M G
Harisha M G
Flag of India image

Hi, Why don't you put the code of B along with A ??

or call

CommandB_Click() within CommandA_Click()


---
Harish
Avatar of MilanKM
MilanKM

Just call ButtonB_Click on the MouseDown event of  ButtonA. I'll suggest MouseDown event because if u do it on Click event, the operation will occour after a fraction of second (miliseconds)

Private Sub ButtonA_MouseDown(sender As Object, e As System.WinForms.MouseEventArgs)
ButtonB_Click
End Sub

Thanks
MilanKM
ASKER CERTIFIED SOLUTION
Avatar of pweegar
pweegar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Way not just put the code from buttons a and b into there own subs and then call the subs from the Button_Click
event handler






The CORRECT way...

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button2.PerformClick()
    End Sub
Avatar of jazjef

ASKER

Thanks to everyone who posted. There were many responses and it was great to see that kind of enthusiasm in solving this... To all who responded, once again, many thanks.

Jeff
Consider this code:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler Button2.Click, AddressOf Me.Button2_Reloaded
            AddHandler Button2.Click, AddressOf Me.Button2_Revolutions
        End Sub

        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Button2.PerformClick()
        End Sub
 
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            MsgBox("Button2_Click")
        End Sub

        Private Sub Button2_Reloaded(ByVal sender As System.Object, ByVal e As System.EventArgs)
            MsgBox("Button2_Reloaded")
        End Sub

        Private Sub Button2_Revolutions(ByVal sender As System.Object, ByVal e As System.EventArgs)
            MsgBox("Button2_Revolutions")
        End Sub

    End Class

When you click on Button2, you will get THREE different MsgBoxes.  When you click on Button1, the Button2.PerformClick() call is made, and then ALL subscribers to the Button2.Click() event will be notified and you will again get three different MsgBoxes.

With the code you accepted:

        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Button2_Click(Nothing, Nothing)
        End Sub

You would only get ONE MsgBox with "Button2_Click" and the other event handlers that have correctly subscribed to your Button2.Click() event would not get executed.

This is why Button2.PerformClick() is the CORRECT answer.

~IM
Avatar of jazjef

ASKER

Dear Idle_Mind:

Thank you for your comment. Yes, you are correct... a sledgehammer will drive thumb-tacks much better.

I was after simplicity... which is driven by ' the parsimony principle'... such as the following by Gregory Chaitin states:

"...as William Occam noted, given two theories that explain the data, the simpler theory is to be preferred (Occam's razor).... if a theory is the same size in bits as the data it explains, then it is worthless, because even the most random of data has a theory of that size. A useful theory is a compression of the data; comprehension is compression. You compress things into computer programs, into concise algorithmic descriptions. The simpler the theory, the better you understand something."

Chaitin, G. (2006). 'The Limits of Reason'. Scientific American, 294 (3).

I wanted to click button 'A', and upon doing so, if a certain criteria had been met, button 'B' would initiate its click event. Pweegar's solution arrived at what I wanted in the most parsimonious way. Sorry to have irked you, but 'less is more' in this case.


Lol...you haven't irked me at all.  I just wanted to clarify why I thought my was answer was more correct.  No more...no less.  No need for points.

I was going to give an explanation with my code when I made the post but unfortunately my wife wanted to use the puter so I had to step away...thus the delay in my posts.

=)

On a side note...regarding your 'less is more' comment:

pweegar:
Call btnB_Click(Nothing, Nothing)
33 characters

(without 'Call')
btnB_Click(Nothing, Nothing)
28 characters

Idle_Mind:
Button2.PerformClick()
22 characters

(using 'btnB')
btnB.PerformClick()
19 characters

Mine is definitely 'less'...hehe...     =)
Avatar of jazjef

ASKER

Idle_Mind:

I owe you an apology.

Your method does work, and it IS shorter... the fact that I used what I used to solve my problem was my choice, but when it comes down to it, when eliminating the 'Call' (which your method does) your method wins out.

I stand corrected.

However, you still don't have any of the points.... can those points be re-distributed 'after the fact' in some way?

The points can be redistributed but don't worry about it...I've got plenty (look at my profile).