⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 一个网络版的考试系统
💻 CS
📖 第 1 页 / 共 3 页
字号:
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button5_Click(object sender, System.EventArgs e) {
			this.BK_Select();
		}
		#endregion

		#region Windows 必考题事件执行代码
		/// <summary>
		/// 必考题按钮事件执行代码
		/// </summary>
		private void BK_Select(){

			this.examType = "必考题";
			this.ClearLabel();

			if(this.isW){  //如果第二次按下

				this.button101.Text    = "(必考)随机抽题";
				this.button101.ForeColor = Color.Black;
				this.button102.Enabled = true;
				this.button103.Enabled = true;
				this.button104.Enabled = true;

				this.button31.Enabled  = true;
				this.button32.Enabled  = true;
				this.button33.Enabled  = true;
				this.button34.Enabled  = true;

				this.isW = false;

				//this.isW = false;
				//this.timer.Stop();

				if(this.softType == "SERVER" && !this.isSelectExam){

					this.timer.Stop();
					this.cuMSG = "{{{UN_BK_SELECT}}}XXXXX";
					this.SendToClient();

					this.isSelectExam = false;

					MessageBox.Show("已取消应试者选题操作!");
					return;

				}//else{
				//	this.button101.Visible = false;
				//	this.ipStr.Clear();
				//	this.ipStr.Add(this.serverIP + "->");
				//	this.cuMSG = "{{{YES_BK_SELECT}}}XXXXX";
				//	this.SendToClient();
				//}
				
			}else{  //第一次按下

				if(this.softType == "SERVER"){

					SelectClient sc = new SelectClient(this);

					if(this.con){

						//清空待通讯IP记录
						sc.ShowDialog();

						if(this.ipStr.Count > 0){

							this.button102.Enabled = false;
							this.button103.Enabled = false;
							this.button104.Enabled = false;
							this.button101.ForeColor = Color.Blue;

							this.cuMSG = "{{{BK_SELECT}}}XXXXX";
							this.SendToClient();
							this.button101.Text    = "(必考)取消选题";

							this.isW = true;
							this.timer.Start();
						}

					}else{
						this.con = true;
						return;
					}

				}else{

					this.button101.Visible  = true;
					this.button101.Location = new Point(776, 656);
					this.button101.Size     = new Size(184, 40);
					this.button101.Text    = "确定考题";

					this.isW = true;
					this.timer.Start();
				}
			}
		}
		#endregion

		#region 通知客户端已停止选题操作
		/// <summary>
		/// 
		/// </summary>
		private void AlertStopSelectExam(){
			//
		}
		#endregion

		#region 清空界面按钮事件
		/// <summary>
		/// 清空界面
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button5_Click_1(object sender, System.EventArgs e) {
			this.ClearLabel();
		}
		#endregion

		#region 清空界面窗体代码
		/// <summary>
		/// 清空界面窗体代码
		/// </summary>
		private void ClearLabel(){

			if(this.softType == "SERVER"){
				this.cuMSG  = "{{{CLEARFORM}}}XXX";
				this.SendToClient();
			}

			this.a.Text = "";
			this.q.Text = "";
		}
		#endregion

		#region 窗体初始化后载入,打开端口,监听来自客户端的访问
		/// <summary>
		/// 窗体初始化后载入
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MainForm_Load(object sender, System.EventArgs e) {

			try{

				Application.AddMessageFilter(new KeyFilterHander());

				if(this.localIP == ""){
					MessageBox.Show("建立服务器连接必须提供相应的IP设置!");
					if(this.softType == "SERVER"){
						SystemSet ss = new SystemSet(this);
						ss.ShowDialog();
					}else{
						SystemSet_ ss = new SystemSet_(this);
						ss.ShowDialog();
					}
					this.CheckLocalIP();
				}

				this.ip__     = IPAddress.Parse(this.localIP);
				this.po__     = new IPEndPoint(this.ip__,21);

				socket__      = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

				socket__.Bind(po__);
				socket__.Listen(21);
			
				Thread t       = new Thread(new ThreadStart(ListenServer));
				t.IsBackground = true;
				t.Start();

			}catch(Exception ex){
				MessageBox.Show(ex.Message.ToString());
			}
		}
		#endregion

		#region 得到指定位数的随机数
		/// <summary>
		/// 得到int_Count位随机数
		/// </summary>
		/// <param name="int_Count"></param>
		/// <returns></returns>
		public string GetRandom(int int_Count) {
			string str_RV="1";
			for (int i=0;i<int_Count-2;i++) {
				str_RV=str_RV+"0";
			}
			Random r=new Random();
			str_RV=r.Next(int.Parse(str_RV)).ToString();
			int int_Count1=int_Count-str_RV.Length;
			if (int_Count1>0) {
				for (int i=0;i<int_Count1;i++) {
					str_RV=str_RV+"0";
				}
			}
			return str_RV.Substring(0,str_RV.Length-2);
		}
		#endregion

		#region 作废代码
		/// <summary>
		/// 作废代码
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void a_Click(object sender, System.EventArgs e) {
		
		}
		#endregion


		/// <summary>
		/// 循环赋值到问题框
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void timer_Tick(object sender, System.EventArgs e) {

			if(!this.isW){

				this.timer.Stop();
				this.q.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

				if(this.softType== "SERVER"){
					this.GetExamItem();
					this.button31.Enabled = true;
					this.button32.Enabled = true;
					this.button33.Enabled = true;
					this.button34.Enabled = true;
					this.button105.Enabled = true;
				}

			}else{

				if(this.softType == "SERVER"){
					this.button31.Enabled = false;
					this.button32.Enabled = false;
					this.button33.Enabled = false;
					this.button34.Enabled = false;
					this.button105.Enabled = false;
				}

				this.q.Font = new System.Drawing.Font("宋体",120F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
				this.q.Text = GetRandom(10);
				//this.q.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

				//this.cuMSG = "{{{RANDOM_COUNT}}}" + this.q.Text;
				//this.SendToClient();
			}
		}

		/// <summary>
		/// 试题管理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button31_Click(object sender, System.EventArgs e) {
			ExamItemMan eim = new ExamItemMan();
			eim.ShowDialog();
		}

		/// <summary>
		/// 应试者管理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button32_Click(object sender, System.EventArgs e) {
			PersonSet ps = new PersonSet(null);
			ps.ShowDialog();
		}

		/// <summary>
		/// 系统设置
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button33_Click(object sender, System.EventArgs e) {
			if(!this.keySpace){
				if(this.softType == "SERVER"){
					SystemSet ss = new SystemSet(this);
					ss.ShowDialog();
				}else{
					SystemSet_ ss = new SystemSet_(this);
					ss.ShowDialog();
				}
			}
			this.ksp.Focus();
		}

		/// <summary>
		/// 抢答题
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button102_Click(object sender, System.EventArgs e) {
			
			this.examType = "抢答题";
			this.ClearLabel();
			this.isAlert = true;

			//装入全部IP地址
			this.LoadAllIP();

			if(this.isW){
				this.button102.Text    = "(抢答)随机抽题";
				this.button102.ForeColor = Color.Black;
				this.button101.Enabled = true;
				this.button103.Enabled = true;
				this.button104.Enabled = true;
				this.isW   = false;
				this.alert = true;
				this.timeOutCon.Interval = this.timeOut * 1000 ;
				this.timeOutCon.Start();
			}else{

				this.button101.Enabled = false;
				this.button103.Enabled = false;
				this.button104.Enabled = false;
				this.button102.ForeColor = Color.Blue;
				this.button102.Text    = "(抢答)固定考题";

				this.cuMSG = "{{{RANDOM}}}XXX";
				this.SendToClient();

				this.isW = true;
				this.timer.Start();
			}
		}

		//载入全部客户机的IP地址
		private void LoadAllIP(){
			this.ipStr.Clear();
			int  c = 0;
			for(int i=0;i<this.clientIPs.Length;i++){
				if(this.clientIPs[i].ToString() != ""){
					string ipStr_ = this.clientIPs[i].ToString();
					ipStr_ += ("->" + this.clieckNas[i].ToString());
					this.ipStr.Add(ipStr_);
					c ++ ;
				}
			}
		}

		/// <summary>
		/// 自选
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button103_Click(object sender, System.EventArgs e) {

			this.ClearLabel();

			this.cuMSG    = "{{{SELECTFEN}}}XXXXXX";

			SelectClient sc = new SelectClient(this);
			if(this.con){

				sc.ShowDialog();

				this.examType = "自选题";
				this.button101.Enabled = false;
				this.button102.Enabled = false;
				this.button104.Enabled = false;
				this.button105.Enabled = false;

				this.button103.ForeColor = Color.Blue;
				this.SendToClient();

			}else{
				return;
				this.con = true;
			}
		}

		/// <summary>
		/// 随机抽取试题
		/// </summary>
		private void GetExamItem(){

			string sql = "select * from ExamItem where examType='"+ this.examType +"' and state='可用'";
			this.conn.Open();
			OleDbDataAdapter odp = new OleDbDataAdapter(sql,conn);
			DataSet ds = new DataSet();
			odp.Fill(ds,"ExamItem");
			this.conn.Close();
			if(ds.Tables[0].Rows.Count > 0){

				//随机取
				Random random = new Random();
				int ret = random.Next(1,ds.Tables[0].Rows.Count+1);

				DataRow row      = ds.Tables[0].Rows[ret-1];
				this.currentExam = row;
				this.q.Text      = row["qStr"].ToString();
				if(this.softType == "SERVER"){
					//this.a.Text  = row["aStr"].ToString();
				}
				if(this.examType == "抢答题"){
					this.cuMSG       = "{{{QUESTION_QUICK}}}" + this.q.Text;
				}else{
					this.cuMSG       = "{{{QUESTION}}}" + this.q.Text;
				}

				this.SendToClient();

				sql = "update ExamItem set state = '作废' where eID=" + row["eID"].ToString();
				this.conn.Open();
				OleDbCommand odc = new OleDbCommand(sql,conn);
				odc.ExecuteNonQuery();
				this.conn.Close();

			}else{
				this.timeOutCon.Stop();
				MessageBox.Show("题库中已没有可用的试题,请在[管理试题]功能窗口中重置所有试题或者新加入试题!");
				return;
			}
		}

		/// <summary>
		/// 循环所有IP发送数据包到客户机
		/// </summary>
		public void SendToClient(){

			for(int i=0;i<this.ipStr.Count;i++){

				try{

					string ip_     = this.ipStr[i].ToString();
					string _ip_    = PublicApplication.GetFormatStr(ip_,"->","start");
					this.ip        = IPAddress.Parse(_ip_);
					this.po        = new IPEndPoint(this.ip,21);

					socket         = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

					socket.Connect(po);
					if (socket.Connected){
						Byte[] fsbyte=	System.Text.Encoding.Unicode.GetBytes(this.cuMSG);
						socket.Send(fsbyte,fsbyte.Length,0);
					}

				}catch(Exception ex){

					this.timer.Stop();
					MessageBox.Show("传送试题时出错:"+ex.Message.ToString()+"\n\n请检查所有客户端设置是否正确!");
					return;
				}
			}
		}

		/// <summary>
		/// 查看答案
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button105_Click(object sender, System.EventArgs e) {
			if(this.currentExam != null){
				this.a.Text = this.currentExam["aStr"].ToString();
				this.cuMSG  = "{{{ANSWER}}}" + this.a.Text;
				this.SendToClient();
			}
		}

		/// <summary>
		/// 连接到客户端
		/// </summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -