Results 1 to 13 of 13

Thread: Radio button problem

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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.

  2. #2
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Radio button problem

    Quote Originally Posted by sanox View Post
    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

  3. #3
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Radio button problem

    Hi,

    You can try this (did not tested it):

    vb Code:
    1. Private Sub rdbAfghanistan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbAfghanistan.CheckedChanged
    2.         If rdbAfghanistan.Checked And random = 0 Then
    3.             lblMessage.Text = "Congratulations, you'ge got the corrent one"
    4.             rdbAfghanistan.Checked = False
    5.         Else
    6.             lblMessage.Text = "Sorry, you are wrong, the correct answer is "
    7.             rdbAfghanistan.Checked = False  
    8.         End If
    9.         btnFlag.Enabled = True 'enable the btnFlag button
    10.         pnlCountries.Enabled = False 'disable the pnlCountries panel
    11.        
    12.     End Sub
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Radio button problem

    Quote Originally Posted by sparrow1 View Post
    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

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    Re: Radio button problem

    Still not working. Never mind. I think i will just leave it

  7. #7
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    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

  8. #8
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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

  10. #10
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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

  12. #12
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Radio button problem

    Quote Originally Posted by sanox View Post
    This is what my ccountries in the form look like

    and



    for rButton.Text, i am not sure what it is as I followed http://www.vbforums.com/showthread.php?t=580584
    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

  13. #13
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    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
  •  



Click Here to Expand Forum to Full Width