Exemplo n.º 1
0
        private void gvCauHoi_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            pnLuaChon.Controls.Clear();
            if (flag)
            {
                rtbNoiDungCH.Text = string.Empty;
                int maCH = Int32.Parse(((DataRowView)cAUHOIBindingSource.Current)[0].ToString().Trim());
                if (maCH == 0)
                {
                    return;
                }

                CAUHOI         chRow = CauHoi.GetCauHoiByMaCH(maCH);
                List <LUACHON> lstLC = CauHoi.GetLuaChonByMaCH(maCH);

                rtbNoiDungCH.AppendText(chRow.NOIDUNG);
                lstLC.OrderBy(x => x.MALC);
                foreach (LUACHON lc in lstLC)
                {
                    RichTextBox rtb = new RichTextBox();

                    rtb.Multiline = true;
                    rtb.Text      = "Câu " + lc.MALC + " : " + lc.NOIDUNG;
                    rtb.Dock      = DockStyle.Top;
                    if (chRow.DAPAN.Equals(lc.MALC))
                    {
                        rtb.BackColor = Color.Green;
                    }

                    pnLuaChon.Controls.Add(rtb);
                    pnLuaChon.AutoScroll = true;
                    pnLuaChon.FireScrollEventOnMouseWheel = true;
                }
            }
        }
Exemplo n.º 2
0
        private void btnTest_Click(object sender, EventArgs e)
        {
            List <BODE> listBD = new List <BODE>();

            if (!CheckData())
            {
                return;
            }

            int demCauHoi = CauHoi.CountCauHoiByMonHoc(MaMH, MaTD);

            if (demCauHoi < txtSoCauThi.Value)
            {
                MessageBox.Show("Số câu hỏi không đủ để tạo đề thi, môn học này chỉ có " + demCauHoi + " câu hỏi!", "Thông báo", MessageBoxButtons.OK);
                return;
            }
            lstMaCH = BoDe.LayCauHoiTaoBD(MaMH, MaKhoa, MaTD, (int)txtSoCauThi.Value);
            if (lstMaCH.Count == 0)
            {
                MessageBox.Show("Đã xảy ra lỗi khi tạo bộ đề thi, vui lòng thử lại sau!", "Thông báo", MessageBoxButtons.OK);
                return;
            }
            FormXemTruocBoDe frmXemTruocBD = new FormXemTruocBoDe(MaKhoa, MaTD, cbbMonHoc.Text, lstMaCH, (int)txtThoiGianThi.Value);

            frmXemTruocBD.Show();
        }
Exemplo n.º 3
0
        public void SetData()
        {
            if (LISTMACH.Count == 0)
            {
                return;
            }
            lblTenMonHoc.Text = "XE TRƯỚC BỘ ĐỀ MÔN : " + TTENMH;
            KHOA kh = Khoa.GetKhoaByMaKhoa(MAKHOA);

            lblTenKhoa.Text    = "KHOA : " + kh.TENKH;
            lblTrinhDo.Text    = TrinhDo.GetTDByMaTD(MATRINHDO).TENTD;
            lblThongTinBD.Text = "Thời gian : " + THOIGIANTHI.ToString() + " phút";

            RichTextBox    rtbCau = new RichTextBox();
            CAUHOI         ch     = null;
            List <LUACHON> lc     = null;

            for (int i = 0; i < LISTMACH.Count; i++)
            {
                ch = CauHoi.GetCauHoiByMaCH(LISTMACH[i]);
                if (rtbCau.Text.Trim() == string.Empty)
                {
                    rtbCau.Text = "Câu " + (i + 1).ToString() + " : ";
                    rtbCau.AppendText(Environment.NewLine + "    " + ch.NOIDUNG);
                }
                else
                {
                    rtbCau.AppendText(Environment.NewLine + "Câu " + (i + 1).ToString() + " : " + ch.NOIDUNG);
                }
                lc = CauHoi.GetLuaChonByMaCH(LISTMACH[i]);
                for (int j = 0; j < lc.Count; j++)
                {
                    rtbCau.AppendText(Environment.NewLine + "     Câu " + KeyConst.LUACHON[j] + " : " + lc[j].NOIDUNG);
                }
            }
            rtbCau.Dock = DockStyle.Fill;
            FontStyle newFontStyle = (FontStyle)(FontStyle.Bold);

            rtbCau.Font           = new Font("Times New Rowman", 25, newFontStyle);
            pnBaiThi.AutoScroll   = true;
            rtbCau.SelectionStart = 0;
            pnBaiThi.Controls.Add(rtbCau);
        }
