示例#1
0
        /// <summary>
        /// Remove specified words from our RichTextBox control identified by they keywords argument.
        /// </summary>
        /// <param name="Keywords">List of strings that identify words we want to remove from TheTextBox</param>
        public void RemoveLines(List <string> Keywords)
        {
            string regex   = String.Empty;
            string NewText = String.Empty;

            //1. Get the Rtf from the Textbox
            if (TheTextBox.InvokeRequired)
            {
                // It's on a different thread, so use Invoke:
                lock (m_LockObject) {
                    GetRtfCallback ourDelegate = new GetRtfCallback(getRtf);
                    NewText = (String)TheTextBox.Invoke(ourDelegate, new object[] { TheTextBox });
                }
            }
            else
            {
                lock (m_LockObject) {
                    NewText = TheTextBox.Rtf;
                }
            }

            //2. Perform regex magic on the text retrieved from the Textbox:
            Regex MyRegex = null;

            foreach (string keyword in Keywords)
            {
                regex   = String.Format(@"{0}", keyword);
                MyRegex = new Regex(regex, RegexOptions.Multiline);
                NewText = MyRegex.Replace(NewText, "");
                //This one would remove blank lines, but is commented away:
                //NewText = Regex.Replace(NewText, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
            }

            //3. Put the Rtf back to the text box after the magic is done:
            if (TheTextBox.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                lock (m_LockObject) {
                    SetRtfCallback ourDelegate = new SetRtfCallback(setRtf);
                    TheTextBox.Invoke(ourDelegate, new object[] { NewText, TheTextBox });
                }
            }
            else
            {
                lock (m_LockObject) {
                    TheTextBox.Rtf = NewText;
                    TheTextBox.Refresh();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Remove last line if it is identified by regex
        /// </summary>
        /// <param name="regex">regex to match in last line</param>
        public void RemoveLastLine(string regex)
        {
            string        OldText  = String.Empty;
            StringBuilder NewText  = new StringBuilder();
            int           stepBack = 2;

            string lastLine = String.Empty;

            TheTextBox.HandleInvokeRequired(__TheTextBox => {
                lastLine = __TheTextBox.Lines[__TheTextBox.Lines.Length - 1];
                if (String.IsNullOrEmpty(lastLine))
                {
                    lastLine = __TheTextBox.Lines[__TheTextBox.Lines.Length - 2];
                    stepBack = 4;
                }
            });
            if (new Regex(regex, RegexOptions.IgnoreCase).Match(lastLine).Success)
            {
                //1. Get the Rtf from the Textbox
                if (TheTextBox.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke:
                    lock (m_LockObject) {
                        GetRtfCallback ourDelegate = new GetRtfCallback(getRtf);
                        OldText = (String)TheTextBox.Invoke(ourDelegate, new object[] { TheTextBox });
                    }
                }
                else
                {
                    lock (m_LockObject) {
                        OldText = TheTextBox.Rtf;
                    }
                }

                //2. Perform magic on the text retrieved from the Textbox:
                int size = OldText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Length;
                for (int count = 0; count < size - stepBack; count++)
                {
                    NewText.Append(OldText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)[count]);
                    if (NewText[NewText.Length - 1] != '\n' || NewText[NewText.Length - 1].ToString() != System.Environment.NewLine)
                    {
                        NewText.Append(System.Environment.NewLine);
                    }
                }
                ;


                //3. Put the Rtf back to the text box after the magic is done:
                if (TheTextBox.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.
                    lock (m_LockObject) {
                        SetRtfCallback ourDelegate = new SetRtfCallback(setRtf);
                        TheTextBox.Invoke(ourDelegate, new object[] { NewText.ToString(), TheTextBox });
                    }
                }
                else
                {
                    lock (m_LockObject) {
                        TheTextBox.Rtf = NewText.ToString();
                        TheTextBox.Refresh();
                    }
                }
            }
        }