private void txtCtrlNoiDungCauHoi_SelectionChanged(object sender, EventArgs e)
        {
            var lenght = txtCtrlNoiDungCauHoi.SelectionLength;

            txtCtrlNoiDungCauHoi.ReadOnly = false;
            var index = txtCtrlNoiDungCauHoi.SelectionStart;
            var line  = txtCtrlNoiDungCauHoi.GetLineFromCharIndex(index);

            if (CauHoiCurent.ListCauTraLoi.Any(n => (n.IndexEx <= index && (n.IndexEx + UICommon.GetLengthRtf(n.NoiDung)) >= index) ||
                                               (n.IndexEx <= (index + lenght) && (n.IndexEx + UICommon.GetLengthRtf(n.NoiDung)) >= (index + lenght)) ||
                                               (n.IndexEx >= index && (n.IndexEx + UICommon.GetLengthRtf(n.NoiDung)) <= (index + lenght))
                                               ))
            {
                txtCtrlNoiDungCauHoi.ReadOnly = true;
                return;
            }
            if (txtCtrlNoiDungCauHoi.GetLineFromCharIndex(index + lenght) != line)
            {
                txtCtrlNoiDungCauHoi.ReadOnly = true;
                return;
            }

            //RichTextBox richTextBox = (RichTextBox)sender;
            //var selectionLenghNow = richTextBox.SelectionLength;

            //if (_cauHoiCurent.ListCauTraLoi.Count < 2)
            //    _textLength = richTextBox.TextLength;
            //richTextBox.ReadOnly = false;
            //foreach (var ctl in _cauHoiCurent.ListCauTraLoi)
            //{
            //    if (richTextBox.SelectionStart >= ctl.IndexEx && richTextBox.SelectionStart <= ctl.IndexEx + UICommon.GetLengthRtf(ctl.NoiDung) || richTextBox.SelectionStart + selectionLenghNow >= ctl.IndexEx && richTextBox.SelectionStart + selectionLenghNow <= ctl.IndexEx + UICommon.GetLengthRtf(ctl.NoiDung))
            //    {
            //        richTextBox.ReadOnly = true;
            //        break;
            //    }
            //    if (richTextBox.SelectionStart < ctl.IndexEx && richTextBox.SelectionStart + selectionLenghNow > ctl.IndexEx + UICommon.GetLengthRtf(ctl.NoiDung) && selectionLenghNow != richTextBox.TextLength)
            //    {
            //        richTextBox.ReadOnly = true;
            //        break;
            //    }
            //    richTextBox.ReadOnly = false;
            //}
        }
        private void XoaCauTraLoi(object sender, EventArgs e)
        {
            if (UICommon.ShowMsgQuestionString("Bạn có chắc muốn xóa câu trả lời này không ?", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }

            SimpleButton     button        = sender as SimpleButton;
            TableLayoutPanel tblPanelDapAn = button.Parent as TableLayoutPanel;

            //get id cau hoi bi xoa
            int idEx      = Convert.ToInt32(button.Tag);
            var cauTraLoi = CauHoiCurent.ListCauTraLoi.FirstOrDefault(m => m.IdEx == idEx);

            if (cauTraLoi != null)
            {
                // Xóa gạch chân trên câu hỏi
                txtCtrlNoiDungCauHoi.SelectionStart  = cauTraLoi.IndexEx ?? 0;
                txtCtrlNoiDungCauHoi.SelectionLength = UICommon.GetLengthRtf(cauTraLoi.NoiDung);
                if (txtCtrlNoiDungCauHoi.IsUnderline())
                {
                    txtCtrlNoiDungCauHoi.Underline();
                }
                CauHoiCurent.ListCauTraLoi = CauHoiCurent.ListCauTraLoi.Except(new List <EX_CauTraLoi> {
                    cauTraLoi
                }).ToList();
                // Nhập những câu hỏi có Id bị xóa
                if (cauTraLoi.Id > 0)
                {
                    ListIdCauHoiBiXoa.Add(cauTraLoi.Id);
                    IsChanged = true;
                }
            }
            // Xóa hết trên màn hình
            ClearAllCauTraLoi();
            // Hiển thị lại
            for (int i = 1; i <= CauHoiCurent.ListCauTraLoi.Count; i++)
            {
                var ctraLoi = CauHoiCurent.ListCauTraLoi[i - 1];
                ctraLoi.IdEx = i;
                ThemCauTraLoi(ctraLoi);
            }
        }
        public bool ValidateNoiDung()
        {
            if (txtCtrlNoiDungCauHoi.IsEmpty)
            {
                UICommon.ShowMsgWarningString("Bạn hãy nhập nội dung câu hỏi.");
                return(false);
            }

            List <string> lstCauTraLoi = new List <string>();

            if (!CauHoiCurent.ListCauTraLoi.Any(m => m.IsDung ?? false))
            {
                UICommon.ShowMsgWarningString("Bạn chưa chọn đáp án đúng");
                return(false);
            }

            if (CauHoiCurent.ListCauTraLoi.Any(m => UICommon.GetLengthRtf(m.NoiDung) == 0))
            {
                UICommon.ShowMsgWarningString("Tồn tại câu trả lời bạn chưa nhập nội dung");
                return(false);
            }

            return(true);
        }
        private void OnUnderlineClicked(object sender, EventArg e)
        {
            var obj = (EditorControl)sender;

            if (obj != null && obj.Name == "txtCtrlNoiDungCauHoi")
            {
                var lenght = txtCtrlNoiDungCauHoi.SelectionLength;
                if (lenght > 0 && !string.IsNullOrEmpty(txtCtrlNoiDungCauHoi.SelectedText.Trim()))
                {
                    var index = txtCtrlNoiDungCauHoi.SelectionStart;
                    var line  = txtCtrlNoiDungCauHoi.GetLineFromCharIndex(index);
                    if (CauHoiCurent.ListCauTraLoi.Any(n => (n.IndexEx <= index && (n.IndexEx + UICommon.GetLengthRtf(n.NoiDung)) >= index) ||
                                                       (n.IndexEx <= (index + lenght) && (n.IndexEx + UICommon.GetLengthRtf(n.NoiDung)) >= (index + lenght)) ||
                                                       (n.IndexEx >= index && (n.IndexEx + UICommon.GetLengthRtf(n.NoiDung)) <= (index + lenght))
                                                       ))
                    {
                        UICommon.ShowMsgWarningString("Vị trí gạch chân không hợp lệ");
                        txtCtrlNoiDungCauHoi.ReadOnly = false;
                        txtCtrlNoiDungCauHoi.Undo();
                        txtCtrlNoiDungCauHoi.ReadOnly = true;
                        return;
                    }
                    if (txtCtrlNoiDungCauHoi.GetLineFromCharIndex(index + lenght) != line)
                    {
                        UICommon.ShowMsgWarningString("Vị trí gạch chân không hợp lệ");
                        txtCtrlNoiDungCauHoi.ReadOnly = false;
                        txtCtrlNoiDungCauHoi.Undo();
                        txtCtrlNoiDungCauHoi.ReadOnly = true;
                        return;
                    }
                    // Xóa hết câu trả lời để add lại
                    ClearAllCauTraLoi();
                    // nếu như là hợp lệ
                    CauHoiCurent.ListCauTraLoi.Add(new EX_CauTraLoi {
                        NoiDung = txtCtrlNoiDungCauHoi.SelectedRtf, IndexEx = index, Line = line
                    });
                    //Add lại câu trả lời
                    CauHoiCurent.ListCauTraLoi = CauHoiCurent.ListCauTraLoi.OrderBy(n => n.IndexEx).ToList();
                    for (int i = 1; i <= CauHoiCurent.ListCauTraLoi.Count; i++)
                    {
                        var ctraLoi = CauHoiCurent.ListCauTraLoi[i - 1];
                        ctraLoi.IdEx = i;
                        ThemCauTraLoi(ctraLoi);
                    }
                }
                else if (lenght > 0)
                {
                    UICommon.ShowMsgWarningString("Vị trí gạch chân không hợp lệ");
                    txtCtrlNoiDungCauHoi.ReadOnly = false;
                    txtCtrlNoiDungCauHoi.Undo();
                    txtCtrlNoiDungCauHoi.ReadOnly = true;
                }
            }
            else
            {
                if (txtCtrlNoiDungCauHoi.SelectionLength > 0)
                {
                    txtCtrlNoiDungCauHoi.ReadOnly = false;
                    txtCtrlNoiDungCauHoi.Undo();
                    txtCtrlNoiDungCauHoi.ReadOnly = true;
                }
            }
        }
        private void txtCtrlNoiDungCauHoi_KeyDown(object sender, KeyEventArgs e)
        {
            RichTextBox richTextBox1 = (RichTextBox)sender;

            if (richTextBox1.ReadOnly)
            {
                return;
            }
            bool flag1          = e.Modifiers == Keys.Control && e.KeyCode == Keys.V;
            bool flag2          = e.Modifiers == Keys.Shift && e.KeyCode == Keys.Insert;
            bool flag3          = e.KeyCode == Keys.Delete;
            bool flag4          = e.KeyCode == Keys.Back;
            int  selectionStart = richTextBox1.SelectionStart;

            if (flag3)
            {
                foreach (var ctl in CauHoiCurent.ListCauTraLoi)
                {
                    if (ctl.IndexEx == selectionStart)
                    {
                        richTextBox1.ReadOnly = true;
                        break;
                    }
                    richTextBox1.ReadOnly = false;
                }
            }
            if (flag4)
            {
                foreach (var ctl in CauHoiCurent.ListCauTraLoi)
                {
                    if (ctl.IndexEx + UICommon.GetLengthRtf(ctl.NoiDung) == selectionStart)
                    {
                        richTextBox1.ReadOnly = true;
                        break;
                    }
                    richTextBox1.ReadOnly = false;
                }
            }

            if (richTextBox1.SelectionLength == richTextBox1.TextLength)
            {
                if (flag1 | flag2)
                {
                    RichTextBox richTextBox2 = new RichTextBox();
                    richTextBox2.Clear();
                    richTextBox2.Paste();
                    Clipboard.Clear();
                    richTextBox2.SelectAll();
                    richTextBox2.SelectionColor = Color.Black;
                    richTextBox2.SelectionFont  = new Font("Times New Roman", 11.25f, FontStyle.Regular);
                    richTextBox2.Copy();
                    richTextBox1.Visible = false;
                    richTextBox1.Clear();
                    richTextBox1.Paste();
                    richTextBox1.Focus();
                    richTextBox1.Visible         = true;
                    richTextBox1.SelectionLength = 0;
                    richTextBox1.Select(richTextBox1.TextLength, 0);
                    Clipboard.Clear();
                }
                else if (flag3 | flag4)
                {
                    RichTextBox richTextBox2 = new RichTextBox();
                    Clipboard.Clear();
                    richTextBox2.Clear();
                }
            }
        }