Beispiel #1
0
        public void AddPanel()
        {
            StuDB stu = new StuDB();

            ds = stu.ExamQuestions();
            DataSet ExamInfo = stu.ExamInfo();
            int     count    = 0;
            string  name     = "pan";

            #region 试卷头部

            //试卷头部最外层panel
            Panel panHead = new Panel();
            panHead.Name      = "pan_Head";
            panHead.Width     = 1519;
            panHead.Height    = 150;
            panHead.BackColor = Color.FromArgb(192, 255, 255);
            this.Controls.Add(panHead);

            //添加倒计时控件
            panHead.Controls.Add(label1);

            //添加标题部分
            Label examName = new Label();
            examName.AutoSize = false;
            examName.Width    = 350;
            examName.Height   = 50;
            examName.Font     = new System.Drawing.Font("微软雅黑", 25, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            examName.Location = new Point(600, 10);//第二个panel位置
            examName.Text     = ExamInfo.Tables["Examination_list"].Rows[UserInfo.ExamName][1].ToString();
            panHead.Controls.Add(examName);

            //提交按钮
            Button btn = new Button();
            btn.Name     = "btn_Submit";
            btn.Text     = "提交";
            btn.Width    = 86;
            btn.Height   = 38;
            btn.Location = new Point(1000, 100);
            //btn.Anchor = AnchorStyles.Top|AnchorStyles.Right;
            btn.Font   = new System.Drawing.Font("微软雅黑", 13, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            btn.Click += new System.EventHandler(button1_Click);
            panHead.Controls.Add(btn);

            //考生姓名
            Label lab = new Label();
            lab.Width    = 250;
            lab.Height   = 30;
            lab.Font     = new System.Drawing.Font("微软雅黑", 22, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
            lab.Text     = "考生姓名:" + UserInfo._StuName;
            lab.Location = new Point(30, 50);
            panHead.Controls.Add(lab);

            //考生学号
            Label lab2 = new Label();
            lab2.Width    = 250;
            lab2.Height   = 30;
            lab2.Font     = new System.Drawing.Font("微软雅黑", 22, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
            lab2.Text     = "考生学号:" + UserInfo._LogId;
            lab2.Location = new Point(30, 100);
            panHead.Controls.Add(lab2);

            //考试科目
            Label lab3 = new Label();
            lab3.Width    = 250;
            lab3.Height   = 30;
            lab3.Font     = new System.Drawing.Font("微软雅黑", 22, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
            lab3.Text     = "考试科目:" + UserInfo._Examsuject;
            lab3.Location = new Point(600, 100);
            panHead.Controls.Add(lab3);

            #endregion

            #region 试卷题部分

            //添加选择题标题
            for (int i = 0; i < ds.Tables["TheQuestionBank"].Rows.Count; i++)
            {
                if (ds.Tables["TheQuestionBank"].Rows[i][2].ToString().Equals("选择题"))
                {
                    Label examName1 = new Label();
                    examName1.AutoSize = false;
                    examName1.Width    = 300;
                    examName1.Height   = 28;
                    examName1.Font     = new System.Drawing.Font("微软雅黑", 17, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    examName1.Location = new Point(250, 170);//第二个panel位置
                    examName1.Text     = "选择题";
                    this.Controls.Add(examName1);
                    break;
                }
            }

            //选择题部分
            for (int i = 0; i < ds.Tables["TheQuestionBank"].Rows.Count; i++)
            {
                if (ds.Tables["TheQuestionBank"].Rows[i][2].ToString().Equals("选择题"))
                {
                    //panle部分
                    Panel pan = new Panel();
                    pan.Name = name + i;
                    //pan.Text = name + i;
                    pan.Width  = 2000;
                    pan.Height = 200;
                    //pan.BackColor = Color.Red;
                    pan.Location = new Point(300, 200 + count * 200);//第二个panel位置
                    RadioButton aa = new RadioButton();
                    aa.Text = "sca";
                    aa.Name = "aaa" + 1;

                    count++;
                    //题干部分
                    Label questionTxt = new Label();
                    questionTxt.AutoSize = false;
                    questionTxt.Width    = 2000;
                    questionTxt.Text     = count + "、" + ds.Tables["TheQuestionBank"].Rows[i][3].ToString() + "(" + ds.Tables["TheQuestionBank"].Rows[i][6].ToString() + "分)";//把值赋给lable
                    questionTxt.Font     = new System.Drawing.Font("微软雅黑", 13, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    //选项部分
                    List <RadioButton> radioButton = new List <RadioButton>();//button控件
                    RadioButton        anwer       = null;

                    int Y = 0;

                    //答案选项文本
                    string   CorrentAnswer       = ds.Tables["TheQuestionBank"].Rows[i][4].ToString();
                    string   WrongAnswer         = ds.Tables["TheQuestionBank"].Rows[i][5].ToString();
                    string[] CorrentAnswerSingle = CorrentAnswer.Split(',');                        //正确答案文本
                    string[] WrongAnswerSingle   = WrongAnswer.Split(',');                          //错误答案文本

                    for (int j = 0; j < CorrentAnswerSingle.Length + WrongAnswerSingle.Length; j++) //条件为答案个数  添加radiobutton控件
                    {
                        Y             += 30;
                        anwer          = new RadioButton();
                        anwer.AutoSize = false;
                        anwer.Width    = 1000;
                        anwer.Name     = "rdo" + j;
                        anwer.Location = new Point(50, 20 + Y);
                        radioButton.Add(anwer);
                    }

                    List <int> random = new List <int>();//存随机数的
                    for (int h = 0; h < CorrentAnswerSingle.Length; h++)
                    {
                        int suiji = new Random().Next(0, CorrentAnswerSingle.Length + CorrentAnswerSingle.Length);
                        random.Add(suiji);
                        radioButton[suiji].Text = CorrentAnswerSingle[h];
                        radioButton[suiji].Font = new System.Drawing.Font("微软雅黑", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    }

                    for (int j = 0; j < WrongAnswerSingle.Length; j++)
                    {
                        for (int g = 0; g < radioButton.Count; g++)
                        {
                            if (radioButton[g].Text == "")
                            {
                                radioButton[g].Text = WrongAnswerSingle[j];
                                radioButton[g].Font = new System.Drawing.Font("微软雅黑", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                                break;
                            }
                        }
                    }

                    //调用添加标签控件的方法

                    pan.Controls.Add(questionTxt);

                    for (int k = 0; k < radioButton.Count; k++)//答案的个数
                    {
                        pan.Controls.Add(radioButton[k]);
                    }
                    this.Controls.Add(pan);
                }
            }


            //填空题标题
            for (int i = 0; i < ds.Tables["TheQuestionBank"].Rows.Count; i++)
            {
                if (ds.Tables["TheQuestionBank"].Rows[i][2].ToString().Equals("填空题"))
                {
                    Label examName2 = new Label();
                    examName2.AutoSize = false;
                    examName2.Width    = 300;
                    examName2.Height   = 28;
                    examName2.Font     = new System.Drawing.Font("微软雅黑", 17, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    examName2.Location = new Point(250, 200 + count * 200);//第二个panel位置
                    examName2.Text     = "填空题";
                    this.Controls.Add(examName2);
                    break;
                }
            }

            //填空题部分
            for (int i = 0; i < ds.Tables["TheQuestionBank"].Rows.Count; i++)
            {
                if (ds.Tables["TheQuestionBank"].Rows[i][2].ToString().Equals("填空题"))
                {
                    //panle部分
                    Panel pan = new Panel();
                    pan.Name = name + i;
                    //pan.Text = name + i;
                    pan.Width  = 2000;
                    pan.Height = 200;
                    //pan.BackColor = Color.Red;
                    pan.Location = new Point(300, 240 + count * 200);//第二个panel位置
                    RadioButton aa = new RadioButton();
                    aa.Text = "sca";
                    aa.Name = "aaa" + 1;

                    count++;
                    //题干部分
                    Label questionTxt = new Label();
                    questionTxt.AutoSize = false;
                    questionTxt.Width    = 2000;
                    questionTxt.Text     = count + "、" + ds.Tables["TheQuestionBank"].Rows[i][3].ToString() + "(" + ds.Tables["TheQuestionBank"].Rows[i][6].ToString() + "分)";//把值赋给lable
                    questionTxt.Font     = new System.Drawing.Font("微软雅黑", 13, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    //答题的地方
                    TextBox txtAnswer = new TextBox();
                    txtAnswer.Width     = 300;
                    txtAnswer.Height    = 100;
                    txtAnswer.AutoSize  = false;
                    txtAnswer.Multiline = true;
                    txtAnswer.Location  = new Point(50, 50);
                    txtAnswer.Font      = new System.Drawing.Font("微软雅黑", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    txtAnswer.BackColor = Color.Yellow;
                    string   CorrentAnswer       = ds.Tables["TheQuestionBank"].Rows[i][4].ToString();
                    string[] CorrentAnswerSingle = CorrentAnswer.Split(',');//正确答案文本

                    pan.Controls.Add(txtAnswer);
                    pan.Controls.Add(questionTxt);
                    this.Controls.Add(pan);
                }
            }


            //解答题标题
            for (int i = 0; i < ds.Tables["TheQuestionBank"].Rows.Count; i++)
            {
                if (ds.Tables["TheQuestionBank"].Rows[i][2].ToString().Equals("解答题"))
                {
                    Label examName3 = new Label();
                    examName3.AutoSize = false;
                    examName3.Width    = 300;
                    examName3.Height   = 28;
                    examName3.Font     = new System.Drawing.Font("微软雅黑", 17, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    examName3.Location = new Point(250, 240 + count * 200);//第二个panel位置
                    examName3.Text     = "解答题";
                    this.Controls.Add(examName3);
                    break;
                }
            }
            //解答题部分
            for (int i = 0; i < ds.Tables["TheQuestionBank"].Rows.Count; i++)
            {
                if (ds.Tables["TheQuestionBank"].Rows[i][2].ToString().Equals("解答题"))
                {
                    //panle部分
                    Panel pan = new Panel();
                    pan.Name = name + i;
                    //pan.Text = name + i;
                    pan.Width  = 2000;
                    pan.Height = 200;
                    //pan.BackColor = Color.Red;
                    pan.Location = new Point(300, 280 + count * 200);//第二个panel位置
                    RadioButton aa = new RadioButton();
                    aa.Text = "sca";
                    aa.Name = "aaa" + 1;

                    count++;
                    //题干部分
                    Label questionTxt = new Label();
                    questionTxt.AutoSize = false;
                    questionTxt.Width    = 2000;
                    questionTxt.Text     = count + "、" + ds.Tables["TheQuestionBank"].Rows[i][3].ToString() + "(" + ds.Tables["TheQuestionBank"].Rows[i][6].ToString() + "分)";
                    questionTxt.Font     = new System.Drawing.Font("微软雅黑", 13, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    //解答题答题部分
                    TextBox txtAnswer = new TextBox();
                    txtAnswer.Width     = 300;
                    txtAnswer.Height    = 100;
                    txtAnswer.AutoSize  = false;
                    txtAnswer.Multiline = true;
                    txtAnswer.Location  = new Point(50, 50);
                    txtAnswer.Font      = new System.Drawing.Font("微软雅黑", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    txtAnswer.BackColor = Color.Yellow;

                    pan.Controls.Add(txtAnswer);
                    pan.Controls.Add(questionTxt);
                    this.Controls.Add(pan);
                }
            }
        }