Exemplo n.º 4
0
        void Laydethi()
        {
            int           sttMang = 0;
            string        lenh    = "EXEC SP_LAYDETHI '" + lbMalop.Text + "','" + lbMonthi.Text + "','" + lbLanthi.Text + "','" + Program.Macs + "'";
            SqlDataReader rd;

            // SP trả về thứ tự các Field :
            // 0 CAUHOI 1 MAMH 2 TRINHDO 3 NOIDUNG 4A 5B 6C 7D 8DAPAN 9MAGV
            try
            {
                rd = Program.ExecSqlDataReader(lenh);
                // Ghi toàn bộ câu hỏi được trả về vào 1 mảng
                if (rd.HasRows)
                {
                    for (sttMang = 0; sttMang < Program.Socauhoithi; sttMang++)
                    {
                        rd.Read();
                        int    sttBode  = rd.GetInt32(0);
                        String noidung  = rd.GetString(3);
                        String a        = rd.GetString(4);
                        String b        = rd.GetString(5);
                        String c        = rd.GetString(6);
                        String d        = rd.GetString(7);
                        String strDapan = rd.GetString(8);
                        int    dapAn    = 0;
                        if (strDapan == "A")
                        {
                            dapAn = 1;
                        }
                        else if (strDapan == "B")
                        {
                            dapAn = 2;
                        }
                        else if (strDapan == "C")
                        {
                            dapAn = 3;
                        }
                        else if (strDapan == "D")
                        {
                            dapAn = 4;
                        }
                        else
                        {
                            dapAn = 0;
                        }
                        // tạo câu hỏi
                        dscauhoi[sttMang] = new CauHoi(sttBode, noidung, a, b, c, d, dapAn, null);
                        //MessageBox.Show(noidung);
                    }
                    rd.Close();
                }
                else
                {
                    MessageBox.Show("Kết quả trả về rỗng !!!", "", MessageBoxButtons.OK);
                    rd.Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi gọi thực hiện truy vấn SQL " + ex.Message, "", MessageBoxButtons.OK);
                return;
            }
        }
Exemplo n.º 5
0
        private void FormThi_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dtsTTN.CT_BAITHI' table. You can move, or remove it, as needed.
            this.cT_BAITHITableAdapter.Fill(this.dtsTTN.CT_BAITHI);
            bANGDIEMTableAdapter.Fill(this.dtsTTN.BANGDIEM);
            MyTimer.Interval = (1000); // 1 mins
            MyTimer.Tick    += new EventHandler(t_Tick);
            MyTimer.Start();

            if (MaBD != -1)
            {
                BDThiTam = BoDe.LayBDThi(MaBD);
                if (BDThi == null)
                {
                    return;
                }
                CAUHOITHI = new List <HoTroLayCauHoiThi>();
                HoTroLayCauHoiThi htCHThi  = null;
                Panel             pnCauHoi = null;
                int i = BDThiTam.Count;
                while (BDThiTam.Count != 0)
                {
                    htCHThi = new HoTroLayCauHoiThi();
                    int           r  = rnd.Next(BDThiTam.Count);
                    HoTroLayBDThi ht = BDThiTam[r];
                    BDThi.Add(ht);
                    CAUHOI         cauHoi  = CauHoi.GetCauHoiByMaCH(ht.MaCauHoi);
                    List <LUACHON> luachon = CauHoi.GetLuaChonByMaCH(ht.MaCauHoi);

                    pnCauHoi          = new Panel();
                    pnCauHoi.AutoSize = true;
                    pnCauHoi.Dock     = DockStyle.Top;

                    Label lb = new Label();
                    lb.AutoSize = true;
                    FontStyle newFontStyle = (FontStyle)(FontStyle.Bold);
                    lb.Font = new Font("Times New Rowman", 25, newFontStyle);
                    lb.Text = "Câu " + i + " : " + cauHoi.NOIDUNG;

                    RadioButton rGLuaChon = null;

                    foreach (LUACHON lc in luachon)
                    {
                        rGLuaChon          = new RadioButton();
                        rGLuaChon.AutoSize = true;
                        rGLuaChon.Name     = lc.MALC.Trim() + "-" + cauHoi.MACH.ToString().Trim();
                        rGLuaChon.Font     = new Font("Times New Rowman", 25, newFontStyle);
                        // update dap an neu thay doi lua chon
                        rGLuaChon.CheckedChanged += new EventHandler(LuaChon_Click);
                        //rGLuaChon.Add(new RadioGroupItem(lc.MALC.ToString(), "Câu " + lc.MALC.ToString() + " : " + lc.NOIDUNG));
                        rGLuaChon.Text = "Câu " + lc.MALC.ToString() + " : " + lc.NOIDUNG;
                        rGLuaChon.Dock = DockStyle.Bottom;
                        rGLuaChon.Focus();
                        pnCauHoi.Controls.Add(rGLuaChon);
                    }

                    lb.Dock = DockStyle.Top;
                    pnCauHoi.Controls.Add(lb);
                    pnThi.Controls.Add(pnCauHoi);

                    htCHThi.MaCH      = cauHoi.MACH;
                    htCHThi.STT       = ht.SoTT;
                    htCHThi.DapAn     = cauHoi.DAPAN;
                    htCHThi.STTKhiThi = i - 1;

                    CAUHOITHI.Add(htCHThi);
                    BDThiTam.RemoveAt(r);
                    i--;
                }
            }
            pnThi.AllowDrop  = true;
            pnThi.AutoScroll = true;
        }
        public QuanLyCauHoi()
        {
            InitializeComponent();

            loadCBCauHoi();
            loadDGVDapAn();

            set();

            btnThemCauHoi.Click += (s, e) =>
            {
                using (var qlttn = new QLTTNDataContext())
                {
                    if (qlttn.CauHois.Where(ch => ch.NoiDung.ToLower() == txtCauHoi.Text.ToLower()).Count() != 0)
                    {
                        MessageBox.Show("Câu hỏi này đã có trong danh sách. Xin mời tạo câu hỏi mới", "Trùng record", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    // tìm thử xem có câu hỏi nào chưa có đáp án hay không, nếu như có thì ngừng việc
                    // thêm câu hỏi mới mà yêu cầu người dùng thêm đáp án cho câu hỏi đó
                    var cauhoi = qlttn.CauHois.Where(ch => ch.DapAns == null || ch.DapAns.Count < 2).FirstOrDefault();
                    if (cauhoi != null)
                    {
                        MessageBox.Show($"Xin mời nhập tối thiểu 2 đáp án cho câu hỏi sau trước khi thêm câu hỏi mới:{Environment.NewLine} <{cauhoi.NoiDung}>", "Câu hỏi chưa có đáp án", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }
                    qlttn.CauHois.InsertOnSubmit(new CauHoi()
                    {
                        NoiDung = txtCauHoi.Text,
                        CapDo   = qlttn.CapDos.Where(cd => cd.maCD == int.Parse(cbCapDo.SelectedValue.ToString())).SingleOrDefault()
                    });
                    qlttn.SubmitChanges();
                }
                loadCBCauHoi();
                cbDSCH.SelectedItem = cbDSCH.Items[cbDSCH.Items.Count - 1];
                txtDapAn.Focus();
            };
            btnXoaCauHoi.Click += (s, e) =>
            {
                using (var qlttn = new QLTTNDataContext())
                {
                    if (qlttn.CauHois.Count() <= 1)
                    {
                        MessageBox.Show("Không thể xóa vì cần phải có ít nhất một câu hỏi trong Database", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }
                    var cauHoiHienTai = qlttn.CauHois
                                        .Where(ch => ch.maCH == int.Parse(cbDSCH.SelectedValue.ToString()))
                                        .FirstOrDefault();
                    qlttn.DapAns.DeleteAllOnSubmit(cauHoiHienTai.DapAns);
                    qlttn.CauHois.DeleteOnSubmit(cauHoiHienTai);
                    qlttn.SubmitChanges();
                }
                loadCBCauHoi();
            };
            btnSuaCauHoi.Click += (s, e) =>
            {
                using (var qlttn = new QLTTNDataContext())
                {
                    if (qlttn.CauHois.Where(ch => ch.NoiDung.ToLower() == txtCauHoi.Text.ToLower()).Count() != 0)
                    {
                        MessageBox.Show("Câu hỏi này đã có trong danh sách. Xin mời tạo câu hỏi mới", "Trùng record", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    var cauHoiHienTai = qlttn.CauHois
                                        .Where(ch => ch.maCH == int.Parse(cbDSCH.SelectedValue.ToString()))
                                        .FirstOrDefault();

                    cauHoiHienTai.NoiDung = txtCauHoi.Text;
                    cauHoiHienTai.CapDo   = qlttn.CapDos.Where(cd => cd.maCD == int.Parse(cbCapDo.SelectedValue.ToString())).FirstOrDefault();
                    qlttn.SubmitChanges();
                }
                loadCBCauHoi();
                cbDSCH.SelectedItem = cbDSCH.Items[cbDSCH.Items.Count - 1];
            };
            btnThemDapAn.Click += (s, e) =>
            {
                using (var qlttn = new QLTTNDataContext())
                {
                    var cauHoiHienTai = qlttn.CauHois
                                        .Where(ch => ch.maCH == int.Parse(cbDSCH.SelectedValue.ToString()))
                                        .FirstOrDefault();
                    if (cauHoiHienTai.DapAns.Where(da => da.NoiDung.ToLower() == txtDapAn.Text.ToLower()).Count() != 0)
                    {
                        MessageBox.Show("Đáp án này đã có trong danh sách. Xin mời tạo đáp án mới", "Lỗi trùng dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }
                    if (cauHoiHienTai.DapAns.Count >= 6)
                    {
                        MessageBox.Show("Mỗi câu hỏi có tối đa 6 đáp án. Xin mời xóa bớt đáp án để thêm mới", "Hướng dẫn", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    qlttn.DapAns.InsertOnSubmit(new DapAn()
                    {
                        NoiDung = txtDapAn.Text,
                        DungSai = ckbDungSai.Checked,
                        //DungSai = bool.Parse(txtDungSai.Text),
                        CauHoi = qlttn.CauHois.Where(ch => ch.maCH == int.Parse(cbDSCH.SelectedValue.ToString())).SingleOrDefault()
                    });
                    qlttn.SubmitChanges();
                }
                loadDGVDapAn();
                dgvDSDA.Rows[dgvDSDA.Rows.GetLastRow(DataGridViewElementStates.Displayed)].Selected = true;
                txtDapAn.Focus();
            };
            btnXoaDapAn.Click += (s, e) =>
            {
                using (var qlttn = new QLTTNDataContext())
                {
                    var cauHoiHienTai = qlttn.CauHois
                                        .Where(ch => ch.maCH == int.Parse(cbDSCH.SelectedValue.ToString()))
                                        .FirstOrDefault();
                    if (cauHoiHienTai.DapAns.Count <= 2)
                    {
                        MessageBox.Show("Mỗi câu hỏi cần phải có tối thiểu 2 đáp án", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (dgvDSDA.SelectedRows.Count == 0)
                    {
                        MessageBox.Show("Hãy chọn đáp án cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    qlttn.DapAns.DeleteOnSubmit(cauHoiHienTai.DapAns.Where(da => da.maDA == int.Parse(dgvDSDA.SelectedRows[0].Cells["maDA"].Value.ToString())).FirstOrDefault());
                    qlttn.SubmitChanges();
                }
                loadDGVDapAn();
            };
            btnSuaDapAn.Click += (s, e) =>
            {
                using (var qlttn = new QLTTNDataContext())
                {
                    var cauHoiHienTai = qlttn.CauHois
                                        .Where(ch => ch.maCH == int.Parse(cbDSCH.SelectedValue.ToString()))
                                        .FirstOrDefault();
                    if (cauHoiHienTai.DapAns.Where(da => da.NoiDung.ToLower() == txtDapAn.Text.ToLower()).Count() != 0)
                    {
                        MessageBox.Show("Đáp án này đã có trong danh sách. Xin mời tạo đáp án mới", "Lỗi trùng dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }

                    var dapAnHienTai = cauHoiHienTai.DapAns.Where(da => da.maDA == int.Parse(dgvDSDA.SelectedRows[0].Cells["maDA"].Value.ToString())).FirstOrDefault();
                    dapAnHienTai.NoiDung = txtDapAn.Text;
                    dapAnHienTai.DungSai = ckbDungSai.Checked;
                    //dapAnHienTai.DungSai = bool.Parse(txtDungSai.Text);

                    qlttn.SubmitChanges();
                }
                loadDGVDapAn();
                txtDapAn.Focus();
            };

            btnImport.Click += (s, e) =>
            {
                using (OpenFileDialog ofd = new OpenFileDialog()
                {
                    Filter = "Excel Workbook|*.xlsx|Excel Workbook 97-2003|*.xls", ValidateNames = true
                })
                {
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        using (var stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            IExcelDataReader reader;
                            DataSet          ds;
                            List <CauHoi>    cauHoiBiTrung = new List <CauHoi>();

                            if (ofd.FilterIndex == 2)
                            {
                                reader = ExcelReaderFactory.CreateBinaryReader(stream);
                            }
                            else
                            {
                                reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                            }

                            ds = reader.AsDataSet(new ExcelDataSetConfiguration()
                            {
                                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                                {
                                    UseHeaderRow = true
                                }
                            });
                            reader.Close();

                            //cb_sheet.Items.Clear();
                            using (var qlttn = new QLTTNDataContext())
                            {
                                DataTable dtCauHoi = ds.Tables["CauHoi"];
                                DataTable dtDapAn  = ds.Tables["DapAn"];
                                DataRow   firstRow = dtCauHoi.Rows[0];

                                int lastIdent = (int)qlttn.ExecuteQuery <decimal>("select IDENT_CURRENT('dbo.CauHoi')").FirstOrDefault();

                                foreach (DataRow row in dtCauHoi.Rows)
                                {
                                    CauHoi cauHoiTmp = new CauHoi()
                                    {
                                        NoiDung = row["NoiDung"].ToString(),
                                        maCD    = int.Parse(row["maCD"].ToString()),
                                    };
                                    if (qlttn.CauHois.Where(ch => ch.NoiDung.ToLower() == cauHoiTmp.NoiDung.ToLower()).Count() == 0)
                                    {
                                        qlttn.CauHois.InsertOnSubmit(cauHoiTmp);
                                        qlttn.SubmitChanges();
                                        foreach (DataRow rowDapAn in dtDapAn.Rows)
                                        {
                                            if (rowDapAn["maCH"].ToString() == row["maCH"].ToString())
                                            {
                                                DapAn datmp = new DapAn()
                                                {
                                                    NoiDung = rowDapAn["NoiDung"].ToString(),
                                                    maCH    = qlttn.CauHois.Max(ch => ch.maCH),
                                                    DungSai = rowDapAn["DungSai"].ToString().ToLower() == "true" ? true : false
                                                };
                                                qlttn.DapAns.InsertOnSubmit(datmp);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        cauHoiBiTrung.Add(cauHoiTmp);
                                    }
                                }
                                qlttn.SubmitChanges();
                            }
                            loadCBCauHoi();
                            loadDGVDapAn();
                            if (cauHoiBiTrung.Count > 0)
                            {
                                string strCauHois = "";
                                for (int i = 0; i < cauHoiBiTrung.Count; i++)
                                {
                                    string str     = cauHoiBiTrung[i].NoiDung;
                                    int    maxLeng = 50;
                                    if (str.Length > maxLeng)
                                    {
                                        str = str.Replace(str.Substring(maxLeng, str.Length - maxLeng), " ...");
                                    }
                                    strCauHois += $"{Environment.NewLine} {i + 1}. {str}";
                                }
                                MessageBox.Show($">>>> DANH SÁCH NHỮNG CÂU HỎI BỊ TRÙNG <<<< {strCauHois}", "Không thể import", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                MessageBox.Show("Import danh sách câu hỏi thành công", "Thông báo");
                            }
                        }
                    }
                }
            };
            btnExport.Click += (s, e) =>
            {
                using (var sfd = new SaveFileDialog()
                {
                    CreatePrompt = false,
                    OverwritePrompt = true,
                    AddExtension = true,
                    Filter = "Excel Workbook|*.xlsx|Excel Workbook 97-2003|*.xls",
                    ValidateNames = true
                })
                {
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        DataTable dtCauHoi = new DataTable();
                        dtCauHoi.TableName = "CauHoi";
                        dtCauHoi.Columns.Add("maCH", typeof(int));
                        dtCauHoi.Columns.Add("NoiDung", typeof(string));
                        dtCauHoi.Columns.Add("maCD", typeof(int));

                        DataTable dtDapAn = new DataTable();
                        dtDapAn.TableName = "DapAn";
                        dtDapAn.Columns.Add("maCH", typeof(int));
                        dtDapAn.Columns.Add("maDA", typeof(int));
                        dtDapAn.Columns.Add("NoiDung", typeof(string));
                        dtDapAn.Columns.Add("DungSai", typeof(bool));

                        using (var qlttn = new QLTTNDataContext())
                        {
                            List <CauHoi> chs = qlttn.CauHois.ToList();
                            List <DapAn>  das = qlttn.DapAns.ToList();
                            for (int i = 0; i < qlttn.CauHois.Count(); i++)
                            {
                                List <DapAn> dasTmp = das.Where(da => da.maCH == chs[i].maCH).ToList();
                                chs[i].maCH = i + 1;
                                dtCauHoi.Rows.Add(chs[i].maCH, chs[i].NoiDung, chs[i].maCD);

                                foreach (var da in dasTmp)
                                {
                                    da.maCH = i + 1;
                                    dtDapAn.Rows.Add(da.maCH, da.maDA, da.NoiDung, da.DungSai);
                                }
                            }
                        };

                        XLWorkbook wb = new XLWorkbook();
                        wb.Worksheets.Add(dtCauHoi, dtCauHoi.TableName);
                        wb.Worksheets.Add(dtDapAn, dtDapAn.TableName);


                        wb.SaveAs(sfd.InitialDirectory + sfd.FileName);
                    }
                }
            };
        }