Counter control loop

alex_ea_2a

Member
Joined
Mar 2, 2006
Messages
8
Programming Experience
Beginner
I need some help with a simple problem. I created a project with a label control "0" and a button control. I Added instructions to a button click handler, so that every time the button is pressed the number in the control increases by 1.

Basicalley what I need is a counter that gives me the number of times that the button is cliked.

Here is the code:

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click

Dim sum As Integer = 0
Dim count As Integer = 1

For count = 1 To 1 Step 1
sum = sum + count
Next
lblScore.Text = Convert.ToString(sum)
Return
End Sub
 
Sooooo simple:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] [COLOR=darkgreen]'by default it is 0 (zero)[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
i += 1
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label1.Text = i
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

Regards ;)

edit: is that what you want or you want to arise an event that will return how many times the buttom has been clicked at once? say user is clicking the certain button and when game stops then it shows the number of clicks ... is that what you want to acomplish or code above does the job?
 
kulrom said:
Sooooo simple:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] [COLOR=darkgreen]'by default it is 0 (zero)[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
i += 1
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label1.Text = i
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
just an fyi, if option strict is turned on, this will throw an error in which case simply modify the Me.Label1 line to this:
VB.NET:
Me.Label1.Text = i.ToString
 
kulrom said:
even if it's turned on VS.NET will not throw an error but vs.net intellisense will underline it with warning message that it is dissalowed to convert integer to string ;)
No big deal anyway (terminology matter) :)

you knew what i meant :p lol
 
Back
Top