Thread: Radiobutton text to string

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Radiobutton text to string

    Hello. I'm creating a simple program that randomizes answers in 3 radio buttons. The user will pick 1 answer. A button is pressed and the answer is checked against the real answer. I can't seem to be able to check the selected radiobutton text to the correct answer.

    I have the correct answer defined at a global level.

    I need to use a simple way to do this, so I figured this would be the best way:

    Code:
                RadioButton selectedRadioButton = (RadioButton)sender;
    
                decimal decGivenCorrectAnswer = selectedRadioButton.Text.ToString("N2");
    
                if (decGivenCorrectAnswer = decCorrectAnswer)
                {
    
                }
    
                else
                {
    
                }
    decCorrectAnswer is the global variable for the already calculated correct answer.

    Errors:

    Code:
    Error	1	The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments
    Code:
    Error	2	Argument '1': cannot convert from 'string' to 'System.IFormatProvider'
    Code:
    Error	3	Cannot implicitly convert type 'decimal' to 'bool'
    Sorry. I'm new at this so I'm not understanding the way to check the selected radio button's text to a decimal variable.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Are you sure you didn't mean:

    Code:
    if (decGivenCorrectAnswer == decCorrectAnswer)
    I'm pretty sure this explains the last 2 errors...

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    decimal decGivenCorrectAnswer = selectedRadioButton.Text.ToString("N2");
    You're converting a string to a string? I'm sure you're attempting to convert a string to a decimal instead:
    Code:
    decimal decGivenCorrectAnswer = decimal.Parse(selectedRadioButton.Text, System.Globalization.NumberStyles.Float);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Well tried both ideas to fix the code. The first one helped a little, that was simple. The second one gave a lot of errors. I ended up linking the click events for all the radio buttons and throwing the following code into one of the check change events :

    Code:
                //Sender argument to a RadioButton data type.
                RadioButton selectedRadioButton = (RadioButton)sender;
    
                decChosenAnswer = decimal.Parse(selectedRadioButton.Text);
    decChosenAnswer is a global variable.

    I then put the following the the button to check for validation :

    Code:
                if (decChosenAnswer == decCorrectAnswer)
                {
                    MessageBox.Show("Hello");
                }
    
                else
                {
                    MessageBox.Show("Bye");
                }
    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Reading text file into a string obj
    By thetinman in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2008, 11:42 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM