Solved

A simple button program

Posted on 2011-03-15
48
635 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:Dier02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 31
  • 14
  • 2
  • +1
48 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35136535
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
 

Author Comment

by:Dier02
ID: 35136547
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35136634
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Dier02
ID: 35136644
0
 

Author Comment

by:Dier02
ID: 35136670
How would I add an image, a rollover image and an on click image to those buttons?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35136684
add your own buttons and onclick event dd the code taht I have gien
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35136697
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
 

Author Comment

by:Dier02
ID: 35136702
Pratima - see comment above.  Robert - how would I add those images to the buttons?
0
 

Author Comment

by:Dier02
ID: 35136719
Robert, I also can;t see how to change the names on each button to the names of 25 different people one per?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35136735
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35136765
ah, it _does_ work, but I forgot to put the image(s) in the same directory as the executable...
0
 

Author Comment

by:Dier02
ID: 35136766
I can't get that to work either
0
 

Author Comment

by:Dier02
ID: 35136774
0
 

Author Comment

by:Dier02
ID: 35136776
0
 

Author Comment

by:Dier02
ID: 35136782
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35136783
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
 

Author Comment

by:Dier02
ID: 35136787
There are the three images - image, on rollover, on click
0
 

Author Comment

by:Dier02
ID: 35136812
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
 

Author Comment

by:Dier02
ID: 35136819
How do I add the names to that array?
0
 

Author Comment

by:Dier02
ID: 35136821
btn.Text = contest("Jan1", "Dave", "Bill")??
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35136845
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
 

Author Comment

by:Dier02
ID: 35136866
0
 

Author Comment

by:Dier02
ID: 35136869
not sure what the issue is
0
 

Author Comment

by:Dier02
ID: 35136932
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
 

Author Comment

by:Dier02
ID: 35136936
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
 

Author Comment

by:Dier02
ID: 35137041
When you refer to the 'for loop' are you talking about the line below?

 For i As Integer = 1 To 25
0
 

Author Comment

by:Dier02
ID: 35137068
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35137099
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
 

Author Comment

by:Dier02
ID: 35137161
Great, thanks
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 35137233
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
 

Author Comment

by:Dier02
ID: 35137363
0
 

Author Comment

by:Dier02
ID: 35137367
coming up with an error
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35137378
are there 25 names in the array?
0
 

Author Comment

by:Dier02
ID: 35137380
yes
0
 

Author Comment

by:Dier02
ID: 35137396
Sorry, just had to save and reopen and it worked.  Thanks!
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35137397
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35137402
isn't visual studio great? I'm kind-a curious now what it is for by the way ;-)
0
 

Author Closing Comment

by:Dier02
ID: 35137463
Brilliant, thank you!
0
 

Author Comment

by:Dier02
ID: 35137480
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
 

Author Comment

by:Dier02
ID: 35137490
When you see a student doing the right thing you just tap on the button to record it.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35137493
yeah i can see that working. nice!
0
 

Author Comment

by:Dier02
ID: 35137586
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35137597
installing? on an Ipad you mean? one of the images was a bmp, maybe that has to be converted?
0
 

Author Comment

by:Dier02
ID: 35142071
no I can install it on my win 7 machine but it will not run.  It closes down everytime
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35145786
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
 

Author Comment

by:Dier02
ID: 35145925
If it were in the programs files directory it would show up in C: Program Files?  It doesn't.
0
 

Author Comment

by:Dier02
ID: 35145941
.net 4 client network in advanced compile options - should I change that to .NET 2?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 35146214
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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question