我在弄一个在线答题模块,现在的问题是:选了答案后 在答题卡里面无法显示考生选择的答案。
在答题界面里通过下面的代码实现记录考生答案 //将学生选择的答案记录到studentAnswer[]中
        private void radioBtA_Click(object sender, EventArgs e)
        {
            QuizHelper.studentAnswers[QuesIndex] = Convert.ToString(((RadioButton)sender).Tag);
        }在答题卡界面通过下面的代码实现显示考生的答案        private void AnswerCard_Load(object sender, EventArgs e)
        {
            countdown.Start();//启动倒计时
            int index = 0;
            foreach (Control item in this.Controls)
            {
                if (item is Label)//如果是标签
                {
                    index = Convert.ToInt32(item.Tag);
                    if (index != -1)
                    {
                        item.Text = QuizHelper.studentAnswers[index];
                    }
                }
            }
        }但是,运行后答题卡里面不显示考生答案

解决方案 »

  1.   

    在 radioBtA_Click 方法里设置断点,看看 QuesIndex 是多少,再在 AnswerCard_Load 方法里 item.Text = QuizHelper.studentAnswers[index] 这行设置断点,看看能不能执行到这,如果能看看
    studentAnswers 里有些什么内容,跟 index = Convert.ToInt32(item.Tag) 的 index 能不能对上
      

  2.   

    在 radioBtA_Click 方法里设置断点后,QuesIndex 是0
    再在item.Text = QuizHelper.studentAnswers[index] 这行设置断点,然后继续逐语句运行,
    到index = Convert.ToInt32(item.Tag);这里时 Tag 是19,index 也是19,
    item.Text = QuizHelper.studentAnswers[index]里面 index 是19,studentAnswers[0]对应的是我选的A,这句执行过后再看 item.Text  里面却是空的,也就是并没有将studentAnswers[0]里面的A放到item.Text里面去。
    这个要怎么解决呢!
      

  3.   

    RadioButton.Tag 和 Label.Tag 是一一对应的吗?
    radioBtA_Click 里只给了 QuizHelper.studentAnswers[0] 赋值,其他 index 的都空着,AnswerCard_Load 里只有等到 foreach 循环到 index 为 0 时,再看看 item.Text = QuizHelper.studentAnswers[index] 之后 item.Text 有没有值
      

  4.   

    接 3楼
    第一行说错了
    QuesIndex 和 Label.Tag 是不是对应的?
      

  5.   

    你说的是item.Tag 吗
    QuesIndex是0,item.Tag 是19  
      

  6.   

    我的意思是,你上面只给 QuizHelper.studentAnswers 第 0 索引赋了值,下面查找第 19 索引自然找不到,你在给界面上的 Label 的 Tag 属性赋值时,是不是与 QuesIndex 对应起来的?
      

  7.   

    是的  对应的,下载这个问题解决了  我原来加了个panel控件 现在把它删了,就正常显示了。
    但是现在交卷出问题了,
              private void Result_Load(object sender, EventArgs e)
            {
                //答对的题目数
                int correctNum = 0;
                for (int i = 0; i < QuizHelper.questionNum; i++)
                {
                    if (QuizHelper.studentAnswers[i] == QuizHelper.correctAnswers[i])
                    {
                        correctNum++;
                    }
                }
    提示说索引超出范围,这是肿么回事呢?
    我是在答题界面 读取题目信息的时候将标准答案放进List<string> correctAnswers里面的,不知道 这里是否有问题
              public void GetQuestion()
            {
                lblQuesId.Text = "题 " + (QuesIndex + 1).ToString();
                int quesId = QuizHelper.selectedQuestionId[QuesIndex];
                string sql = "select Question,OptionA,OptionB,OptionC,OptionD,Answer from Question where QuestionId=" + quesId + "";
                try
                {
                    DBHelper.conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (sdr.Read())
                    {
                        riTBQuestion.Text = sdr["Question"].ToString();
                        radioBtA.Text ="A."+sdr["OptionA"].ToString();
                        radioBtB.Text ="B."+ sdr["OptionB"].ToString();
                        radioBtC.Text ="C."+sdr["OptionC"].ToString();
                        radioBtD.Text ="D."+sdr["OptionD"].ToString();
                        QuizHelper.correctAnswers.Add(sdr["Answer"].ToString());
                    }
                    sdr.Close();
                }
      

  8.   

    QuizHelper.questionNum 这个值怎么来的
    你这答题系统是不是一次一题,答好一题才显示下一题?
      

  9.   

    晕死  刚才CSDN系统维护。
    定义了QuizHelper这个类,里面定义了这些东东:
            public static List<int> allQuestionIds=new List<int>();//所有题目 Id
            public static bool[] selectedStates=new bool[50];//题目是否已被抽中
            public static int questionNum=20;//题目数量
            public static int[] selectedQuestionId=new int[20];//选中题目的Id
            public static List<string> correctAnswers = new List<string>();//标准答案
            public static string[] studentAnswers=new string[20];//学员用户的答案
    “你这答题系统是不是一次一题,答好一题才显示下一题?”是的
    我设断点调试了一下,发现List<string> correctAnswers 里面只有点击查看了的题的答案,也就是说不能将List<string> correctAnswers放在读取题目那里获取答案,但是要怎么改呢 
      

  10.   

    把 QuizHelper.questionNum 换成 QuizHelper.studentAnswers.Count
    for (int i = 0; i < QuizHelper.studentAnswers.Count; i++)
    {
      if (QuizHelper.studentAnswers[i] == QuizHelper.correctAnswers[i])
      {
        correctNum++;
      }
    }
      

  11.   

    我这里设定的questionNum=studentAnswers=20,学生没回答的studentAnswers里面记录为Null,而且就算studentAnswers!=20,把 QuizHelper.questionNum 换成 QuizHelper.studentAnswers.Count这样计算的也是答题的正确率,那些没回答的题 就没被算在里面。
      

  12.   

    只是 for 循环时换,统计时还是 correctNum / QuizHelper.questionNum ,不就是正确率了
      

  13.   

    可这里 把 QuizHelper.questionNum 换成QuizHelper.studentAnswers.Count也没有任何改变的,它们都等于20