qq_41500526 2019-04-17 16:24 采纳率: 0%
浏览 442

C#窗体实现在线答题系统时代码无bug能运行,但不显示题目信息,求问怎么回事?

数据库那边没有问题,连接语句在别的窗体也能用,怀疑是questionIndex的问题,求大神帮忙看下怎么改
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections;

namespace graduationproject
{
public partial class PsychologicalTest : Form
{
public int questionIndex = 1;
public PsychologicalTest()
{
InitializeComponent();
}
private void PsychologicalTest_Load(object sender, EventArgs e)
{

        timer1.Start();   // 启动计时器
        GetQuestionDetails();  // 显示题目信息
        CheckOption();      // 如果题目已经答过,让相应的选项选中 
        CheckBtnNext();       // 确定是否到了最后一题
    }
    private void CheckBtnNext()
    {
        // 如果达到20题,就让“下一题”按钮的文字显示为“提交”
        if (questionIndex >= QuizHelper.selectedQuestionIds.Length)
        {
            button2.Text = "提交";
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        // 如果没有到最后一题,就继续显示新题目信息
        if (questionIndex < QuizHelper.selectedQuestionIds.Length)
        {
            questionIndex++;
            GetQuestionDetails();  // 显示试题信息
            CheckOption();         // 如果题目已经答过,让相应的选项选中    
            CheckBtnNext();       // 确定是否到了最后一题
        }
        else  // 否则,打开答题卡窗体
        {
            OpenResultCard();
        }
    }
    private void OpenResultCard()
    {
        PsychologicalTestResult psychologicalTestResult = new PsychologicalTestResult();
        psychologicalTestResult.MdiParent = this.MdiParent;
        psychologicalTestResult.Show();
        this.Close();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        int minute;   // 当前的分钟
        int second;   // 秒

        // 如果还有剩余时间,就显示剩余的分钟和秒数
        if (QuizHelper.remainSeconds > 0)
        {
            QuizHelper.remainSeconds--;
            minute = QuizHelper.remainSeconds / 60;
            second = QuizHelper.remainSeconds % 60;
            label4.Text = string.Format("{0:00}:{1:00}", minute, second);
        }
        // 否则,就提示交卷
        else
        {
            timer1.Stop();
            MessageBox.Show("时间到了!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            this.Close();
        }
    }
    private void radioButton_Click(object sender, EventArgs e)
    {
        QuizHelper.studentAnswers[questionIndex] = Convert.ToString(((RadioButton)sender).Tag);
    }

    public void GetQuestionDetails()
    {
        // 显示当前的题目信息
        label1.Text = string.Format("第{0}题:", questionIndex);
        // 查询题目信息的sql语句
        string sql = "SELECT Question FROM PsyQuestion WHERE QNumber=questionIndex ";
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=graduationproject;Integrated Security=true");
        try
        {
            conn.Open();
            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                label3.Text = reader["Question"].ToString();
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            DBHelper.connection.Close();
        }
    }
    private void CheckOption()
    {
        switch (QuizHelper.studentAnswers[questionIndex])
        {
            case "A":
                radioButton1.Checked = true;
                break;
            case "B":
                radioButton2.Checked = true;
                break;
            case "C":
                radioButton3.Checked = true;
                break;
            default:
                radioButton1.Checked = false;
                radioButton2.Checked = false;
                radioButton3.Checked = false;
            break;
        }
    }
}

}

  • 写回答

2条回答 默认 最新

  • 1进击的小白 2019-04-17 16:44
    关注

    在GetQuestionDetails方法里面lable1.text和lable3.text两行打断点调试下,这样看代码看不出来问题

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料