/// <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> /// 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); } }
// 按下初始化按钮之后执行的操作,即:发送连接状态查询帧。 private void InitializeButton_Click(object sender, RoutedEventArgs e) { // 确认串口已经被打开 if (serial != null && serial.rs232.ComPortIsOpen) { // 按钮被按下后进行初始化,发送对应的帧 // 第一次被按下的时候现实性初始化相关字样 if (!is_initialized) { _output.AppendLine("Initialize process activated." + Environment.NewLine + "Initializing..." + Environment.NewLine + Environment.NewLine); is_initialized = true; } // 准备好要发送的帧 ConnectionStatusInquiryFrame frames_inq = new ConnectionStatusInquiryFrame(); // 做好帧的赋值工作(这个帧的值都是确定的) // 字符串保存即将发出去的帧内容,然后显示 string outStr = frames_inq.FrameBytesInString(); _output.AppendLine("Sending inquiry frame......Content: " + outStr + Environment.NewLine + Environment.NewLine); TheTextBox.ScrollToEnd(); // 注册等待接收事件 serial.rs232.rsr.NewFrame += ComFrameUp; // 调用发送方法将其发给串口 frames_inq.SendFrame(serial); } else { MessageBox.Show("未打开串口。"); return; } }
private void TestButton2_Click(object sender, RoutedEventArgs e) { // 确认串口已经被打开 if (serial != null && serial.rs232.ComPortIsOpen) { // 按钮被按下后发送对应的测试帧 // 准备好要发送的帧 PortParameterInquiryFrame frames_portpara_inq = new PortParameterInquiryFrame(); // 做好帧的赋值工作(这个帧的值都是确定的) // 字符串保存即将发出去的帧内容,然后显示 string outStr = frames_portpara_inq.FrameBytesInString(); _output.AppendLine("Sending test frame......Content: " + outStr + Environment.NewLine + Environment.NewLine); TheTextBox.ScrollToEnd(); // 注册等待接收事件 serial.rs232.rsr.NewFrame += ComFrameUp; // 调用发送方法将其发给串口 frames_portpara_inq.SendFrame(serial); } else { MessageBox.Show("未打开串口。"); return; } }
/// <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(); } } }
async void OnPlayClicked(object sender, RoutedEventArgs e) { var text = TheTextBox.Text; TheTextBox.SelectAll(); var stream = await _engine.SayText(text); foreach (var mark in stream.Markers) { Debug.WriteLine($"{mark.Text} @ {mark.Time.Milliseconds}ms"); } TheMedia.SetSource(stream, stream.ContentType); TheMedia.Play(); }
/// <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); } } }
private void Window_Loaded(object sender, EventArgs e) { TelemetryMessage.Telemetry.TraceEvent(TraceEventType.Information, IdMapper.GetId(EventId.AppStart)); // New session Properties.Settings.Default.LastSessionId = Guid.Empty; Properties.Settings.Default.LastSessionStart = DateTime.Now; Properties.Settings.Default.LastSessionStop = DateTime.Now; Properties.Settings.Default.LastSessionLength = 0; Properties.Settings.Default.Save(); // Try to initialize the ellipsis ActivityDisplayProvider.Instance.IsOn = true; // Give the textbox focus on launch TheTextBox.Focus(); }
}; // 先默认他是四个 // 当下层帧来的时候在这儿显示一下原始内容 private void ComFrameUp(object sender, FrameReceivedArgs e) { // 如果不用下面这一行,我想要的对象还被子线程所拥有,无法用来改变UI层的元素 // 按这一行这样操作一下就可以了。虽然我也不清楚为什么,C#这边还需要学习。 // 参考了https://blog.csdn.net/u014117094/article/details/47776165 Dispatcher.Invoke(new Action(() => { _output.AppendLine("Frame received: " + e.IncomingFrame + Environment.NewLine + Environment.NewLine); TheTextBox.ScrollToEnd(); // 调用FrameClassifier来解析帧 FrameClassifier.IncomingFrameTypes result; FrameClassifier classifier = new FrameClassifier(); result = classifier.RunClassifier(e.IncomingFrameBytes); ProcessClassifyResult(result, e.IncomingFrameBytes); })); // 显示一次之后就取消委托,下次要再显示下次再添加委托。不然每次“初始化”都增加一个委托,每次都多显示一行同样的内容。 serial.rs232.rsr.NewFrame -= ComFrameUp; }
public void AddMsg(string msg) { if (TheTextBox.InvokeRequired == false) { TheTextBox.AppendText(msg); } else { var addMsg = new AddMsgDelegate(AddMsg); try { Invoke(addMsg, msg); } catch { Thread.Sleep(1000); Invoke(addMsg, msg); } } }
/// <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(); } } } }