gpt4 book ai didi

c# - 更新 GUI 形式单独的类

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

所以过去几个小时我一直在搜索,阅读有关如何从另一个类更新表单 GUI 的所有内容。我尝试了 backgroundworker 和 Invoke,但似乎没有任何效果,或者我没有做对。 (我对 c# 还是很陌生)那么..为什么这个方法根本不起作用?

表格 1:

 private void button2_Click_1(object sender, EventArgs e)
{
prog.stuff();
}
public void Updateprogressbar(int input)
{
progressBar1.Value = input;

}

类(class)计划

 public static void stuff()
{
Form1 f = new Form1();
int up = 100;
f.Updateprogressbar(up);
}

我知道这可能是一个非常简单的问题,但我还是想不通。进度条不会更新。我确实在属性中启用了所有公开功能。不管怎么说,还是要谢谢你。

最佳答案

private void button2_Click_1(object sender, EventArgs e)
{
prog.stuff(this);
}
public void Updateprogressbar(int input)
{
progressBar1.Value = input;

}


public static void stuff(Form f)
{
int up = 100;
f.Updateprogressbar(up);
}

所以您可以看到您的代码不起作用的原因是因为您实例化了一个仅在 stuff() 方法中有效的 Form1 的新实例。在我的代码中,我通过允许我访问 Form1 的方法,将 Form1 的引用传递到类 Prog.Stuff 中。

关于c# - 更新 GUI 形式单独的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004331/

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