I need help with VB studio 5

bidnez

Member
Joined
Feb 11, 2007
Messages
5
Programming Experience
Beginner
Can anybody help me with this program there aresome simple task i need to complete.
 
The Help I need

I have a simple project to complete.

Create a form with with 5 controls and everytime some click a control is displays "you have just clicked the (Blank-----) control

Then create a counter to determine how many times each counter is clicked...

Can anyone help?
 
Thanks for re-sponding. I have all the control done and I am able to get the display message to appear but as far as a counter i don't know where to start.

For example I have a button for a new inquiry

me.inquirybutton.text = You have just clicked the inquiry control

Would I write a command for the counter in here under this sub?
How would a create a variable for the counter?
 
For the counter, you can either create a static variable if you only want to use it inside the sub treating your control event or a public variable that will be acessible by all your form objetcs.

Therefore you need 5 integer variables or an integer array.

By double clicking in each control you will have access to the event handler sub where you will increment the variable or the array element.

When you gain more experience, you will treat that with only one sub by adding the control names after 'Hanldes' and casting the sender object to a control, giving you the name of the control used. But now it's a lot easier to copy your code in each sub and only change the variable name and appropriate message.

There are a lot of examples available in the included Help, presse 'F1'.
 
Thanks but this is the issue. This is maybe my second time using this program and I am grasping some concepts. But the variable is where I am getting stuck because the way I am tring to determine it recieves and error.

This is what I have so far I just tried something to complete the task because honestly it has me beat. I created label1 to display the counter.

VB.NET:
PublicClass Form1
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
EndSub
PrivateSub pushButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pushButton1.Click
'Displays You have clicked the Push Control
Me.messageLabel.Text = "You have clicked the Push Control"
Me.Label1.Text = "0"
Me.Label1.Text = Me.Label1.Text + 1
EndSub
PrivateSub yesradiobutton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yesradiobutton.CheckedChanged
' Diplays you have clicked the Yes Control
Me.messageLabel.Text = "You have clicked the Yes Control"
Me.Label1.Text = "0"
Me.Label1.Text = Me.Label1.Text + 1
EndSub
PrivateSub NoRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoRadioButton.CheckedChanged
'Displays You have clicked the No Control
Me.messageLabel.Text = "You have clicked the No Control"
 
Me.Label1.Text = "0"
Me.Label1.Text = Me.Label1.Text + 1
EndSub
PrivateSub crazyCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles crazyCheckBox.CheckedChanged
'Displays crazy control
Me.messageLabel.Text = "You have clicked the Crazy Control"
Me.Label1.Text = "0"
Me.Label1.Text = Me.Label1.Text + 1
EndSub
PrivateSub ExitLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitLabel.Click
'Displays exit control
 
Me.messageLabel.Text = "You have clicked the Exit Control"
Me.Label1.Text = "0"
Me.Label1.Text = Me.Label1.Text + 1
 
 
 
EndSub
PrivateSub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
'Clear text box
Me.messageLabel.Text = ""
 
EndSub
EndClass
 
Last edited by a moderator:
Here is one solution :
VB.NET:
    Private Sub pushButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PushButton1.Click
        Static Pushes As Integer = 0 'Static variables are only accessible inside the sub
        Pushes += 1
        Display("Push Control", Pushes)
    End Sub

    Private Sub YesRadioButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YesRadioButton.Click
        Static Yes As Integer = 0
        Yes += 1
        Display("Yes Radio Control", Yes)
    End Sub

    Private Sub NoRadioButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoRadioButton.Click
        Static No As Integer = 0
        No += 1
        Display("No Radio Control", No)
    End Sub

    Private Sub crazyCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrazyCheckBox.CheckedChanged
        Static Crazy As Integer = 0
        Crazy += 1
        Display("Crazy CheckBox Control", Crazy)
    End Sub

    Private Sub ExitLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitLabel.Click
        Static ExitL As Integer = 0
        ExitL += 1
        Display("Crazy Exit Label", ExitL)
    End Sub

    Private Sub Display(ByRef Ctlname As String, ByVal Number As Integer)
        If Number = 1 Then
            Me.MessageLabel.Text = String.Format("You have clicked Once the {0} ", Ctlname, Number)
        Else
            Me.MessageLabel.Text = String.Format("You have clicked {1} times the {0} ", Ctlname, Number)
        End If
    End Sub
 
Just in case you missed it, the reason your original code did not work is that if you look carefully, every time you press the button, you are setting the counter to zero, and then adding one to it. That's why it would never get past 1.

Also, you are using the same counter for every control.
 
Back
Top