private void frmFindText_onClosed(object sender, FormClosedEventArgs e)
 {
     if (!this._frmFindText.IsDisposed)
         this._frmFindText.Dispose();
     this._frmFindText = null;
 }
        //***************************************************************************
        // Event Overrides
        // 
        protected override void OnKeyDown(KeyEventArgs e)
        {
            //this._lastKeyPress = Keys.None;
            //this._lastModifierPress = Keys.None;
            //this._lastCharPos = -1;
            //this._clearedText = string.Empty;

            AdvRichTextBox.SelectDirection newSelDir = AdvRichTextBox.SelectDirection.None;

            if (e.KeyCode == System.Windows.Forms.Keys.X && e.Modifiers == System.Windows.Forms.Keys.Control)
            {
                #region CTRL-X - Cut
                this.Cut();
                e.Handled = true;
                e.SuppressKeyPress = true;
                #endregion
            }
            if (e.KeyCode == System.Windows.Forms.Keys.V && e.Modifiers == System.Windows.Forms.Keys.Control)
            {
                #region CTRL-V - Paste
                this.Paste();
                e.Handled = true;
                e.SuppressKeyPress = true;
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Z && e.Modifiers == (System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift))
            {
                #region CTRL-SHIFT-Z - Redo
                this.Redo();
                e.Handled = true;
                e.SuppressKeyPress = true;
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Z && e.Modifiers == System.Windows.Forms.Keys.Control)
            {
                #region CTRL-Z - Undo
                this.Undo();
                e.Handled = true;
                e.SuppressKeyPress = true;
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Z && e.Modifiers == System.Windows.Forms.Keys.Control)
            {
                #region CTRL-P - Print
                this.Print(this.DocumentName);
                e.Handled = true;
                e.SuppressKeyPress = true;
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Delete)
            {
                // This key does not trigger this event. Check the 'OnPreviewKeyDown' method.
                #region DELETE
                //if (this.SelectionLength > 0)
                //{
                //    // Grab the text that's about to be whacked.
                //    this.AddUndo(new UndoState(UndoStateType.Clear, string.Empty, this.SelectedText, this.SelectionStart));
                //}
                //else
                //{
                //    // Treat it like a delete.
                //    if (this.SelectionStart < this.Text.Length)
                //        try
                //        { this.AddUndo(new UndoState(UndoStateType.Delete, string.Empty, this.Text.Substring(this.SelectionStart, 1), this.SelectionStart)); }
                //        catch { }
                //}
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Back)
            {
                // This key does not trigger this event. Check the 'OnPreviewKeyDown' method.
                #region BACKSPACE
                //if (this.SelectionLength > 0)
                //{
                //    // Grab the text that's about to be whacked.
                //    this.AddUndo(new UndoState(UndoStateType.Clear, string.Empty, this.SelectedText, this.SelectionStart));
                //}
                //else
                //{
                //    // Treat it like a backspace.
                //    if (this.SelectionStart > 0)
                //        try
                //        { this.AddUndo(new UndoState(UndoStateType.Backspace, string.Empty, this.Text.Substring(this.SelectionStart - 1, 1), this.SelectionStart - 1)); }
                //        catch { }
                //}
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.F && e.Modifiers == System.Windows.Forms.Keys.Control)
            {
                #region CTRL-F - Find Text
                if (this._frmFindText == null || this._frmFindText.IsDisposed)
                {
                    this.StartFind();
                }
                else
                {
                    this._frmFindText.Dispose();
                    this._frmFindText = null;
                }
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.F3)
            {
                #region F3 - Find Next
                if (this._frmFindText != null && !this._frmFindText.IsDisposed)
                    this.FindNext();
                else
                    this.StartFind();
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Shift || e.KeyCode == System.Windows.Forms.Keys.ShiftKey)
            {
                // Prevent these keys from being processed.
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Home)
            {
                #region HOME Key
                // If the user was holding the CTRL key when they pressed home, then
                //   we just move to the very beginning of the document.
                if (e.Control)
                {
                    if (e.Shift)
                        //this.Select(0, this.SelectionStart + this.SelectionLength);
                        this.Select(this.SelectionStart, 0 - this.SelectionStart);
                    else
                        this.Select(0, 0);
                    e.Handled = true;
                }
                else
                {
                    // If the user presses the "HOME" key, then we want to move the
                    //   cursor inteligently to either the beginning of the line,
                    //   or the beginning of the actual text.
                    int curLn = this.GetLineFromCharIndex(this.SelectionStart),
                        curPos = this.SelectionStart,
                        lnStart = this.GetFirstCharIndexFromLine(curLn);

                    // If the current line doesn't begin with any whitespace, then we
                    //   have nothing to do here.
                    string lnTxt = this.Lines[curLn];
                    if (lnTxt.StartsWith("\t") || lnTxt.StartsWith(" "))
                    {
                        int wsCharCnt = (lnTxt.Length - lnTxt.TrimStart('\t', ' ').Length),
                            selLen = lnStart - (curPos + SelectionLength);
                        if (this.SelectionStart == (lnStart + wsCharCnt))
                        {
                            // If we're at the beginning of the text, then move to the
                            //   first of the line.
                            if (e.Shift)
                                //this.Select(lnStart, (curPos + this.SelectionLength) - lnStart);
                                this.Select(curPos + this.SelectionLength, selLen);
                            else
                                this.Select(lnStart, 0);
                        }
                        else
                        {
                            // If the cursor is already at the beginning of the line,
                            //   or anywhere else in the line, then move to the end
                            //   of the whitespace.
                            if (e.Shift)
                                //this.Select(lnStart + wsCharCnt, (curPos - (lnStart + wsCharCnt)) + this.SelectionLength);
                                this.Select(curPos + this.SelectionLength, selLen + wsCharCnt);
                            else
                                this.Select(lnStart + wsCharCnt, 0);
                        }
                        newSelDir = (selLen > 0) ? AdvRichTextBox.SelectDirection.Forward : AdvRichTextBox.SelectDirection.Backword;
                        e.Handled = true;
                    }
                }
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.PageUp)
            {
                #region PAGE-UP Key
                //TODO:: Write logic to determine selection direction based on
                //   current selection direction and selection length.
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.PageDown)
            {
                #region PAGE-DOWN Key
                //TODO:: Write logic to determine selection direction based on
                //   current selection direction and selection length.
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Up)
            {
                #region UP Key
                //TODO:: Write logic to determine selection direction based on
                //   current selection direction and selection length.
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Down)
            {
                #region DOWN Key
                //TODO:: Write logic to determine selection direction based on
                //   current selection direction and selection length.
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Left)
            {
                #region LEFT Key
                //TODO:: Write logic to determine selection direction based on
                //   current selection direction and selection length.
                #endregion
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Right)
            {
                #region RIGHT Key
                //TODO:: Write logic to determine selection direction based on
                //   current selection direction and selection length.
                #endregion
            }

            this._lastCharPos = this.SelectionStart;
            this._clearedText = this.SelectedText;
            this._lastKeyPress = e.KeyCode;
            this._lastModifierPress = e.Modifiers;
            this._selDir = newSelDir;
            if (this.IsInputKey(e.KeyCode) && !e.Control && !e.Alt)
            {
                //string keyValue = this.Text.Substring(this._lastCharPos, 1);
                KeysConverter kConv = new KeysConverter();
                string keyValue = kConv.ConvertToString(e.KeyCode);
                if (!e.Shift)
                    keyValue = keyValue.ToLower();
                this.AddUndo(new UndoState(UndoStateType.Insert, keyValue, this._clearedText, this._lastCharPos));
            }
            base.OnKeyDown(e);
        }
 protected void StartFind()
 {
     this._frmFindText = new RainstormStudios.Forms.frmFindText();
     this._frmFindText.FindNext += new EventHandler(this.frmFindText_onFindNext);
     this._frmFindText.FormClosed += new FormClosedEventHandler(this.frmFindText_onClosed);
     this._frmFindText.Show(this.FindForm());
 }