gpt4 book ai didi

C#更新文本框时看不到文本

转载 作者:太空宇宙 更新时间:2023-11-03 22:03:19 25 4
gpt4 key购买 nike

我想知道是否有人可以帮助我解决我遇到的一个小问题。我正在尝试更新另一个类的文本框,但文本没有显示在文本框中,即使它是在我将它打印到屏幕上时发送的。

我使用的代码如下:

程序.cs

namespace Search
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]

static void Main()
{
Application.EnableVisualStyles();

try
{
Application.SetCompatibleTextRenderingDefault(false);
}
catch (InvalidOperationException e)
{

}
Application.Run(new Form1());
}

public static readonly Form1 MainLogWindow = new Form1();
}
}

十六进制转ASCII:

public class HexToASCII
{
Output o = new Output();

public void hexToAscii(String hex, int textBox)
{
//Convert the string of HEX to ASCII
StringBuilder sb = new StringBuilder();

for (int i = 0; i < hex.Length; i += 2)
{
string hs = hex.Substring(i, 2);
sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));
}
//Pass the string to be output
string convertedHex = sb.ToString();

Program.MainLogWindow.UpdateTextBox(convertedHex);
}
}

表格 1:

    private delegate void NameCallBack(string varText);
public void UpdateTextBox(string input)
{
if (InvokeRequired)
{
textBox2.BeginInvoke(new NameCallBack(UpdateTextBox), new object[] { input });
}
else
{
textBox2.Text = textBox2.Text + Environment.NewLine + input;
}
}

我尝试使用新线程 ThreadStart ts = delegate()... 运行它,但我无法更新文本框。抱歉,我是 c# 的新手,有人可以解释一下这个问题,这样我就可以理解它并为下次学习。非常感谢:)

最佳答案

问题是:

static void Main()
{
...
Application.Run(new Form1());
}

public static readonly Form1 MainLogWindow = new Form1();

您正在创建两个表单:正在显示其中一个(使用 Application.Run),但您正在更改另一个表单上的文本框的内容:

Program.MainLogWindow.UpdateTextBox(convertedHex);  

您还没有展示您首先是如何调用 hexToAscii - 就我个人而言,我会尽量避免像这样对 GUI 元素进行静态引用,但是您可以 只需更改要使用的 Main 方法,即可让您的代码正常工作:

Application.Run(MainLogWindow);

关于C#更新文本框时看不到文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9352210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com