[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2121
  • Last Modified:

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.

0
jazjef
Asked:
jazjef
1 Solution
 
Harisha M GCommented:
Hi, Why don't you put the code of B along with A ??

or call

CommandB_Click() within CommandA_Click()


---
Harish
0
 
MilanKMCommented:
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
0
 
pweegarCommented:
You click a button in code like this:

1) Make sure your button to be clicked has code for the click event
2) Then: Call btnB_Click(Nothing, Nothing)

This will click your B button.  To test it, I would put a message in the B button's click method so you know it's being clicked.  (Or whatever code you want/need to execute).
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JBAKER_5500Commented:
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






0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
jazjefAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
jazjefAuthor Commented:
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.


0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...     =)
0
 
jazjefAuthor Commented:
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?

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The points can be redistributed but don't worry about it...I've got plenty (look at my profile).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now