1,105,290 Community Members

How can I call or track the Button click events

Member Avatar
Mr.M
Junior Poster
197 posts since Dec 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 2 [?]
 
0
 

I want to call the button events. here is the similarity of what I want.

NB the following code is not working it just the sample to show what I want the code to look like.

if Button1.Clicked = True Then
' Do my thing
End If

So my problem is to call the Click handler. I don't want this please.

Private Sub Button1_Click(ByVal............)................Click

End Sub

Hope my Question is clear.

Member Avatar
Ancient Dragon
Achieved Level 70
27,583 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,031 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

button.click is not a property, it's an event handler which doesn't return anything. your program can call the event handler from anywhere, just pass it the two arguments.

Member Avatar
Mr.M
Junior Poster
197 posts since Dec 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 2 [?]
 
0
 

but how can I? I want to pass this if it happens so that i can call the actions when this has been happened.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
1
 

Perhaps use a checkbox with a button appearance instead?

Public Class Form1
   Private WithEvents chkButton As New CheckBox _
            With {.Appearance = Appearance.Button, _
                  .CheckState = CheckState.Unchecked, _
                  .Location = New Point(10, 10), _
                  .Size = New Size(150, 25), _
                  .TextAlign = ContentAlignment.MiddleCenter, _
                  .Text = "I'm really a checkbox", _
                  .Parent = Me}

   Private Sub chkButton_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkButton.CheckedChanged
      If chkButton.Checked Then
         MsgBox("I'm Depressed")
      Else
         MsgBox("I'm Up")
      End If
   End Sub
End Class
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

I think what you want is button1.PerformClick(); This will trigger the button click event from anywhere in your code. I'm pretty sure the click event handler subroutine is a must. If you need to record the status of the button click a simple boolean in the click handler routine can handle that.

Member Avatar
Mr.M
Junior Poster
197 posts since Dec 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 2 [?]
 
0
 

So tinstaafl how will I go about doing that do I have to call it as.

Private Sub Button1.PerformClick();

or Do I have to put it under the timer control or I have to change the

Private Sub Button1.Click()

to that line of code?

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

In your code where you want to fire the button click, simple enter this statement,Button1.PerformClick(), and the code in the Button1_Click event handler will execute.

Member Avatar
Mr.M
Junior Poster
197 posts since Dec 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 2 [?]
 
0
 

Well I just discovered that the Question that I'm asking here is not easy to understand without any example or more explanation this comes as a result that these all solutions are not what I meant and I discovered this on Friday when I was telling one of my friend about this question but if you will read very careful my very first code you will get the picture of what I meant. You all think I want to perform a click on a button but that is what I don't want because I double click the button then code it but what I want is to let say I will use a time and this timer will be waiting or checking that has the button pushed yet. here is the Example of what I want in code.

Private Sub Button1_Click()
Button1.Hide()
End Sub

Private Sub Timer1_Tick()
If Button1.Click = True and Button3.click = True Then
Button2.Show
Else
If Button1.click = true and Button4.click = true Then
Button5.show
End If
End If
End Sub

I want something like this which will watch the clicking of a button. Hope The question is clear and thank you for you help hope you will be able to help me on this.

Member Avatar
Ancient Dragon
Achieved Level 70
27,583 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,031 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

buttons don't have a boolean property that works like that. You might have to create your own boolean variables that are toggled on and off each time you click the button. Something like this example

Public Class Form1
    Dim button1_clicked As Boolean = False
    Dim button2_clicked As Boolean = False

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If button1_clicked = False Then
            button1_clicked = True
            TextBox1.Text += "Button1 is on" & vbCrLf
        Else
            button1_clicked = False
            TextBox1.Text += "Button1 is off" & vbCrLf
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If button2_clicked = False Then
            button2_clicked = True
            TextBox1.Text += "Button2 is on" & vbCrLf
        Else
            button2_clicked = False
            TextBox1.Text += "Button2 is off" & vbCrLf
        End If

    End Sub
End Class
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

If you need to record the status of the button click a simple boolean in the click handler routine can handle that.

When you quote the part of the post you're referring it's easier for people to understand.

I think this will do what you want. But you might find it easier to use TnTinMN's suggestion of a checkbox that looks like a button. It has a boolean property that changes whenever it's clicked.

            Public Class Form1
                Dim ButtonClick() As Boolean = {False, False, False, False, False}

                Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.Click
                    ButtonClick(0) = Not ButtonClick(0)
                End Sub

                Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                    ButtonClick(1) = Not ButtonClick(1)
                End Sub

                Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
                    ButtonClick(2) = Not ButtonClick(2)
                End Sub 

                Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
                    ButtonClick(3) = Not ButtonClick(3)
                End Sub

                Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
                    ButtonClick(4) = Not ButtonClick(4)
                End Sub 

                Private Sub Timer1_Tick()
                    If ButtonClick(0) And ButtonClick(2) Then
                    Button3.Show
                    Else
                    If ButtonClick(0) And ButtonClick(3) Then
                    Button4.Show
                    End If
                    End If
                End Sub             
            End Class
Member Avatar
Gé48
Junior Poster in Training
76 posts since Apr 2010
Reputation Points: 1 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

This solved a simular problem for me is a different appraoch but it does what you want it to do

Public Class Form1
    Dim ButClicked As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button3.Click, Button4.Click
        Dim strName As String = ""
        Dim text As Button = sender
        strName = text.Name
        If strName = "Button1" Then
            Button1.Hide()
            ButClicked = ButClicked + 1
        ElseIf strName = "Button3" Then
            ButClicked = ButClicked + 3
        ElseIf strName = "Button4" Then
            ButClicked = ButClicked + 4
        End If
    End Sub

    Private Sub Timer1_Tick_1(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If ButClicked = 4 Then
            Button2.Show()
            ButClicked = 0
        ElseIf ButClicked = 5 Then
            Button5.Show()
            ButClicked = 0
        End If
    End Sub
End Class
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

If you use the Visible or the Enabled property you can do it without the timer.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button3.Click, Button4.Click
    Dim TempButton As Button = sender
    TempButton.Visible = False

    If Not Button1.Visible And Not Button3.Visible Then
        Button2.Visible = True
    ElseIf Not Button1.Visible And Not Button4.Visible Then
        Button5.Visible = True
    End If
End Sub
Question Answered as of 9 Months Ago by tinstaafl, Ancient Dragon, Gé48 and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article