?? mainform.cs
字號:
/// <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 + -