📄 randtestform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace CTestApp
{
public partial class RandTestForm : Form
{
protected SqlConnection conn;
public RandTestForm()
{
InitializeComponent();
}
private void RandTestForm_Load(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestLibraryConnectionString"].ConnectionString);
conn.Open();
this.GenQuestion();
}
catch (Exception exp)
{
MessageBox.Show("无法建立数据连接:" + exp.Message + exp.StackTrace, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
private void RandTestForm_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void btnGen_Click(object sender, EventArgs e)
{
try
{
conn.Open();
this.GenQuestion();
lbAnswer.Visible = false;
}
catch (Exception exp)
{
MessageBox.Show("生成失败:" + exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if(lbAnswer.Visible == false)
lbAnswer.Visible = true;
}
/// <summary>
/// 随机生成试题
/// </summary>
protected void GenQuestion()
{
SqlCommand cmd1 = new SqlCommand("GenQuestion", conn);
cmd1.CommandType = CommandType.StoredProcedure;
SqlDataReader reader1 = cmd1.ExecuteReader();
if (reader1.Read())
{
lbQuestion.Text = reader1[0].ToString();
int iType = (int)reader1[6];
if (iType == 0)
{
radioButton1.Text = reader1[1].ToString();
radioButton2.Text = reader1[2].ToString();
radioButton3.Text = reader1[3].ToString();
radioButton4.Text = reader1[4].ToString();
radioButton1.Visible = true;
radioButton2.Visible = true;
radioButton3.Visible = true;
radioButton4.Visible = true;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
checkBox1.Visible = false;
checkBox2.Visible = false;
checkBox3.Visible = false;
checkBox4.Visible = false;
lbAnswer.Text = "本题答案为:" + (char)(64 + int.Parse(reader1[5].ToString()));
}
else
{
checkBox1.Text = reader1[1].ToString();
checkBox2.Text = reader1[2].ToString();
checkBox3.Text = reader1[3].ToString();
checkBox4.Text = reader1[4].ToString();
checkBox1.Visible = true;
checkBox2.Visible = true;
checkBox3.Visible = true;
checkBox4.Visible = true;
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
radioButton1.Visible = false;
radioButton2.Visible = false;
radioButton3.Visible = false;
radioButton4.Visible = false;
lbAnswer.Text = "本题答案为:" + MultiAnswer.Parse(reader1[5].ToString()).ToAnswerString();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -