[英]Selected Radio Buttons Error
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. 在此表单上,我有一个文本框,可从数据库中删除问题,还有4个单选按钮,可从数据库中读取可能的答案。 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. 例如,表中有5列,即:option1,option2,option3,option4和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" 因此,每当用户选择一个单选按钮时,我希望系统检查所选的单选按钮的文本名称是否等于“ RightAnswer”列中的记录,如果是,则我将使一个消息框显示“正确”,如果不是,则显示messgaebox “错误”
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 加载表单时调用此方法
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 单击按钮时调用此方法
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? 我错过了什么吗?
As referring to your code, do this check ( rdb.Checked
) before entering to the CorrectAnswer
method. 参考您的代码,在进入CorrectAnswer
方法之前,请执行此检查( rdb.Checked
)。 Like 喜欢
if(rdb.Checked)
{
CorrectAnswer(rdb);
}
Do this check for all RadioButton
s and pass only the checked RadioButton
to the method. 进行所有RadioButton
的检查,仅将选中的RadioButton
传递给方法。
And you can remove rdb.Checked
condition in side the CorrectAnswer
method. 您可以在CorrectAnswer
方法旁边删除rdb.Checked
条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.