A simple button program

I want to create a VB 2010 windows application that has a series of 25 buttons with peoples names on the buttons.  There is a 26th button with result on it.  I want to be able to click on the buttons and have a point alloted to that person each time I click their button.  When I press result it shows me the name of the person who has the most button presses.  I have another button named "clear" which resets the system back to zero so I can begin again.
Dier02Asked:
Who is Participating?
 
Robert SchuttConnect With a Mentor Software EngineerCommented:
okay, here it is: http://schutt.nl/ee/ButtonApp2.zip

probably it would be better to use the names from the xml as well and get rid of the string array now...
0
 
Pratima PharandeCommented:
I have created simple program for 2 buttons
and Result and Cleare buttons , cb1 and cb2 are button presees
try this

see the code behind code

Public Class Form1
    Inherits System.Windows.Forms.Form


    Dim cb1 As Integer = 0
    Dim cb2 As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cb1 = cb1 + 1
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        cb2 = cb2 + 1
    End Sub


    Private Sub Result_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Result.Click
        MessageBox.Show("Button 1 pressed by " & cb1)
        MessageBox.Show("Button 2 pressed by " & cb2)
    End Sub

    Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
        cb1 = 0
        cb2 = 0
    End Sub
End Class
0
 
Dier02Author Commented:
Getting an error message:

Error      2      Handles clause requires a WithEvents variable defined in the containing type or one of its base types.      C:\Users\User\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      12      99      WindowsApplication1
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Robert SchuttSoftware EngineerCommented:
I made a little app that uses dynamically generated buttons, just the part about the 'winner' you would have to add. EE won't let me attach the zip file so I've put it here: http://schutt.nl/ee/ButtonApp.zip 
0
 
Dier02Author Commented:
0
 
Dier02Author Commented:
How would I add an image, a rollover image and an on click image to those buttons?
0
 
Pratima PharandeCommented:
add your own buttons and onclick event dd the code taht I have gien
0
 
DhaestCommented:
Something more dynamicly (done through LINQ)
Just make sure that all the buttons (except clear and result button) handle the same click-event
Public clickList As List(Of Clicks)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clickList = New List(Of Clicks)
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        Dim clickedButton As Button = sender
        Dim click As Clicks = clickList.Find(Function(x) x.mName = clickedButton.Name)
        If click Is Nothing Then
            clickList.Add(New Clicks(clickedButton.Name))
        Else
            click.addClick()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If clickList.Count > 0 Then
            Dim n As Integer = clickList.Max(Function(p As Clicks) p.mClicks)
            Dim x As Clicks = clickList.Find(Function(p) p.mClicks = n)
            MessageBox.Show(x.mName & " has been clicked " & x.mClicks & " times")
        Else
            MessageBox.Show("No button has been clicked on")
        End If
    End Sub


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        clickList = New List(Of Clicks)
    End Sub
End Class

Public Class Clicks
    Public mName As String
    Public mClicks As Integer

    Public Sub New(ByVal Name As String)
        'constructor
        mName = Name
        mClicks = 1
        'storing the value of x in constructor
    End Sub

    Public Sub addClick()
        mclicks += 1
    End Sub

End Class

Open in new window

0
 
Dier02Author Commented:
Pratima - see comment above.  Robert - how would I add those images to the buttons?
0
 
Dier02Author Commented:
Robert, I also can;t see how to change the names on each button to the names of 25 different people one per?
0
 
Robert SchuttSoftware EngineerCommented:
you should be able to do something like:

btn.Image = New Bitmap("contestant" & i & ".jpg")

in the for-loop but I haven't got it to work yet
0
 
Robert SchuttSoftware EngineerCommented:
ah, it _does_ work, but I forgot to put the image(s) in the same directory as the executable...
0
 
Dier02Author Commented:
I can't get that to work either
0
 
Dier02Author Commented:
0
 
Dier02Author Commented:
0
 
Dier02Author Commented:
0
 
Robert SchuttSoftware EngineerCommented:
For the name: put the 25 names in the string array 'contest' and use that instead of the number like:

btn.Text = contest(i - 1) ' instead of "Contestant" & i
0
 
Dier02Author Commented:
There are the three images - image, on rollover, on click
0
 
Dier02Author Commented:
Error      1      Number of indices exceeds the number of dimensions of the indexed array.      C:\Users\User\Downloads\ButtonApp\ButtonApp\ButtonApp\Form1.vb      12      31      ButtonApp
0
 
Dier02Author Commented:
How do I add the names to that array?
0
 
Dier02Author Commented:
btn.Text = contest("Jan1", "Dave", "Bill")??
0
 
Robert SchuttSoftware EngineerCommented:
no, where it says "1", "2", .... (first line in Form_Load) put the names in the array and the btn.Text = contest(i-1) uses those.

for mouseover in the for-loop add:
            AddHandler btn.MouseEnter, AddressOf btn_MouseEnter

and add this function:
    Private Sub btn_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = sender
        btn.Image = New Bitmap("eap2.jpg")
    End Sub


better would be to make a global Bitmap variable for each of the images in this situation.
0
 
Dier02Author Commented:
0
 
Dier02Author Commented:
not sure what the issue is
0
 
Dier02Author Commented:
One other thing - it needed to save the scores when closed so that the next day I can continue to click and it will continue to add to those scores until I click result and then clear.  The scores have to persist.
0
 
Dier02Author Commented:
I sorted out the problem above.  It now works the way I want (assuming the scores persist) but its just the images on the buttons thing now.
0
 
Dier02Author Commented:
When you refer to the 'for loop' are you talking about the line below?

 For i As Integer = 1 To 25
0
 
Dier02Author Commented:
I assume that in order for persistence to occur the program would have to be linked to a database it can read from and write to?
0
 
Robert SchuttSoftware EngineerCommented:
persistance could be done in an xml file, i'll include that and the images in a new zip file i'll post in a few minutes.
0
 
Dier02Author Commented:
Great, thanks
0
 
Dier02Author Commented:
0
 
Dier02Author Commented:
coming up with an error
0
 
Robert SchuttSoftware EngineerCommented:
are there 25 names in the array?
0
 
Dier02Author Commented:
yes
0
 
Dier02Author Commented:
Sorry, just had to save and reopen and it worked.  Thanks!
0
 
Robert SchuttSoftware EngineerCommented:
if you hover over the variable "i" it should tell you where it is in the loop, could there be something wrong with the name at that place in the array?
0
 
Robert SchuttSoftware EngineerCommented:
isn't visual studio great? I'm kind-a curious now what it is for by the way ;-)
0
 
Dier02Author Commented:
Brilliant, thank you!
0
 
Dier02Author Commented:
A classroom - I was thinking about a way of keeping tabs on student behaviour and awarding the winner some prize at the end of the week and using this to keep tabs.  It would make a very useful Ipad 2 app.
0
 
Dier02Author Commented:
When you see a student doing the right thing you just tap on the button to record it.
0
 
Robert SchuttSoftware EngineerCommented:
yeah i can see that working. nice!
0
 
Dier02Author Commented:
Problem installing


Problem signature:
  Problem Event Name:      CLR20r3
  Problem Signature 01:      buttonapp.exe
  Problem Signature 02:      1.0.0.0
  Problem Signature 03:      4d7f62b5
  Problem Signature 04:      System.Drawing
  Problem Signature 05:      4.0.0.0
  Problem Signature 06:      4ba1e086
  Problem Signature 07:      6c
  Problem Signature 08:      26
  Problem Signature 09:      System.InvalidOperationException
  OS Version:      6.1.7600.2.0.0.256.48
  Locale ID:      3081
  Additional Information 1:      0a9e
  Additional Information 2:      0a9e372d3b4ad19135b953a78882e789
  Additional Information 3:      0a9e
  Additional Information 4:      0a9e372d3b4ad19135b953a78882e789

0
 
Robert SchuttSoftware EngineerCommented:
installing? on an Ipad you mean? one of the images was a bmp, maybe that has to be converted?
0
 
Dier02Author Commented:
no I can install it on my win 7 machine but it will not run.  It closes down everytime
0
 
Robert SchuttSoftware EngineerCommented:
hmm could be a lot of things but I'm assuming on a win7 machine the .NET (2.0) framework is always installed. And you copied all the files over, the images and the results.xml? You didn't put it on a network share or in the program files directory where user permissions could be an issue?
0
 
Dier02Author Commented:
If it were in the programs files directory it would show up in C: Program Files?  It doesn't.
0
 
Dier02Author Commented:
.net 4 client network in advanced compile options - should I change that to .NET 2?
0
 
Robert SchuttSoftware EngineerCommented:
I guess no harm in trying, if the newest framework is installed on your win7 machine (not sure if that's in the automatic updates?) it shouldn't matter though. But if it's not on there it would explain it, the error you got seems to indicate missing dll's or other files, that's why I asked if you copied over all files from the bin direcotry, or have you made an installer for it?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.