我试图在一个类中创建一个方法,并试图在单击按钮时从另一个类(窗体)调用 [英] I am trying to create a method in one class and trying to call from another class (form) on click of a button
问题描述
这是我的第一堂课:
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个输出.我想要的是每次循环运行时的输出.
The program is running correctly and I am getting only 20 as output in the TextBox
. What I want is the output each time the loop runs.
像 1,2,3,......... 20
并停止.
也许像柜台.我也尝试使用 Timer
,但无法做到这一点.
Like a counter maybe. I also tried using Timer
but couldn't do that.
@Mong Zhu我已经交叉检查了代码,仍然遇到异常.
@Mong Zhu I have cross checked the code, still getting the exception.
完整的代码供您参考:
Form1.cpp
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
and 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>
界面.在此处和
If you are looking to report progress from some object back to your form you can use the IProgress<T>
interface instead. It is well explained here and here but to translate it to your given code it would look something like this:
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
Class1
does not need any knowledge ofForm1
.- Since
Class1.MyMethod
is static you do not require an instance of it. If you want to modify fields/properties in Class1 you need an instance. It's really up to you if this is correct or not. IProgress<T>
requires .NET Framework 4.5
这篇关于我试图在一个类中创建一个方法,并试图在单击按钮时从另一个类(窗体)调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!