当前位置:文档之家› 模拟考试系统C#源码

模拟考试系统C#源码

开始窗体////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Exam{public partial class ExamForm : Form{public ExamForm(){InitializeComponent();}/// <summary>/// ExamForm/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void ExamForm_Load(object sender, EventArgs e){CenterToScreen();Text = "模拟考试系统";this.Width = 360;this.Height = 230;this.progressBar1.Width = 360;this.progressBar1.Height = 10;this.progressBar1.Left = 0;this.progressBar1.Top = 220;this.timer1.Start();}int i = 8;private void timer1_Tick(object sender, EventArgs e){this.progressBar1.PerformStep();i--;if (i==0){this.timer1.Stop();FormExam f = new FormExam();f.Show();this.Hide();}}}}数据库辅助类////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace Exam{public class DataBase{public string connString =@"Data Source=XUTAO;Initial Catalog=Exam;Integrated Security=True";private SqlConnection conn;/// <summary>/// SqlConnection/// </summary>public SqlConnection Conn{get{if (conn == null){conn = new SqlConnection(connString);}return conn;}}/// <summary>/// 打开数据库/// </summary>public void openData(){if (Conn.State == ConnectionState.Closed){Conn.Open();}else if (Conn.State == ConnectionState.Broken){Conn.Close();Conn.Open();}}/// <summary>/// 关闭数据库/// </summary>public void closeData(){if (Conn.State == ConnectionState.Open ||Conn.State == ConnectionState.Broken){Conn.Close();}}}}主窗体////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace Exam{public partial class FormExam : Form{public FormExam(){InitializeComponent();}/// <summary>/// 调用数据库/// </summary>DataBase db = new DataBase();/// <summary>/// ExamForm/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void FormExam_Load(object sender, EventArgs e) {CenterToScreen();this.Cursor = Cursors.Hand;this.Text = "模拟考试系统";this.tscmbExamType.SelectedIndex = 0;this.timerDate.Start();this.Width = 1000;this.Height = 600;this.MaximizeBox = false;this.Top = 50;this.groupBox1.Top = 40;this.groupBox1.Left = 10;this.groupBox1.Height = 250;this.groupBox1.Width = 200;this.listExam.GridLines = true;this.listExam.FullRowSelect = true;this.groupBox2.BackColor = Color.Transparent;this.groupBox2.Text = "试题题干";this.groupBox2.Height = 250;this.groupBox2.Width = 755;this.groupBox2.Left = 215;this.groupBox2.Top = 40;this.groupBox2.ForeColor = Color.White;this.rtbExam.ReadOnly = true;this.groupBox3.Text = "试题选项";this.groupBox3.ForeColor = Color.White;this.groupBox3.BackColor = Color.Transparent;this.groupBox3.Left = 215;this.groupBox3.Width = 755;this.groupBox3.Height = 250;this.groupBox3.Top = 295;this.groupBox4.Text = "操作区域";this.groupBox4.ForeColor = Color.White; this.groupBox4.BackColor = Color.Transparent; this.groupBox4.Width = 200;this.groupBox4.Height = 250;this.groupBox4.Top = 295;this.groupBox4.Left = 10;this.button1.Text = "开始答题";this.button1.Top = 40;this.button1.Left = 50;this.button1.Width = 90;this.button1.Height = 25;this.button1.ForeColor = Color.Blue;this.button2.Text = "上一试题";this.button2.Top = 80;this.button2.Left = 50;this.button2.Width = 90;this.button2.Height = 25;this.button2.ForeColor = Color.Blue;this.button3.Text = "下一试题";this.button3.Top = 120;this.button3.Left = 50;this.button3.Width = 90;this.button3.Height = 25;this.button3.ForeColor = Color.Blue;this.button4.Text = "停止答题";this.button4.Top = 160;this.button4.Left = 50;this.button4.Width = 90;this.button4.Height = 25;this.button4.ForeColor = Color.Blue;this.button5.Text = "提交试题";this.button5.Top = 200;this.button5.Left = 50;this.button5.Width = 90;this.button5.Height = 25;this.button5.ForeColor = Color.Blue;this.radioButton1.Text = "A";this.radioButton1.Left = 5;this.radioButton1.Top = 50;this.radioButton1.ForeColor = Color.GreenYellow; this.radioButton1.Enabled = false;this.radioButton2.Text = "B";this.radioButton2.Left = 5;this.radioButton2.Top = 100;this.radioButton2.ForeColor = Color.GreenYellow; this.radioButton2.Enabled = false;this.radioButton3.Text = "C";this.radioButton3.Left = 5;this.radioButton3.Top = 150;this.radioButton3.ForeColor = Color.GreenYellow; this.radioButton3.Enabled = false;this.radioButton4.Text = "D";this.radioButton4.Left = 5;this.radioButton4.Top = 200;this.radioButton4.ForeColor = Color.GreenYellow; this.radioButton4.Enabled = false;bel1.Left = 40;bel1.Top = 52;bel1.Text = "";bel2.Left = 40;bel2.Top = 102;bel2.Text = "";bel3.Left = 40;bel3.Top = 152;bel3.Text = "";bel4.Left = 40;bel4.Top = 202;bel4.Text = "";bel5.Left = 5;bel5.Top = 20;bel5.Text = "";bel5.ForeColor = Color.Red;bel6.Left = 35;bel6.Top = 20;bel6.Text = "";this.listExam.Enabled = false;this.button2.Enabled = false;this.button3.Enabled = false;this.button4.Enabled = false;this.button5.Enabled = false;this.toolStripButton1.Enabled = false;}/// <summary>/// 时间/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void timerDate_Tick(object sender, EventArgs e){this.toolStripLabel2.Text = DateTime.Now.ToString();}int j = 600;/// <summary>/// 计时器/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void timerP_Tick(object sender, EventArgs e){j--;this.toolStripLabel4.Text = j.ToString();if (j > 60){this.toolStripLabel4.ForeColor = Color.GreenYellow;}else{this.toolStripLabel4.ForeColor = Color.Red;}if (j == 0){this.timerP.Stop();this.radioButton1.Enabled = false;this.radioButton2.Enabled = false;this.radioButton3.Enabled = false;this.radioButton4.Enabled = false;bel1.Enabled = false;bel2.Enabled = false;bel3.Enabled = false;bel4.Enabled = false;this.listExam.Enabled = false;this.button1.Enabled = false;this.button2.Enabled = false;this.button3.Enabled = false;this.button4.Enabled = false;this.button5.Enabled = true;MessageBox.Show("时间到,请提交试题!");return;}}/// <summary>/// 开始答题/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e) {this.timerP.Start();this.radioButton1.Enabled = true;this.radioButton2.Enabled = true;this.radioButton3.Enabled = true;this.radioButton4.Enabled = true;bel1.Enabled = true;bel2.Enabled = true;bel3.Enabled = true;bel4.Enabled = true;this.listExam.Enabled = true;this.listExam.Focus();//第一行默认选中this.listExam.Items[0].Selected = true;this.button1.Enabled = false;this.button2.Enabled = true;this.button3.Enabled = true;this.button4.Enabled = true;this.button5.Enabled = true;}/// <summary>/// 停止答题/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button4_Click(object sender, EventArgs e){this.timerP.Stop();this.radioButton1.Enabled = false;this.radioButton2.Enabled = false;this.radioButton3.Enabled = false;this.radioButton4.Enabled = false;bel1.Enabled = false;bel2.Enabled = false;bel3.Enabled = false;bel4.Enabled = false;this.listExam.Enabled = false;this.button1.Enabled = true;this.button2.Enabled = false;this.button3.Enabled = false;this.button4.Enabled = false;this.button5.Enabled = false;}/// <summary>/// 显示试题表数据/// </summary>public void ListShow(){Random random = new Random();int rand = random.Next(0, 190);string sql = string.Format(@"select top 10ExamPaper.SubjectNo,ExamPaper.SubjectName,ExamResult.A,ExamResult.B,ExamResult.C,ExamResult.D,ExamPaper.TypeIDfrom ExamPaperinner joinExamResultonExamPaper.TypeID=ExamResult.TypeIDandExamPaper.SubjectNo=ExamResult.SubjectNowhere ExamPaper.SubjectNonot in(select top {0} ExamPaper.SubjectNo from ExamPaper)and ExamResult.TypeID=(select TypeID from ExamPaperTypewhereExamPaperType.TypeName like '%{1}%')",rand, this.tscmbExamType.Text.Substring(0, 3));try{db.openData();SqlCommand comm = new SqlCommand(sql, db.Conn);SqlDataReader reader = comm.ExecuteReader();if (!reader.HasRows){return;}else{while (reader.Read()){string SubjectNo = reader["SubjectNo"].ToString();//题号string SubjectName = reader["SubjectName"].ToString();//题干string TypeID = reader["TypeID"].ToString();string A = reader["A"].ToString();string B = reader["B"].ToString();string C = reader["C"].ToString();string D = reader["D"].ToString();ListViewItem items = new ListViewItem(SubjectNo);items.SubItems.Add(SubjectName);items.SubItems.Add(TypeID);items.SubItems.Add(A);items.SubItems.Add(B);items.SubItems.Add(C);items.SubItems.Add(D);this.listExam.Items.Add(items);}}reader.Close();}catch (Exception ex){MessageBox.Show(ex.Message);}finally{db.closeData();}}/// <summary>/// listShow/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tscmbExamType_SelectedIndexChanged(object sender, EventArgs e) {if (this.listExam.Items.Count != 0){this.listExam.Items.Clear();}this.timerP.Stop();j = 600;this.toolStripLabel4.Text = "00";this.radioButton1.Enabled = false;this.radioButton2.Enabled = false;this.radioButton3.Enabled = false;this.radioButton4.Enabled = false;bel1.Enabled = false;bel2.Enabled = false;bel3.Enabled = false;bel4.Enabled = false;this.listExam.Enabled = false;this.rtbExam.Text = "";bel1.Text = "";bel2.Text = "";bel3.Text = "";bel4.Text = "";bel5.Text = "";bel6.Text = "";this.button1.Enabled = true;this.button2.Enabled = false;this.button3.Enabled = false;this.button4.Enabled = false;this.button5.Enabled = false;this.ListShow();}string TypeID = "";//类型号/// <summary>/// 显示题干和选项/// </summary>public void ShowExam(){if (this.listExam.SelectedItems.Count != 0){bel5.Text = this.listExam.SelectedItems[0].SubItems[0].Text;this.rtbExam.Text = this.listExam.SelectedItems[0].SubItems[1].Text;TypeID = this.listExam.SelectedItems[0].SubItems[2].Text;bel1.Text = this.listExam.SelectedItems[0].SubItems[3].Text;bel2.Text = this.listExam.SelectedItems[0].SubItems[4].Text;bel3.Text = this.listExam.SelectedItems[0].SubItems[5].Text;bel4.Text = this.listExam.SelectedItems[0].SubItems[6].Text;}else{return;}}/// <summary>/// 选项/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void listExam_SelectedIndexChanged(object sender, EventArgs e) {this.ShowExam();}private void button3_Click(object sender, EventArgs e){this.radioButton1.Checked = false;this.radioButton2.Checked = false;this.radioButton3.Checked = false;this.radioButton4.Checked = false;bel6.Text = "";for (int i = 0; i < listExam.Items.Count - 1; i++){if (listExam.Items[i].Selected) // 得到当前选中项,因为这个控件支持多选,所以只能通过遍历得到{if (i != 0 || i == 0){listExam.Items[i + 1].Selected = true;listExam.Select();break;}}}}/// <summary>/// 上一题/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){this.radioButton1.Checked = false;this.radioButton2.Checked = false;this.radioButton3.Checked = false;this.radioButton4.Checked = false;bel6.Text = "";for (int i = 0; i < listExam.Items.Count; i++){if (listExam.Items[i].Selected) // 得到当前选中项,因为这个控件支持多选,所以只能通过遍历得到{if (i != 0){listExam.Items[i - 1].Selected = true;listExam.Select();break;}}}}/// <summary>/// 所选答案/// </summary>public int InsertData(){string rightname = "";if (this.radioButton1.Checked == true){rightname = radioButton1.Text;}else if (this.radioButton2.Checked == true){rightname = radioButton2.Text;}else if (this.radioButton3.Checked == true){rightname = radioButton3.Text;}else if (this.radioButton4.Checked == true){rightname = radioButton4.Text;}int result = 0;string sql = string.Format(@"if exists(select AnswerName from ExamAnswer where AnswerName='{0}' and TypeID={1} and SubjectNo={2})beginif exists (select * from ExamRight where SubjectNo={3} and TypeID={4})beginupdate ExamRight set RightType =0, RightDate=GETDA TE(), RightName='{5}'where SubjectNo={6} and TypeID={7}endelsebegininsert into ExamRight (RightType,SubjectNo,TypeID,RightDate,RightName)values(0,{8},{9},GETDATE(),'{10}')endendelsebeginif exists (select * from ExamRight where SubjectNo={11} and TypeID={12})beginupdate ExamRight set RightType =1,RightDate=GETDATE(), RightName='{13}'where SubjectNo={14} and TypeID={15}endelsebegininsert into ExamRight (RightType,SubjectNo,TypeID,RightDate,RightName)values(1,{16},{17},GETDA TE(),'{18}')endend", rightname, TypeID, bel5.Text,bel5.Text, TypeID, rightname,bel5.Text, TypeID, bel5.Text,TypeID, rightname, bel5.Text,TypeID, rightname, bel5.Text,TypeID,bel5.Text,TypeID, rightname);SqlCommand comm = new SqlCommand(sql, db.Conn);try{db.openData();result = comm.ExecuteNonQuery();if (result != 0){bel6.Text = "答题成功,请继续答题!";bel6.ForeColor = Color.GreenYellow;return result;}else{bel6.Text = "答题失败,请重新答题!";bel6.ForeColor = Color.Red;return result;}}catch (Exception ex){MessageBox.Show(ex.Message);}finally{db.closeData();}return result;}/// <summary>/// 总计/// </summary>public void CountExam(){string sql = string.Format(@"select COUNT(*) as 总计from ExamRight where RightType in(select top 10 RightType from ExamRightwhere TypeID=(select TypeID from ExamPaperTypewhereExamPaperType.TypeName like '%{0}%')order by RightDate desc)and RightType=1 andTypeID=(select TypeID from ExamPaperTypewhereExamPaperType.TypeName like '%{1}%') ",this.tscmbExamType.Text.Substring(0, 3),this.tscmbExamType.Text.Substring(0, 3));try{db.openData();SqlCommand comm = new SqlCommand(sql, db.Conn);SqlDataReader reader = comm.ExecuteReader();if (!reader.HasRows){return;}else{while (reader.Read()){this.toolStripLabel6.Text = reader["总计"].ToString();}}reader.Close();}catch (Exception ex){MessageBox.Show(ex.Message);}finally{db.closeData();}}/// <summary>/// 提交试卷/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button5_Click(object sender, EventArgs e){this.button1.Enabled = false;this.button2.Enabled = false;this.button3.Enabled = false;this.button4.Enabled = false;this.timerP.Stop();this.radioButton1.Enabled = false;this.radioButton2.Enabled = false;this.radioButton3.Enabled = false;this.radioButton4.Enabled = false;bel1.Enabled = false;bel2.Enabled = false;bel3.Enabled = false;bel4.Enabled = false;this.listExam.Enabled = false;this.toolStripButton1.Enabled = true;this.CountExam();}private void radioButton1_Click(object sender, EventArgs e){InsertData();}private void radioButton2_Click(object sender, EventArgs e){InsertData();}private void radioButton3_Click(object sender, EventArgs e){InsertData();}private void radioButton4_Click(object sender, EventArgs e){InsertData();}/// <summary>/// 查看详情/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void toolStripButton1_Click(object sender, EventArgs e){FormMsg f = new FormMsg();f.Show();this.Hide();}private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) {Application.Exit();}private void 关于ToolStripMenuItem_Click(object sender, EventArgs e){FormAbout fa = new FormAbout();fa.Show();}private void FormExam_FormClosing(object sender, FormClosingEventArgs e){}}}次窗体////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace Exam{public partial class FormMsg : Form{public FormMsg(){InitializeComponent();}/// <summary>/// 调用数据库/// </summary>DataBase db = new DataBase();/// <summary>/// FormMsg/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void FormMsg_Load(object sender, EventArgs e){CenterToScreen();this.Width = 800;this.Height = 460;MaximizeBox = false;this.groupBox1.Text = "所答题目";this.groupBox1.BackColor = Color.Transparent;this.groupBox1.Width = 765;this.groupBox1.Height = 150;this.groupBox1.Left = 10;this.groupBox1.Top = 40;this.groupBox1.ForeColor = Color.White;this.Cursor = Cursors.Hand;this.listView1.GridLines = true;this.toolStripComboBox1.SelectedIndex = 0;this.toolStripLabel3.Text = "";this.groupBox2.Width = 765;this.groupBox2.Height = 200;this.groupBox2.Top = 200;this.groupBox2.Text = "所答题目";this.groupBox2.BackColor = Color.Transparent;this.groupBox2.ForeColor = Color.White;this.groupBox2.Left = 10;this.richTextBox1.ReadOnly = true;this.Text = "模拟考试系统";this.listView1.FullRowSelect = true;}/// <summary>/// 显示/// </summary>public void ShowList(){try{string sql = string.Format(@" select top 10 ExamPaper.SubjectNo,ExamPaper.SubjectName,ExamRight.TypeID,ExamAnswer.AnswerName,ExamRight.RightName,ExamRight.RightType,ExamRight.RightDate,ExamResult.A,ExamResult.B,ExamResult.C,ExamResult.DfromExamRightinner joinExamAnsweronExamAnswer.TypeID=ExamRight.TypeIDandExamAnswer.SubjectNo=ExamRight.SubjectNoinner joinExamPaperonExamPaper.TypeID=ExamRight.TypeIDandExamPaper.SubjectNo=ExamRight.SubjectNoinner joinExamResultonExamPaper.TypeID= ExamResult.TypeIDandExamPaper.SubjectNo= ExamResult.SubjectNowhere ExamRight.TypeID=(select TypeID from ExamPaperTypewhereExamPaperType.TypeName like '%{0}%')order by RightDate desc",this.toolStripComboBox1.Text.Substring(0, 3));db.openData();SqlCommand comm = new SqlCommand(sql, db.Conn);SqlDataReader reader = comm.ExecuteReader();if (!reader.HasRows){return;}else{while (reader.Read()){string SubjectNo = reader["SubjectNo"].ToString();int TypeID = int.Parse(reader["TypeID"].ToString());string AnswerName = reader["AnswerName"].ToString();string RightName = reader["RightName"].ToString();int RightType = int.Parse(reader["RightType"].ToString());string SubjectName = reader["SubjectName"].ToString();string RightDate = reader["RightDate"].ToString();string A = reader["A"].ToString();string B = reader["B"].ToString();string C = reader["C"].ToString();string D = reader["D"].ToString();ListViewItem items = new ListViewItem(SubjectNo);items.SubItems.Add(RightName);items.SubItems.Add(RightDate);if (TypeID == 1){items.SubItems.Add("计算机等级试题");}else if (TypeID == 2){items.SubItems.Add("四六级等级试题");}if (RightType == 0){items.SubItems.Add("√");}else if (RightType == 1){items.SubItems.Add("╳");}items.SubItems.Add(SubjectName);items.SubItems.Add(AnswerName);items.SubItems.Add(A);items.SubItems.Add(B);items.SubItems.Add(C);items.SubItems.Add(D);this.listView1.Items.Add(items);}}reader.Close();}catch (Exception ex){MessageBox.Show(ex.Message);}finally{db.closeData();}}/// <summary>/// LIST/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void listView1_SelectedIndexChanged(object sender, EventArgs e){if (this.listView1.SelectedItems.Count != 0){this.toolStripLabel3.Text = this.listView1.SelectedItems[0].SubItems[6].Text;this.richTextBox1.Text = this.listView1.SelectedItems[0].SubItems[5].Text +"\nA:"+this.listView1.SelectedItems[0].SubItems[7].Text+"\nB:"+this.listView1.SelectedItems[0].SubItems[8].Text+"\nC:"+this.listView1.SelectedItems[0].SubItems[9].Text+"\nD:"+this.listView1.SelectedItems[0].SubItems[10].Text;}}/// <summary>/// 选项/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) {this.toolStripLabel3.Text = "";this.richTextBox1.Text = "";this.listView1.Items.Clear();this.ShowList();}private void FormMsg_FormClosed(object sender, FormClosedEventArgs e){FormExam f = new FormExam();f.Show();}/// <summary>/// 清扫记录/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void toolStripButton1_Click(object sender, EventArgs e) {string sql = "";SqlCommand comm = null;if (this.toolStripComboBox1.Text == "计算机等级考试"){sql = "delete from ExamRight where TypeID=1 ";comm = new SqlCommand(sql, db.Conn);try{db.openData();int result = comm.ExecuteNonQuery();if (result != 0){this.listView1.Items.Clear();this.richTextBox1.Clear();this.toolStripLabel3.Text = "";ShowList();MessageBox.Show("清扫成功!");return;}else{return;}}catch (Exception ex){MessageBox.Show(ex.Message);}finally{db.closeData();}}else if (this.toolStripComboBox1.Text == "四六级等级考试") {sql = "delete from ExamRight where TypeID=2 ";comm = new SqlCommand(sql, db.Conn);try{db.openData();int result = comm.ExecuteNonQuery();if (result != 0){this.listView1.Items.Clear();ShowList();MessageBox.Show("清扫成功!");return;}else{return;}}catch (Exception ex){MessageBox.Show(ex.Message);}finally{db.closeData();}}}}}附:软件截图。

相关主题