C# 为什么赢了';当我用不同的功能做完全相同的事情时,我的文本框不会更新吗?
我的文本框不会更新!我用它作为日志来更新其他正在做的事情 表格1代码:C# 为什么赢了';当我用不同的功能做完全相同的事情时,我的文本框不会更新吗?,c#,multithreading,class,delegates,invoke,C#,Multithreading,Class,Delegates,Invoke,我的文本框不会更新!我用它作为日志来更新其他正在做的事情 表格1代码: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Data.OleDb; using System.Collections.Specializ
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;
delegate void logAdd(string message);
namespace LCR_ShepherdStaffupdater_1._0
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public void logAdd(string message)
{
this.Log.Items.Add(message); // Doesnt add to textbox
this.Log.Items.Add("This won't update the textbox!!! Why?"); // Doesnt add to textbox
}
private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form aboutBox = new AboutBox1();
aboutBox.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
settingsForm.settings.ShowDialog();
}
private void synchronize_Click(object sender, EventArgs e)
{
this.Log.Items.Add("This WILL update the textbox. It is exactly the same as the other one..."); // This will add to the textbox
DatabaseHandling.createDataSet();
}
}
public class settingsForm
{
public static Form settings = new Settings();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCR_ShepherdStaffupdater_1._0
{
public class Logging
{
static Main mainClass = new Main();
static logAdd logAddDelegate;
public static void updateLog(string message)
{
logAddDelegate = mainClass.logAdd;
logAddDelegate(message);
}
}
}
我的日志类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCR_ShepherdStaffupdater_1._0
{
public class Logging
{
static Main mainClass = new Main();
static logAdd logAddDelegate;
public static void updateLog(string message)
{
logAddDelegate = mainClass.logAdd;
logAddDelegate(message);
}
}
}
我完全被难住了……有人知道我如何修复logAdd函数,以便最终更新我的文本框日志吗
编辑:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;
delegate void logAdd(string message);
namespace LCR_ShepherdStaffupdater_1._0
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public void logAdd(string message)
{
this.Log.Items.Add(message); // Doesnt add to textbox
this.Log.Items.Add("This won't update the textbox!!! Why?"); // Doesnt add to textbox
}
private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form aboutBox = new AboutBox1();
aboutBox.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
settingsForm.settings.ShowDialog();
}
private void synchronize_Click(object sender, EventArgs e)
{
this.Log.Items.Add("This WILL update the textbox. It is exactly the same as the other one..."); // This will add to the textbox
DatabaseHandling.createDataSet();
}
}
public class settingsForm
{
public static Form settings = new Settings();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCR_ShepherdStaffupdater_1._0
{
public class Logging
{
static Main mainClass = new Main();
static logAdd logAddDelegate;
public static void updateLog(string message)
{
logAddDelegate = mainClass.logAdd;
logAddDelegate(message);
}
}
}
我终于让它开始工作了,下面是解决方法:
表格1代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;
delegate void logAdd(string message);
namespace LCR_ShepherdStaffupdater_1._0
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public void add(string message)
{
this.Log.Items.Add(message);
}
public void logAdd(string message)
{
Log.Invoke(new logAdd(add), new object[] { message });
}
private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form aboutBox = new AboutBox1();
aboutBox.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
settingsForm.settings.ShowDialog();
}
private void synchronize_Click(object sender, EventArgs e)
{
logAdd("this works");
}
}
public class settingsForm
{
public static Form settings = new Settings();
}
}
日志记录代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;
delegate void logAdd(string message);
namespace LCR_ShepherdStaffupdater_1._0
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public void logAdd(string message)
{
this.Log.Items.Add(message); // Doesnt add to textbox
this.Log.Items.Add("This won't update the textbox!!! Why?"); // Doesnt add to textbox
}
private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form aboutBox = new AboutBox1();
aboutBox.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
settingsForm.settings.ShowDialog();
}
private void synchronize_Click(object sender, EventArgs e)
{
this.Log.Items.Add("This WILL update the textbox. It is exactly the same as the other one..."); // This will add to the textbox
DatabaseHandling.createDataSet();
}
}
public class settingsForm
{
public static Form settings = new Settings();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCR_ShepherdStaffupdater_1._0
{
public class Logging
{
static Main mainClass = new Main();
static logAdd logAddDelegate;
public static void updateLog(string message)
{
logAddDelegate = mainClass.logAdd;
logAddDelegate(message);
}
}
}
嗯…我可以说这是一个非常糟糕的方法…但直到我找到一个真正的解决方案,只要它起作用,我很高兴。谢谢你们的帮助 我以前也遇到过类似的问题,在写入文本框后,通过调用文本框上的
.Invalidate()
解决了这个问题
您还可以通过以下方式检查您的邮件是否实际到达文本框:
debug.WriteLine( this.Log.Items( this.Log.Items.Count-1));
希望能对您有所帮助。我以前也遇到过类似的问题,在写入文本框后,通过调用文本框上的
.Invalidate()
解决了这个问题
您还可以通过以下方式检查您的邮件是否实际到达文本框:
debug.WriteLine( this.Log.Items( this.Log.Items.Count-1));
希望对您有所帮助。您从哪个线程调用
Logging.updateLog()
?如果不是UI线程,我可以看到文本框在这种情况下不会得到更新。如果您尝试从synchronize\u Click()
调用logAdd()
(假设这是基于名称从UI线程调用的),那么它是否工作?您从哪个线程调用Logging.updateLog()
?如果不是UI线程,我可以看到文本框在这种情况下不会得到更新。如果您尝试从synchronize\u Click()
调用logAdd()
(假设这是基于名称从UI线程调用的),那么它是否工作?从您发布的代码中,您的静态记录器类正在创建主窗体的新实例。这与您在UI上运行的不同
总的来说,这不是一个很好的设计模式。如果您希望我详细说明并提供示例,请写一条注释。从您发布的代码中,您的静态记录器类正在创建主窗体的新实例。这与您在UI上运行的不同
总的来说,这不是一个很好的设计模式。如果您想让我详细说明并提供示例,请写一条评论。在正在运行的主窗体的实例上运行“同步”单击。logadd函数在日志代码中创建的单独主窗体上运行。为此,您需要将现有的主窗体传递到日志代码中,以便它更新您可以看到的文本框,而不是您没有看到的额外文本框。在正在运行的主窗体实例上运行“同步”单击。logadd函数在日志代码中创建的单独主窗体上运行。要使其工作,您需要将现有的主窗体传递到日志代码中,以便它更新您可以看到的文本框,不是你没有的额外文本框。当你从
synchronize\u Click
调用logAdd
时会发生什么?然后它工作:]这是一个线程问题。当你从synchronize\u Click
调用logAdd时会发生什么?然后它工作:]这是一个线程问题。如果有更好的方法,请告诉我!我知道我的设计模式很糟糕,但谷歌搜索“如何纠正糟糕的编程设计模式”并不像看上去那么容易。我不知道还有什么更好的!:(简短的回答是创建一个用于日志记录的接口。创建一个mainformlogging应用程序,您可以启动该应用程序,并将其分配到您的Program.cs文件中,该文件将知道您的MainForm(可能通过constructor),非静态的。然后你可以从静态类调用它。另外,我也不确定在这种情况下为什么需要委托。如果你的日志记录程序知道MainForm并且MainForm有一个公共logAdd。只需调用MainForm.logAdd(消息)如果有更好的方法,让我知道!我知道我的设计模式很糟糕,但谷歌搜索“如何纠正糟糕的编程设计模式”看起来并不容易。我不知道还有更好的方法(简短的回答是创建一个用于日志记录的接口。创建一个mainformlogging应用程序,您可以启动该应用程序,并将其分配到您的Program.cs文件中,该文件将知道您的MainForm(可能通过constructor),非静态的。然后你可以从静态类调用它。另外,我也不确定在这种情况下为什么需要委托。如果你的日志记录程序知道MainForm并且MainForm有一个公共logAdd。只需调用MainForm.logAdd(消息)只是一些我放在一起的东西,希望它能帮助我无效,但不起作用…而且我肯定我的消息没有到达文本框。这是因为我声明了一个新表单。只是一个设计缺陷…如何更正它?没有线索--还没有。尝试无效,但不起作用…我肯定我的消息没有到达文本框。这是因为我是declari正在生成一个新表单。只是一个设计缺陷…如何更正它?还没有线索。您可以使用Control.Invoke()在创建控件的线程上调用委托。这是必需的,因为WinForms控件只能在创建它们的线程上访问。谢谢!我想是时候用谷歌搜索了,因为我不知道如何使用control.Invoke()。我发现了…可能有点草率…但我做到了。你可以使用control.Invoke()在创建控件的线程上调用委托。这是必需的,因为WinForms控件只能在创建它们的线程上访问。谢谢!我想是时候用谷歌搜索了,因为我不知道如何使用control.Invoke()。我想出来了…可能太草率了…但我做到了。