|
-
May 27th, 2011, 04:11 AM
#1
Thread Starter
New Member
Visual Basic 2010 HELP
Ok this might be confusing as I dont know how to explain.
I'm doing a quiz and am using radio buttons. On a seperate form is a results page with a label which I want the results to show up on. I have made the label public.
The label should start with zero. There are three questions, each correct question should add a point to the result label.
I have no idea what I am doing! 
So far I have done:
Dim lblResult1 As Integer
If rbtnAnswer1_1_1.Checked = True Then
lblResult1.Text =
Can anyone help?
-
May 27th, 2011, 04:51 AM
#2
Re: Visual Basic 2010 HELP
Hi,
Try this:
vb Code:
If rbtnAnswer1_1_1.Checked = True Then
lblResult1.Text = lblResult1.Text +1
-
May 27th, 2011, 04:59 AM
#3
Thread Starter
New Member
Re: Visual Basic 2010 HELP
Cheers, just tryed it. It doesnt work, suggest that lblResult1.Text is not right. but I dont know what to put instead.
I tried:
Dim lblResult1 As Integer = 0
If rbtnAnswer1_1_1.Checked = True Then
lblResult1 = lblResult1 + 1
End If
but this still didnt work
-
May 27th, 2011, 05:26 AM
#4
Re: Visual Basic 2010 HELP
Hi,
Try this:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lblResult1 As Integer = 0
If rbtnAnswer1_1_1.Checked = True Then
lblResult1.Text = lblResult1.Text + lblResult1 + 1
End If
End Sub
-
May 27th, 2011, 05:27 AM
#5
Re: Visual Basic 2010 HELP
You need to give more information then just "it didn't work". Please read the link in my signature on how to create a post that gives you the best chance of getting a good response. Note the title of the thread should not be HELP as it is understood you need help, you are asking a question!
Now, to your problem. You should know you have a fault, as VS should underline the code in question and tell you what the problem is. Ensure you have Option Strict/Option Explict turned on, they will also tell you of fault that are obvious once you see them.
I will give you an example of why your code would not work. You want a car to go at 100 mph, so you say
Of course, this will not work, you are telling the car that is it 100, what you want to tell the car, is to change its speed to 100. So this would be more approriate.
You need to tell the object, which property you want to change. Using this on your code above you would get:
Code:
lblResult1.Text = lblResult1.Text + 1
In theory, this would work, except that you are adding 1, a number, to a text value which is a string. What would happen if you did this
Do you get "hello 21" or should it be "3" or should it be something else? That is what the options are for, to tell you that you could get the wrong answer as you have not been explicit enough. The simple way to solve this, is to change the value in the label into a number.
Code:
lblResult1.Text = Val(lblResult1.Text) + 1
So now you are almost there. But... The value that comes out of the equation is a number, and again, you are trying to put it back into text! So a final piece should be used.
Code:
lblResult1.Text = (Val(lblResult1.Text) + 1).ToString
Convert the existing .Text value to a number, add 1 to that number, convert the number back into a string, and put the string back into the labels text property.
-
May 27th, 2011, 07:48 AM
#6
Re: Visual Basic 2010 HELP
Grim - I'm a little shocked and somewhat disappointed in you... encouraging the use of a label to keep track of data... tisk tisk tisk...
This is something I see a lot... I'm beginning to wonder if this is something people just do... or are there instructors out there teaching this carp ... Labels should be used for DISPLAYING DATA... NOT as a storage bin. You're better off creating a variable with the appropriate scope... and incrementing that... and then using that value to update the label... if you want to simplify it, create a sub that will increment the variable and the label at the same time... then all you would need to do is call that.
-tg
-
May 27th, 2011, 08:32 AM
#7
Re: Visual Basic 2010 HELP
Hah, I wasn't doing that honest! On something at this level, giving someone a completely different way of working often means they get lost on how it relates to their starting question. Once they have worked out why, then I can hit them with the next bit, honest!
-1 for teaching skills
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|