Button Counter

teamdad

Member
Joined
Jun 14, 2004
Messages
12
Programming Experience
Beginner
Please bear with me on this, I have a cognative disability and am trying to write a program to help myself with doing everyday tasks. I'm doing a pretty good job so far but ran into some problems on this one part. I'm using Visual Basic 2008 Express Edition.

On my Form1 I have ten individual buttons named Button1, Button2 etc... what I need to do is count the times each button is clicked and then display the information in something like a mulit line textbox when another button called "Show" is pressed. The format should be something like below, button1 was pressed 5 times and button4 was pressed ten times.

Button1 x 5
Button4 x 10


Any help is greatly appreciated!!
 
String Help

I got my buttons to work with the code below but if say button1 isn't pushed and just button2 is pushed I don't want my messagebox to show the variable or it's text if the button hasn't been pushed. It should just show the information for the buttons that have been pushed. How can I do this with my code below?

VB.NET:
Public Class Form1
    Dim intValue As Integer
    Dim intValue2 As Integer
    Dim test As String

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

   intValue += 1

End Sub

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

    intValue2 += 1

End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    test = (intValue & " x $1" & vbCrLf & intValue2 & " x $5")
    MessageBox.Show(test, "Button Count", MessageBoxButtons.OK, MessageBoxIcon.Information)


End Sub


End Class
 
Learn about dictionaries and collections. Here is one example:
VB.NET:
Private clicks As New Dictionary(Of Button, Integer)

Private Sub buttons_click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click 'etc
    Dim b As Button = DirectCast(sender, Button)
    If clicks.ContainsKey(b) Then
        clicks(b) += 1
    Else
        clicks.Add(b, 1)
    End If
End Sub

Private Sub ShowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowButton.Click
    Dim sb As New System.Text.StringBuilder
    For Each entry As KeyValuePair(Of Button, Integer) In clicks
        sb.AppendFormat("Button {0} was clicked {1} times.", entry.Key.Name, entry.Value)
        sb.AppendLine()
    Next
    MessageBox.Show(sb.ToString)
End Sub
 
Thank you so much!! my simple 2 year project is almost done and your suggestion works great but I have one little thing left. Your code uses the name of the button "button1, button2 etc..." but I need it to use the buttons text or make each buttons name a variable or something.

Let me explain, my project is a money calculator that uses monitary denominations instead of regular numbers. Example $1, $5, $10 instead of 1, 2, 3 buttons. I can't rename my buttons from Button1 to $1 or Button2 to $5 I get "Property value not valid" errors and the code you showed me displays "Button1 was pressed x times" and I need it to say "$1 was pressed x times" instead if it's possible.


Everyone has been a great help and I really appreciate it
 
entry.Key is the Button so entry.Key.Name is using the Name property of the button, you can use Text property instead.
 
Great job!! works like a charm but I noticed when I was debugging my program that the string that is built doesn't reset or clear itself. If I push button1 one time it counts the number as 1 press but when I clear the results to do another calculation and press button1 again it counts the number of presses as 2 instead of just one. My question is how do I clear the value of the string to start over when I clear my results?
 
String?? The data is stored in the dictionary. It has a Clear method that "Removes all keys and values from the Dictionary".
 
Back
Top