private void LoadCauHoiNew(long idCauHoi)
        {
            //Load cau hoi
            EX_CauHoi cauHoi = new EX_CauHoi();

            cauHoi         = ListCauHoi.FirstOrDefault(m => m.IDEx == idCauHoi);
            IdCauHoiCurent = cauHoi.IDEx;
            if (!txtCtrlNoiDungCauHoi.IsEmpty)
            {
                LoadCauTraLoiNew(IdCauHoiCurent);
            }
        }
        public bool SaveNewCauHoi()
        {
            try
            {
                //get body
                var cauHoiCha = new EX_CauHoi();
                cauHoiCha.NoiDung      = txtCtrlNoiDungCauHoi.Rtf;
                cauHoiCha.IDChuong     = IdDanhMuc;
                cauHoiCha.IDLoaiCauHoi = (int)LoaiCauHoi.CauHoiDienKhuyet;
                cauHoiCha.IsCauHoiCha  = true;
                cauHoiCha.DoKho        = IdMucDoNhanThuc;
                _business.AddNewCauHoi(cauHoiCha);

                foreach (var ch in ListCauHoi)
                {
                    ch.IDCauHoiCha  = cauHoiCha.Id;
                    ch.IDLoaiCauHoi = (int)LoaiCauHoi.CauHoiDienKhuyet;
                    ch.IDChuong     = IdDanhMuc;
                    ch.DoKho        = IdMucDoNhanThuc;
                    ch.IsKhongDao   = ckbKhongDaoCauHoi.Checked;
                    ch.IsSuDung     = true;
                    ch.NgaySoan     = DateTime.Now;
                    _business.AddNewCauHoi(ch);
                    foreach (var ctl in ch.ListCauTraLoi)
                    {
                        ctl.IDCauHoi   = ch.Id;
                        ctl.IsKhongDao = false;
                        _business.AddNewCauTraLoi(ctl);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
        private bool SaveNewCauHoiTemp(long idCauHoiEx)
        {
            var lstCauTraLoi = new List <EX_CauTraLoi>();

            //Cau tra loi dau tien
            if (_lstTablePanelCauHoi.Count == 0)
            {
                return(false);
            }
            var           tblPanelDapAn = _lstTablePanelCauHoi.FirstOrDefault();
            EditorControl txtDapAn      = tblPanelDapAn.Controls[_nameTextNoiDungDapAn] as EditorControl;
            Label         btnDapAn      = tblPanelDapAn.Controls[_nameLabelDapAn] as Label;

            lstCauTraLoi.Add(new EX_CauTraLoi()
            {
                NoiDung = txtDapAn.Rtf, IsDung = true, IsKhongDao = true
            });

            foreach (var tblPanelDapAnGayNhieu in _lstTablePanelGayNhieu)
            {
                EditorControl txtDapAnGayNhieu = tblPanelDapAnGayNhieu.Controls[_nameTextNoiDungDapAn] as EditorControl;
                Label         btnDapAnGayNhieu = tblPanelDapAnGayNhieu.Controls[_nameLabelDapAn] as Label;
                lstCauTraLoi.Add(new EX_CauTraLoi()
                {
                    NoiDung = txtDapAnGayNhieu.Rtf, IsDung = false, IsKhongDao = true
                });
            }

            #region Save new câu hỏi
            //Cau hoi cha
            EX_CauHoi cauHoiCha = new EX_CauHoi();
            cauHoiCha.IDChuong     = IdDanhMuc;
            cauHoiCha.NoiDung      = txtCtrlNoiDungCauHoi.Rtf;
            cauHoiCha.IDLoaiCauHoi = (int)LoaiCauHoi.CauHoiDienKhuyet;

            EX_CauHoi cauHoi = ListCauHoi.FirstOrDefault(m => m.IDEx == IdCauHoiCurent);
            cauHoi.IDChuong = IdDanhMuc;
            cauHoi.DoKho    = IdMucDoNhanThuc;
            cauHoi.IsSuDung = true;
            cauHoi.NgaySoan = DateTime.Now;

            // Danh sách câu trả lời
            int i = 1;
            cauHoi.ListCauTraLoi.OrderBy(m => m.IdEx).ToList();
            cauHoi.ListCauTraLoi.ForEach(m => { m.IdEx = i++; });
            i = 1;
            foreach (var esCauTraLoi in lstCauTraLoi)
            {
                if (cauHoi.ListCauTraLoi.Any(m => m.IdEx == i))
                {
                    var cauTraLoi = cauHoi.ListCauTraLoi.FirstOrDefault(m => m.IdEx == i);
                    cauTraLoi.IdEx       = i;
                    cauTraLoi.NoiDung    = esCauTraLoi.NoiDung;
                    cauTraLoi.IsDung     = esCauTraLoi.IsDung;
                    cauTraLoi.IsKhongDao = esCauTraLoi.IsKhongDao;
                }
                else
                {
                    esCauTraLoi.IDCauHoi = cauHoi.IDEx;
                    esCauTraLoi.IdEx     = i;
                    cauHoi.ListCauTraLoi.Add(esCauTraLoi);
                }
                i++;
            }

            // Xoa cau tra loi
            foreach (int idCauTraLoiBiXoa in ListIdCauHoiBiXoa)
            {
                if (idCauTraLoiBiXoa != 0)
                {
                    var cauTraLoiBiXoa = cauHoi.ListCauTraLoi.FirstOrDefault(m => m.Id == idCauTraLoiBiXoa);
                    cauHoi.ListCauTraLoi.Remove(cauTraLoiBiXoa);
                }
            }
            #endregion

            return(true);
        }
        private void LoadNoiDungCauHoiCha(long idCauHoiCha)
        {
            //Chuyen id cau hoi va idcau tra li ve dang IDEx từ 1 đến... để xủ lý chung
            if (IdCauHoiCha > 0)
            {
                var cauHoiCha = _business.GetById(IdCauHoiCha);
                if (cauHoiCha != null)
                {
                    txtCtrlNoiDungCauHoi.Rtf = cauHoiCha.NoiDung;
                    //Đổ dữ liệu cau hoi để xử lý
                    ListCauHoi = new List <EX_CauHoi>();
                    ListCauHoi = _business.Context.EX_CauHoi.Where(mbox => mbox.IDCauHoiCha == cauHoiCha.Id).ToList();
                    var lstDataCbo = new List <DM_STT>();
                    int i          = 1;
                    foreach (var item in ListCauHoi)
                    {
                        var lstCauTL = _business.GetCauTraLoi(item.Id);
                        int j        = 1;
                        foreach (var ctl in lstCauTL)
                        {
                            ctl.IdEx = j;
                            j++;
                        }
                        item.ListCauTraLoi = lstCauTL;
                        item.IDEx          = i;
                        lstDataCbo.Add(new DM_STT {
                            Id = i, Ten = string.Format("Câu hỏi {0}", i)
                        });
                        i++;
                    }
                    cboCauHoi.EditValueChanged -= CboCauHoi_EditValueChanged;

                    var lstItem = new List <LookUpEditItem>();
                    lstItem.Add(new LookUpEditItem {
                        ColumnName = "Id", ColumnCaption = "Id"
                    });
                    lstItem.Add(new LookUpEditItem {
                        ColumnName = "Ten", ColumnCaption = "Tên"
                    });
                    cboCauHoi.SetData(lstDataCbo, lstItem, "Ten", "Id");

                    var cauHoiFirst = new EX_CauHoi();
                    if (IdCauHoiCurent > 0)
                    {
                        cauHoiFirst = ListCauHoi.FirstOrDefault(m => m.Id == IdCauHoiCurent);
                    }
                    else
                    {
                        cauHoiFirst = ListCauHoi.FirstOrDefault();
                    }

                    CauHoiCurent         = cauHoiFirst;
                    IdCauHoiCurent       = cauHoiFirst.IDEx;
                    cboCauHoi.CategoryID = IdCauHoiCurent;

                    txtSoCauHoi.Text            = ListCauHoi.Count.ToString();
                    cboCauHoi.EditValueChanged += CboCauHoi_EditValueChanged;
                    //Day vao list match
                    _mathCollection = _parsing.ParseDienKhuyet2(txtCtrlNoiDungCauHoi.Text);
                }
            }
        }
        public bool SaveUpdateCauHoi()
        {
            try
            {
                #region Update cau hoi cha

                var cauHoiCha = new EX_CauHoi();
                cauHoiCha.NoiDung      = txtCtrlNoiDungCauHoi.Rtf;
                cauHoiCha.IDChuong     = IdDanhMuc;
                cauHoiCha.IDLoaiCauHoi = (int)LoaiCauHoi.CauHoiDienKhuyet;
                cauHoiCha.IsCauHoiCha  = true;
                cauHoiCha.DoKho        = IdMucDoNhanThuc;
                _business.UpdateCauHoi(IdCauHoiCha ?? 0, cauHoiCha);

                #endregion

                #region Update cau hoi

                foreach (var ch in ListCauHoi)
                {
                    ch.IDCauHoiCha  = IdCauHoiCha;
                    ch.IDLoaiCauHoi = (int)LoaiCauHoi.CauHoiDienKhuyet;
                    ch.DoKho        = IdMucDoNhanThuc;
                    ch.IDChuong     = IdDanhMuc;
                    ch.IsKhongDao   = ckbKhongDaoCauHoi.Checked;
                    ch.NgaySoan     = DateTime.Now;
                    if (ch.Id == 0)
                    {
                        _business.AddNewCauHoi(ch);
                    }
                    else
                    {
                        _business.UpdateCauHoi(ch.Id, ch);
                    }

                    // Câu trả lời
                    foreach (var ctl in ch.ListCauTraLoi)
                    {
                        ctl.IsKhongDao = false;
                        ctl.IDCauHoi   = ch.Id;
                        if (ctl.Id == 0)
                        {
                            _business.AddNewCauTraLoi(ctl);
                        }
                        else
                        {
                            _business.UpdateCauTraLoi(ctl.Id, ctl);
                        }
                    }
                }
                // Xoa cau tra loi
                foreach (int idCauTraLoiBiXoa in ListIdCauTraLoiBiXoa)
                {
                    if (idCauTraLoiBiXoa != 0)
                    {
                        _business.DeleteCauTraLoiById(idCauTraLoiBiXoa);
                    }
                }

                // Xoa cau hoi
                foreach (int idCauHoiBiXoa in ListIdCauHoiBiXoa)
                {
                    if (idCauHoiBiXoa != 0)
                    {
                        _business.DeleteCauHoiByLstId(new List <long> {
                            idCauHoiBiXoa
                        });
                    }
                }


                #endregion

                IsChanged = false;
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
        private void BtnDienKhuyet_Click(object sender, EventArgs e)
        {
            txtCtrlNoiDungCauHoi.Focus();
            //Them vao cau tra loi
            if (txtCtrlNoiDungCauHoi.SelectionLength == 0)
            {
                UICommon.ShowMsgWarningString("Bạn chưa chọn nội dung cần điền khuyết");
                return;
            }
            var textStart = txtCtrlNoiDungCauHoi.SelectionStart;

            //Kiem tra vi tri dien khuyet
            if (_mathCollection != null)
            {
                for (int i = 0; i < _mathCollection.Count; i++)
                {
                    var viTriNext = _mathCollection[i].Index + _mathCollection[i].Length;
                    if (textStart >= _mathCollection[i].Index && textStart <= viTriNext)
                    {
                        UICommon.ShowMsgWarningString("Vị trí điền khuyết không hợp lệ!");
                        if (txtCtrlNoiDungCauHoi.SelectionLength == 0)
                        {
                            txtCtrlNoiDungCauHoi.Undo();
                        }
                        return;
                    }
                }
            }

            var tmp = txtCtrlNoiDungCauHoi.SelectedRtf;

            //Buoc 2: Add cau tra loi dau tien cho cau hoi hien tai => vi trí diền khuyết
            if (txtCtrlNoiDungCauHoi.SelectionLength != 0)
            {
                //Gan du lieu toan cuc và cboCauHoi
                CauHoiCurent = new EX_CauHoi {
                    IDEx = ListCauHoi.Count + 1, DoKho = IdMucDoNhanThuc
                };
                //Buoc 1
                ListCauHoi.Add(CauHoiCurent);
                CauHoiCurent.ListCauTraLoi.Add(new EX_CauTraLoi {
                    IdEx = 1, NoiDung = txtCtrlNoiDungCauHoi.SelectedRtf, IndexEx = textStart
                });

                var textStt = string.Format("({0})_____", ListCauHoi.Count);
PasteClipboard:
                {
                    try
                    {
                        Clipboard.SetText(textStt);
                    }
                    catch (Exception)
                    {
                        goto PasteClipboard;
                    }
                }
                txtCtrlNoiDungCauHoi.Paste();
            }
            else
            {
                UICommon.ShowMsgWarningString("Nội dung chọn không hợp lệ");
                if (txtCtrlNoiDungCauHoi.SelectionLength == 0)
                {
                    txtCtrlNoiDungCauHoi.Undo();
                }
                return;
            }

            //Buoc 3: do du lieu len cbo
            if (ListCauHoi.Count > 0)
            {
                SetDataToCboCauHoi();
            }
            //Buoc 4 => changCategoryID
            _isDienKhuyet        = true;
            cboCauHoi.CategoryID = ListCauHoi.Count;
            IdCauHoiCurent       = (int)cboCauHoi.CategoryID;

            //Ve lai cau hoi => phan ben trai
            ClearAllCauTraLoi();
            ClearAllCauTraLoiGayNhieu();
            ThemCauTraLoi();

            CurrentControl.Focus();
            CurrentControl.Rtf      = tmp;
            CurrentControl.ReadOnly = true;

            // Nhận dạng lại vị trí
            _mathCollection = _parsing.ParseDienKhuyet2(txtCtrlNoiDungCauHoi.Text);
        }