visual studio 富文本框不显示文本
visual studio richtext box not displaying text
我正在使用 Visual studio 2017,我有一个 RichTextBox,当我设置它时不显示文本:
public void ArduinoDataRecived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort TempSerial = (SerialPort)sender;
ConsoleTextBox.AppendText(TempSerial.ReadLine());
}
代码运行没有错误,每当我调用 MessageBox.Show(ConsoleTextBox.text)
它显示串口发送的文本,但文本框不显示它。
如果我有一个完全不同的方法来设置文本那么它有效吗?
可能是线程问题。这是来自 the documentation 的复制粘贴:
The DataReceived event is raised on a secondary thread when data is
received from the SerialPort object. Because this event is raised on a
secondary thread, and not the main thread, attempting to modify some
elements in the main thread, such as UI elements, could raise a
threading exception. If it is necessary to modify elements in the main
Form or Control, post change requests back using Invoke, which will do
the work on the proper thread.
P.S。在使用 System.IO.Ports.SerialPort 之前,请确保 to read this. And this。不幸的是,这些问题在 .NET 4.5.2 中仍未修复。不久前,我将我的串行通信代码从 C# 移植到 C++,以使我的软件能够为所有用户可靠地工作。但是,如果它可以在您的机器上运行,并且您没有使用商业软件,System.IO.Ports.SerialPort 可能仍然适合您。
我正在使用 Visual studio 2017,我有一个 RichTextBox,当我设置它时不显示文本:
public void ArduinoDataRecived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort TempSerial = (SerialPort)sender;
ConsoleTextBox.AppendText(TempSerial.ReadLine());
}
代码运行没有错误,每当我调用 MessageBox.Show(ConsoleTextBox.text)
它显示串口发送的文本,但文本框不显示它。
如果我有一个完全不同的方法来设置文本那么它有效吗?
可能是线程问题。这是来自 the documentation 的复制粘贴:
The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.
P.S。在使用 System.IO.Ports.SerialPort 之前,请确保 to read this. And this。不幸的是,这些问题在 .NET 4.5.2 中仍未修复。不久前,我将我的串行通信代码从 C# 移植到 C++,以使我的软件能够为所有用户可靠地工作。但是,如果它可以在您的机器上运行,并且您没有使用商业软件,System.IO.Ports.SerialPort 可能仍然适合您。