Link to home
Start Free TrialLog in
Avatar of Dier02
Dier02Flag for Australia

asked on

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.
Avatar of Pratima
Pratima
Flag of India image

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
Avatar of Dier02

ASKER

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
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 
Avatar of Dier02

ASKER

How would I add an image, a rollover image and an on click image to those buttons?
add your own buttons and onclick event dd the code taht I have gien
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

Avatar of Dier02

ASKER

Pratima - see comment above.  Robert - how would I add those images to the buttons?
Avatar of Dier02

ASKER

Robert, I also can;t see how to change the names on each button to the names of 25 different people one per?
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
ah, it _does_ work, but I forgot to put the image(s) in the same directory as the executable...
Avatar of Dier02

ASKER

I can't get that to work either
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
Avatar of Dier02

ASKER

There are the three images - image, on rollover, on click
Avatar of Dier02

ASKER

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
Avatar of Dier02

ASKER

How do I add the names to that array?
Avatar of Dier02

ASKER

btn.Text = contest("Jan1", "Dave", "Bill")??
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.
Avatar of Dier02

ASKER

not sure what the issue is
Avatar of Dier02

ASKER

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.
Avatar of Dier02

ASKER

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.
Avatar of Dier02

ASKER

When you refer to the 'for loop' are you talking about the line below?

 For i As Integer = 1 To 25
Avatar of Dier02

ASKER

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?
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.
Avatar of Dier02

ASKER

Great, thanks
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

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
Avatar of Dier02

ASKER

coming up with an error
are there 25 names in the array?
Avatar of Dier02

ASKER

yes
Avatar of Dier02

ASKER

Sorry, just had to save and reopen and it worked.  Thanks!
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?
isn't visual studio great? I'm kind-a curious now what it is for by the way ;-)
Avatar of Dier02

ASKER

Brilliant, thank you!
Avatar of Dier02

ASKER

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.
Avatar of Dier02

ASKER

When you see a student doing the right thing you just tap on the button to record it.
yeah i can see that working. nice!
Avatar of Dier02

ASKER

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

installing? on an Ipad you mean? one of the images was a bmp, maybe that has to be converted?
Avatar of Dier02

ASKER

no I can install it on my win 7 machine but it will not run.  It closes down everytime
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?
Avatar of Dier02

ASKER

If it were in the programs files directory it would show up in C: Program Files?  It doesn't.
Avatar of Dier02

ASKER

.net 4 client network in advanced compile options - should I change that to .NET 2?
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?