gpt4 book ai didi

c# - 我正在尝试在一个类中创建一个方法并尝试通过单击按钮从另一个类(表单)调用

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

这是我的第一个类:

namespace WindowsFormsApplication2 
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
/*_enemy = new Class1(this);
int y = Class1.MyMethod(0);
textBox1.Text = Convert.ToString (y);*/
}
private Class1 _enemy;

private void button1_Click(object sender, EventArgs e)
{
_enemy = new Class1(this);
int y = Class1.MyMethod();
textBox1.Text = Convert.ToString(y);
}
}
}

这是我的第二堂课:

namespace WindowsFormsApplication2
{

public class Class1
{
public Class1( Form1 form )
{
_form1 = form;
}
public static int MyMethod()
{
int i = 0;
for (int j = 1; j <= 20; j++)
{
i = j;
//Thread.Sleep(100);
}
return i;
}
}

// DON'T initialize this with new Form1();
private Form1 _form1;
}

程序运行正常,我在 TextBox 中只得到 20 个输出。我想要的是每次循环运行时的输出。

喜欢 1,2,3,.........20 并停止。

This is the design of the form

也许像一个柜台。我也尝试过使用 Timer 但无法做到这一点。

编辑:

@Mong Zhu 我已经交叉检查了代码,仍然得到异常。

完整的代码供您引用:

Form1.cpp

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Class1 MyCounterClass;
private void Form1_Load(object sender, EventArgs e)
{
MyCounterClass = new Class1();
// register the event. The method on the right hand side
// will be called when the event is fired
MyCounterClass.CountEvent += MyCounterClass_CountEvent;
}

private void MyCounterClass_CountEvent(int c)
{
if (textBox1.InvokeRequired)
{
textBox1.BeginInvoke(new Action(() => textBox1.Text = c.ToString()));
}
else
{
textBox1.Text = c.ToString();
}
}

public Form1()
{
InitializeComponent();
}
private Class1 _enemy;

private void button1_Click(object sender, EventArgs e)
{
MyCounterClass.MyCountMethod(300, 0, 10);
}

}
}

和class1.cpp

namespace WindowsFormsApplication2
{
public class Class1
{
public delegate void Counter(int c); // this delegate allows you to transmit an integer


public event Counter CountEvent;

public Class1()
{

}
public void MyCountMethod(int interval_msec, int start, int end)
{
System.Threading.Thread t = new System.Threading.Thread(() =>
{
for (int i = start; i <= end; i++)
{
// Check whether some other class has registered to the event
if (CountEvent != null)
{
// fire the event to transmit the counting data
CountEvent(i);
System.Threading.Thread.Sleep(interval_msec);
}
}
});
// start the thread
t.Start();
}

// DON'T initialize this with new Form1();
private Form1 _form1;
}
}

最佳答案

如果您希望将某个对象的进度报告回您的表单,您可以使用 IProgress<T>接口(interface)代替。解释的很好herehere但是要将它翻译成你给定的代码,它看起来像这样:

public partial class Form1 : Form
{
private async void button1_Click(object sender, EventArgs e)
{
Progress<int> reporter = new Progress<int>(number =>
{
textBox1.Text = number.ToString();
});
await Task.Run(() => MyClass1.MyMethod(reporter));
}
}

public class Class1
{
public static int MyMethod(IProgress<int> reporter)
{
for (int i = 1; i <= 20; ++i)
{
reporter.Report(i);
//Thread.Sleep(100);
}
return i;
}
}

注意

  • Class1不需要任何关于 Form1 的知识.
  • Class1.MyMethod是静态的,您不需要它的实例。如果你想修改 Class1 中的字段/属性,你需要一个实例。这是否正确完全取决于您。
  • IProgress<T>需要 .NET Framework 4.5

关于c# - 我正在尝试在一个类中创建一个方法并尝试通过单击按钮从另一个类(表单)调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42345671/

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