我正在尝试将一个文本框从一个名为“hex”的类更新到主窗体。在“十六进制”类中,我有以下代码:
Main m = new Main();
m.updateTextBox(convertedHex);
Run Code Online (Sandbox Code Playgroud)
代码将变量传递给主窗体到名为“updateTextBox”的方法,如下所示:
public void updateLog(string input)
{
textBox2.AppendText(input);
}
Run Code Online (Sandbox Code Playgroud)
对不起,如果这似乎是一个愚蠢的问题,我已经被困了一段时间,我谷歌搜索上的所有链接现在都是紫色的,所以我希望有人能向我解释这一点。非常感谢。
在创建 textBox 的 Main 类中添加这种方法并从外部调用它。
假设您在Program.cs类中添加了代码以开始新的
// Add this code in Program.cs (or similar where you start the gui
public static readonly Main MainLogWindow = new Main();
// Add this code in Main.cs class
private delegate void NameCallBack(string varText);
public void UpdateTextBox(string input) {
if (InvokeRequired) {
textBox.BeginInvoke(new NameCallBack(UpdateTextBox), new object[] {input});
} else {
textBox.Text = input;
// textBox.Text = textBox.Text + Environment.NewLine + input // This might work as append in next line but haven't tested so not sure
}
}
Run Code Online (Sandbox Code Playgroud)
称之为:Program.MainLogWindow.UpdateTextBox("test test");从任何地方假设你已经MainLogWindow打开
这也将允许您从其他线程中调用更新。
| 归档时间: |
|
| 查看次数: |
15270 次 |
| 最近记录: |