|
-
Aug 18th, 2009, 02:01 AM
#1
Thread Starter
Member
Radio button problem
Referring to this thread http://www.vbforums.com/showthread.php?t=580584
Code:
Private Sub rdbAfghanistan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbAfghanistan.CheckedChanged
If rdbAfghanistan.Checked And random = 0 Then
lblMessage.Text = "Congratulations, you'ge got the corrent one"
Else
lblMessage.Text = "Sorry, you are wrong, the correct answer is "
End If
btnFlag.Enabled = True 'enable the btnFlag button
pnlCountries.Enabled = False 'disable the pnlCountries panel
rdbAfghanistan.Checked = False
End Sub
Whenever I clicked the correct button, it does not show the correct answer as the radio button is quickly unchecked. How to overcome this?
What i want is when i check the button, the message is displayed and the radio button again is unchecked.
-
Aug 18th, 2009, 02:08 AM
#2
Hyperactive Member
Re: Radio button problem
 Originally Posted by sanox
Referring to this thread http://www.vbforums.com/showthread.php?t=580584
Code:
Private Sub rdbAfghanistan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbAfghanistan.CheckedChanged
If rdbAfghanistan.Checked And random = 0 Then
lblMessage.Text = "Congratulations, you'ge got the corrent one"
Else
lblMessage.Text = "Sorry, you are wrong, the correct answer is "
End If
btnFlag.Enabled = True 'enable the btnFlag button
pnlCountries.Enabled = False 'disable the pnlCountries panel
rdbAfghanistan.Checked = False
End Sub
Whenever I clicked the correct button, it does not show the correct answer as the radio button is quickly unchecked. How to overcome this?
What i want is when i check the button, the message is displayed and the radio button again is unchecked.
In your code that handles the CheckedChanged event, you have the line which says "rdbAfghanistan.Checked=False"
This line changes the check state of the radio button, which calls the CheckedChanged handler again - this time, the condition in the If statement evaluates to false (because the state of the radiobutton is unchecked), so it tells you that you got the wrong answer.
Here you need to use a flag, that signals "correct answer given". When the handler is called the second time, it should check the state of the flag, and skip the IF statement completely if the flag is set. Also, you need to move the "rdbAfghanistan.Checked=False" line to the THEN block of your IF statement. That is also where you should set the flag to TRUE.
Last edited by Runesmith; Aug 18th, 2009 at 02:42 AM.
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
-
Aug 18th, 2009, 02:09 AM
#3
Re: Radio button problem
That happens because you're checking then unchecking...
To make that kind of thing work,maybe you can use some var as a flag to see if it was the user that checked/unchecked the radio button...
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Aug 18th, 2009, 03:11 AM
#4
Re: Radio button problem
Hi,
You can try this (did not tested it):
vb Code:
Private Sub rdbAfghanistan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbAfghanistan.CheckedChanged If rdbAfghanistan.Checked And random = 0 Then lblMessage.Text = "Congratulations, you'ge got the corrent one" rdbAfghanistan.Checked = False Else lblMessage.Text = "Sorry, you are wrong, the correct answer is " rdbAfghanistan.Checked = False End If btnFlag.Enabled = True 'enable the btnFlag button pnlCountries.Enabled = False 'disable the pnlCountries panel End Sub
-
Aug 18th, 2009, 04:19 AM
#5
Hyperactive Member
Re: Radio button problem
 Originally Posted by sparrow1
