/// <summary> /// Reads line after keyword and until newline character. /// </summary> /// <param name="keyword">Where reading should start</param> /// <returns>Returns line that has been read.</returns> public String ReadLine(string keyword) { String text = String.Empty; if (TheTextBox.InvokeRequired) { // It's on a different thread, so use Invoke: lock (m_LockObject) { GetTextCallback ourDelegate = new GetTextCallback(getText); text = (String)TheTextBox.Invoke(ourDelegate, new object[] { TheTextBox }); } } else { lock (m_LockObject) { text = TheTextBox.Text; } } if ((text.LastIndexOf("\n") - text.LastIndexOf(keyword) - keyword.Length > 0) && text.LastIndexOf(keyword) > -1) { return(text.Substring(text.LastIndexOf(keyword) + keyword.Length, text.LastIndexOf("\n") - text.LastIndexOf(keyword) - keyword.Length)); } else { return(String.Empty); } }
/// <summary> /// Writes a message to the panel that this PanelHandler keeps a reference to /// </summary> /// <param name="message">The message string we want to write.</param> public void WriteDelayedMessage(string message) { if (!String.IsNullOrEmpty(message)) { //If the PanelHandler is printing progress text, then remove the last line of this progress text so we can update it with new progress count text: if (new Regex(@"[a-z]{8}\:\s{1}\d{1,3}\%", RegexOptions.IgnoreCase).Match(message).Success) { RemoveLastLine(@"[a-z]{8}\:\s{1}\d{1,3}\%"); } //If the PanelHandler is printing progress text, then remove the last line of this progress text so we can update it with new progress count text: if (new Regex(@"\d{1,3}\s{1}\%\s{1}[a-z]{8}", RegexOptions.IgnoreCase).Match(message).Success) { RemoveLastLine(@"\d{1,3}\s{1}\%\s{1}[a-z]{8}"); } if (StatusUpdateList != null) { string statusMessage = StatusUpdateList.Where(m => message.Trim().ToLower().Contains(m.Trim().ToLower())).FirstOrDefault(); if (!String.IsNullOrEmpty(statusMessage) && StatusHandler != null) { string successComparer = !String.IsNullOrEmpty(StatusSuccessString) ? StatusSuccessString.ToLower() : String.Empty; StatusHandler.PrintStatus(statusMessage, statusMessage.ToLower() == successComparer ? Status.Success : Status.InProgress); StatusStepEvent(StatusUpdateList.FindIndex(m => m.ToLower().Trim() == statusMessage.ToLower())); } } int msgLength = message.TrimEnd().Length; string outText; if (message == "PS>" || message == "\nPS>" || msgLength == 0) { outText = message; } else { outText = message.Substring(0, msgLength); if (outText[outText.Length - 1] != '\n') { outText += "\n"; } } foreach (char c in outText) { if (TheTextBox.InvokeRequired) { // It's on a different thread, so use Invoke. lock (m_LockObject) { PrintTextCallback ourDelegate = new PrintTextCallback(printText); TheTextBox.Invoke(ourDelegate, new object[] { c, TheTextBox, TextColor }); } } else { lock (m_LockObject) { // It's on the same thread, no need for Invoke TheTextBox.AppendText(c.ToString(), TextColor); } } Thread.Sleep(PrintDelay); } } }
/// <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(); } } }
/// <summary> /// Sets the cursor on the last line, but does not scroll. /// </summary> public void SetCursorAtLastLine() { if (TheTextBox.InvokeRequired) { lock (m_LockObject) { SetCursorAtLastLineCallback ourDelegate = new SetCursorAtLastLineCallback(setCursorAtLastLine); TheTextBox.Invoke(ourDelegate, new object[] { TheTextBox }); } } else { lock (m_LockObject) { TheTextBox.Select(TheTextBox.TextLength, 1); } } }
/// <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(); } } } }