Click here to Skip to main content
15,886,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am developing a project that generates questions from a database, the questions are generated with multiple choice answers. On this form I have got a textbox that reds the question from the database and 4 radio buttons that reads the possible answers from the database. The radio buttons text names are updated with records from a database table each time the "next button" is clicked.

What I want this program to do is that when the user selects one of the radio buttons, I want the system to check if the selected radio button textname equals the right answer in the database table. for example in the table there are 5 columns namely: option1, option2, option3, option4 and rightAnswer.
So whenever a user selects a radio button, I would like the system to check if the selected radio button's textname equals the record in the "RightAnswer" column and if so I would a messagebox to show "correct" and if not the messgaebox to show "wrong"

WORK I HAVE DONE SO FAR:
This is the the way I am updating the radio button text names from the database
This method is called when the form is loaded
C#
void LoadingPossibleAnswers()
      {

          Query = "SELECT * FROM AnswersTbl";
              theReader = conn.ExecuteStatement(Query);
              while (theReader.Read())
              {
                  
                  radioButton1.Text = theReader["Option1"].ToString();
                  radioButton2.Text = theReader["Option2"].ToString();
                  radioButton3.Text = theReader["Option3"].ToString();
                  radioButton3.Text = theReader["Option4"].ToString();


              }

              conn.CloseConnection();
      }

This method is called when the button is clicked

C#
void CorrectAnswer( RadioButton rdb)
        {
            string correct = rdb.Text;


            Query = "SELECT * FROM FROM AnswersTbl;"
            theReader = conn.ExecuteStatement(Query);
            while (theReader.Read())
            {
                correct = theReader["RightAnswer"].ToString();


                if (rdb.Checked && rdb.Text == correct)
                {

                    MessageBox.Show("correct");
                }

                else
                {
                    MessageBox.Show("wrong");
                }


            }




            }


When ever I run my code above, else condition executes even if the correct radio button is selected. can anyone please help to why this is? am I missing out something?
Posted
Updated 24-Feb-14 2:06am
v2
Comments
CHill60 24-Feb-14 8:26am    
Try if (rdb.Checked && (rdb.Text == correct)) (spot the extra brackets) - does this work?
1Future 24-Feb-14 8:36am    
It sort of works.. but its going through all the 4 radio buttons.. for example radiobutton1 (wrong) radiobutton2(correct) radiobutton3(wrong) radiobutton4(wrong).. is there any i can only make it check the selected radio button?

Hi King,

As per your comments, once you found the radio button having correct answer, you dont want to check/validate other radio buttons.

If that is the case you could use break; statement inside the loop to jump out of the while condition

Like this

if (rdb.Checked && rdb.Text == correct)
{

MessageBox.Show("correct");
break;
}

else
{
MessageBox.Show("wrong");
}

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
Comments
1Future 24-Feb-14 8:52am    
Thank you
♥…ЯҠ…♥ 24-Feb-14 9:05am    
you are welcome ;-)
Your approach is, in my opinion, flawed.

RadioButton has no "text" attribute, it has "content"

This is how I would do it, (you will ofc populate the ArrayList/Array/List from the database.

DO DB CALLS AS FEW TIMES AS POSSIBLE!!! -This is to limit the bandwidth use

Working example:

C#:
C#
using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace ExperimentationProject
{
    public partial class MainWindow : Window
    {
        // String Variables
        public string inputAnswer;
        public string AnswerNum = "1"; // The 
        public string Question = "Who is the current President of the U.S.?";
        
        public MainWindow()
        {
            InitializeComponent();

            // Array 'variable' containing possible answers
            ArrayList Answers = new ArrayList();
            Answers.Add("1. Barack Obama");
            Answers.Add("2. The Govinator");
            Answers.Add("3. Queen Elizabeth II");
            Answers.Add("4. Justin Bieber");

            // Apply values to form content
            groupBox1.Header = Question;

            radioButton1.Content = Answers[0];
            radioButton2.Content = Answers[1];
            radioButton3.Content = Answers[2];
            radioButton4.Content = Answers[3];
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (inputAnswer.Substring(0, 1) == AnswerNum)
            {
                MessageBox.Show("CORRECT!\n your anser was: " + inputAnswer);
            }
            else
            {
                MessageBox.Show("wrong!");
            }
            
        }

        private void radioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton RadBtn = sender as RadioButton;
            if (RadBtn.IsChecked.Value)
                inputAnswer = RadBtn.Content.ToString();
        }
    }
}


WPF:
HTML
<window x:class="ExperimentationProject.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="240">
    <grid>
        <groupbox header="groupBox1" height="245" horizontalalignment="Left" margin="12,12,0,0" name="groupBox1" verticalalignment="Top" width="200" cliptobounds="True">
            <grid>
                <radiobutton content="RadioButton" height="16" horizontalalignment="Left" margin="6,6,0,0" name="radioButton1" verticalalignment="Top" checked="radioButton_Checked " />
                <radiobutton content="RadioButton" height="16" horizontalalignment="Left" margin="6,28,0,0" name="radioButton2" verticalalignment="Top" checked="radioButton_Checked " />
                <radiobutton content="RadioButton" height="16" horizontalalignment="Left" margin="6,50,0,0" name="radioButton3" verticalalignment="Top" checked="radioButton_Checked " />
                <radiobutton content="RadioButton" height="16" horizontalalignment="Left" margin="6,72,0,0" name="radioButton4" verticalalignment="Top" checked="radioButton_Checked " />
                <button content="Button" height="23" horizontalalignment="Left" margin="52,94,0,0" name="button1" verticalalignment="Top" width="75" click="button1_Click" />
            </grid>
        </groupbox>
    </grid>
</window>


The code should be self-explanatory, but notice the -Checked="radioButton_Checked "- that is identical in all the radiobuttons, and the event handler for it. And look at the ".substring(0,1)" those are the only "magic" in this code.

Hope this is helpful!

(remember to accept valid solutions and rate answers appropriately)

-Frank
 
Share this answer
 
Comments
1Future 24-Feb-14 12:57pm    
THANK YOU
1Future 24-Feb-14 17:32pm    
actaully it says 'System.Windows.Forms.RadioButton' does not contain a definition for 'Content' and no extension method 'Content'
is there anything I am missing out?
Frank R. Haugen 24-Feb-14 17:51pm    
Ah! If you are using WinForms it uses "text" and not "content". Since WPF is so superior to WinForms I assume everyone uses WPF

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900