似乎在使用时,System.Windows.Forms.RichTextBox您可以使用textbox.AppendText()或textbox.Text = ""在文本框中添加文本。
AppendText 将滚动到底部,并且直接添加文本将不会滚动,但是当用户将文本框放在焦点上时将跳到顶部。
这是我的功能:
// Function to add a line to the textbox that gets called each time I want to add something
// console = textbox
public void addLine(String line)
{
// Invoking since this function gets accessed by another thread
console.Invoke((MethodInvoker)delegate
{
// Check if user wants the textbox to scroll
if (Settings.Default.enableScrolling)
{
// Only normal inserting into textbox here with AppendText()
}
else
{
// This is the …Run Code Online (Sandbox Code Playgroud) 我的文本框出了问题.
我有一个表示GUI线程和工作线程,做一些网络的东西一类,然后必须将日志添加到GUI线程文本框,所以你可以看到什么是在后台发生的一类.
但是,我遇到的问题是GUI上没有任何反应,只有调用addLine()的调试信息才在控制台中.
应该添加日志的方法addLine()被调用,但似乎AppendText()什么都不做.
我很确定这必须与线程有关,但我不确定如何解决这个问题.
这是代码:
工人线程:
Form1 form = new Form1();
// This method gets called in the worker thread when a new log is available
private void HandleMessage(Log args)
{
// Using an instance of my form and calling the function addLine()
form.addLine(args.Message);
}
Run Code Online (Sandbox Code Playgroud)
GUI线程:
// This method gets called from the worker thread
public void addLine(String line)
{
// Outputting debug information to the console to see if the function gets called, it does get called
Console.WriteLine("addLine …Run Code Online (Sandbox Code Playgroud)