Hi,
You can try this (did not tested it):
It would still need a flag. THEN block of the IF statement is executed if the rdb is checked and random=0. Here you set rdb.checked=False, which generates a new CheckedChanged event. When that event executes, the condition in the IF statement evaluates to False, so the ELSE block is executed, telling you that you got the wrong answer.
The only way to prevent this is to use a flag. I would suggest the following:
Declare a boolean called "GotIt" and set it to False.
Change the code in the CheckedChanged event handler to:
Code:
Private Sub rdbAfghanistan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbAfghanistan.CheckedChanged
If Not (GotIt) then
If rdbAfghanistan.Checked And random = 0 Then
lblMessage.Text = "Congratulations, you'ge got the corrent one"
rdbAfghanistan.Checked = False
GotIt=True
Else
lblMessage.Text = "Sorry, you are wrong, the correct answer is "
rdbAfghanistan.Checked = False
End If
Else
GotIt=False
Exit Sub
End If
btnFlag.Enabled = True 'enable the btnFlag button
pnlCountries.Enabled = False 'disable the pnlCountries panel
End Sub
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
-
Aug 18th, 2009, 07:44 AM
#6
Thread Starter
Member
Re: Radio button problem
Still not working. Never mind. I think i will just leave it
-
Aug 18th, 2009, 07:58 AM
#7
Hyperactive Member
Re: Radio button problem
"Not working" is not a problem description. What is not working, and how is it not working?
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
-
Aug 18th, 2009, 08:07 AM
#8
Re: Radio button problem
You can disable it when selecting the correct answer. Why not using Optionbuttons in groups? Think that will solve your issue in an instance.
-
Aug 18th, 2009, 08:15 AM
#9
Thread Starter
Member
Re: Radio button problem
It is because the instruction asked me to create those radio buttons in a panel. Sad, but never mind. its ok. By the way, referring to this http://www.vbforums.com/showthread.php?t=580584
Code:
Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles rdbAfghanistan.CheckedChanged, rdbBangladesh.CheckedChanged, rdbBelgium.CheckedChanged, _
rdbCanada.CheckedChanged, rdbChina.CheckedChanged, rdbEgypt.CheckedChanged, _
rdbFinland.CheckedChanged, rdbFrance.CheckedChanged, rdbGreenland.CheckedChanged, _
rdbMalaysia.CheckedChanged, rdbSudan.CheckedChanged, rdbZambia.CheckedChanged
Dim rButton As RadioButton = DirectCast(sender, RadioButton)
If rButton.Text.ToUpperInvariant = CStr(countryPic(random)).ToUpperInvariant Then
lblMessage.Text = "Congratulations, you'ge got the corrent one"
Else
lblMessage.Text = "Sorry, you are wrong, the correct answer is "
End If
btnFlag.Enabled = True
pnlCountries.Enabled = False
End Sub
why is this not working? I mean, it always show "Sorry, you are wrong, the correct answer is " even the correct answer is selected
-
Aug 18th, 2009, 08:28 AM
#10
Re: Radio button problem
What's the countryPic?
See what values are in the rButton.Text and countryPic(random) when the application run?
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Aug 18th, 2009, 08:46 AM
#11
Thread Starter
Member
Re: Radio button problem
This is what my ccountries in the form look like 
and
countryPic(0) = "Afghanistan.gif"
countryPic(1) = "Bangladesh.gif"
countryPic(2) = "Belgium.gif"
countryPic(3) = "Canada.gif"
.......an so on
for rButton.Text, i am not sure what it is as I followed http://www.vbforums.com/showthread.php?t=580584
-
Aug 18th, 2009, 08:56 AM
#12
Hyperactive Member
Re: Radio button problem
 Originally Posted by sanox
Your radiobuttontext never equals countrypic name, because countrypic names have a ".gif" added. This part needs to be removed for the two strings to be equal.
You need to compare rButtonText.ToUpperInvariant with Cstr(CountryPic(Random)).ToUpperInvariant.subString(0,Cstr(countryPic(Random)).length-4)
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
-
Aug 18th, 2009, 09:00 AM
#13
Hyperactive Member
Re: Radio button problem
Change this line:
Code:
If rButton.Text.ToUpperInvariant = CStr(countryPic(random)).ToUpperInvariant Then
to
Code:
If rButton.Text.ToUpperInvariant = CStr(countryPic(random)).ToUpperInvariant.SubString(0, CStr(countrypic(random)).length-4) Then
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
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
|