Solved

A simple button program

Posted on 2011-03-15
48
627 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
  • 31
  • 14
  • 2
  • +1
48 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Dier02
Comment Utility
0
 

Author Comment

by:Dier02
Comment Utility
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
Comment Utility
add your own buttons and onclick event dd the code taht I have gien
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
Comment Utility
Pratima - see comment above.  Robert - how would I add those images to the buttons?
0
 

Author Comment

by:Dier02
Comment Utility
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
Comment Utility
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
Comment Utility
ah, it _does_ work, but I forgot to put the image(s) in the same directory as the executable...
0
 

Author Comment

by:Dier02
Comment Utility
I can't get that to work either
0
 

Author Comment

by:Dier02
Comment Utility
0
 

Author Comment

by:Dier02
Comment Utility
0
 

Author Comment

by:Dier02
Comment Utility
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
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
Comment Utility
There are the three images - image, on rollover, on click
0
 

Author Comment

by:Dier02
Comment Utility
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
Comment Utility
How do I add the names to that array?
0
 

Author Comment

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

Expert Comment

by:Robert Schutt
Comment Utility
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
Comment Utility
0
 

Author Comment

by:Dier02
Comment Utility
not sure what the issue is
0
 

Author Comment

by:Dier02
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Dier02
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great, thanks
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
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
Comment Utility
0
 

Author Comment

by:Dier02
Comment Utility
coming up with an error
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
are there 25 names in the array?
0
 

Author Comment

by:Dier02
Comment Utility
yes
0
 

Author Comment

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

Expert Comment

by:Robert Schutt
Comment Utility
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
Comment Utility
isn't visual studio great? I'm kind-a curious now what it is for by the way ;-)
0
 

Author Closing Comment

by:Dier02
Comment Utility
Brilliant, thank you!
0
 

Author Comment

by:Dier02
Comment Utility
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
Comment Utility
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
Comment Utility
yeah i can see that working. nice!
0
 

Author Comment

by:Dier02
Comment Utility
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
Comment Utility
installing? on an Ipad you mean? one of the images was a bmp, maybe that has to be converted?
0
 

Author Comment

by:Dier02
Comment Utility
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
Comment Utility
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
Comment Utility
If it were in the programs files directory it would show up in C: Program Files?  It doesn't.
0
 

Author Comment

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

Expert Comment

by:Robert Schutt
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
If not exist vbs error 4 26
Vb.Net Menu 1 21
Crystal Report for VS2013 17 36
Expando 4 33
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now