c#中带有数据库错误的单选按钮 [英] Radio buttons with database error in c#

查看:105
本文介绍了c#中带有数据库错误的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个从数据库生成问题的项目,这些问题是通过多项选择答案生成的。在这个表单上,我有一个文本框,用于从数据库中删除问题,还有4个单选按钮,用于从数据库中读取可能的答案。每次单击下一个按钮时,单选按钮文本名称都会使用数据库表中的记录进行更新。



我希望这个程序做的是当用户选择其中一个单选按钮,我希望系统检查所选单选按钮textname是否等于数据库表中的正确答案。例如在表中有5列,即:option1,option2,option3,option4和rightAnswer。

因此,每当用户选择一个单选按钮时,我希望系统检查所选的单选按钮是否为textname等于RightAnswer列中的记录,如果是,我会在消息框中显示正确,如果不是,则表示错误的杂物箱



工作我这么快就完成了:

这就是我从数据库更新单选按钮文本名称的方式

加载表单时调用此方法

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

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?

推荐答案

嗨国王,



根据你的意见,一旦你找到了没有正确答案的单选按钮,你不想检查/验证其他单选按钮。



如果是这种情况你可以使用break;循环中的声明跳出来的条件



喜欢这个



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

{



MessageBox.Show(correct);

休息;

}



其他

{

MessageBox.Show(错误);

}



希望这可以帮助你比特。



问候,

RK
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


在我看来,你的方法存在缺陷。



RadioButton没有文本属性,它有内容



这就是我的意思做它,(你将从数据库中填充ArrayList / Array / List。



DO DB尽可能多次调用!!! - 这是为了限制带宽使用



工作例如:



C#:

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#:
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:



WPF:

<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>





代码应该是不言自明的,但请注意-Checked =radioButton_Checked - 在所有的radiobuttons中都是相同的,并且事件处理程序是它。看看.substring(0,1)这些是这段代码中唯一的魔法。



希望这个有用!



(记得接受有效的解决方案和适当的评价答案)



-Frank



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


这篇关于c#中带有数据库错误的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