使用事件处理程序在表单之间传递变量 [英] Using Event Handler to pass variable between forms

查看:76
本文介绍了使用事件处理程序在表单之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表格。单击Form1上的菜单项打开Form2。 Form2有一个文本框来输入int值。在按钮单击时,将检查该值是否为null,然后需要传递回Form 1,在那里它用于创建SQL查询。另一个线程声明
使用事件处理程序而不是声明全局变量,但是我遇到了问题(我是C#的新手虽然知道其他语言)

I have 2 forms. Clicking a menu item on Form1 opens Form2. Form2 has a text box to enter a int value. On a button click, the value is checked for not null, and then needs passed back to Form 1 where it is used to create a SQL query. Another thread stated to use Event Handler instead of declaring global variables, but I am having issues accomplishing that (I am new to C# although know other languages)

我有什么:

Form1

private void ToolStripMenuItem_Click(object sender,EventArgs e)

{

FormAPI APIFrm = new APIForm();

APIForm.AdviseParentEventHandler + = new EventHandler< AdviseParentEventArgs>(APIFrm.AdviseParent);

APIForm.Show();

}

private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormAPI APIFrm = new APIForm();
APIForm.AdviseParentEventHandler += new EventHandler<AdviseParentEventArgs>(APIFrm.AdviseParent);
APIForm.Show();
}

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; public void 2Frm_AdviseParent(object sender,AdvisePArentEventArgs e)

{

int api1 = api;

sSql =" select fields  FROM TABLE其中apino ='" + api1.value +"'" ;;

LoadW(sSql);

formatdgvW();

}

                  public void 2Frm_AdviseParent(object sender, AdvisePArentEventArgs e)
{
int api1 = api;
sSql = "select fields  FROM TABLE where apino = '" + api1.value + "'";
LoadW(sSql);
formatdgvW();
}

表格 2

推荐答案

那是什么其他线索?知道在其他帖子中说了什么会有所帮助。

What other thread is that? It would help to know what was said in that other thread.

表单之间有很多通信方式。这是一个非常常见的初学者问题,有很多文章和以前的答案。

There are many ways to communicate between forms. It is a very common beginner question and there are very many articles and previous answers.

一个常见的解决方案是为数据创建一个类并将类的实例传递给Form2。如果它只是一个字段,那么您可以将字段传递给Form2。无论哪种方式,您都可以创建一个用于传递数据的构造函数。

One common solution is to create a class for the data and to pass an instance of the class to Form2. If it is just one field then you can pass the field to Form2. Either way, you can create a constructor for passing the data.

或者您可以创建一个"属性"。在Form2中。表单关闭后,Form2的实例仍然存在。 Form1可以使用
public 属性来获取数据。

Or you can create a "property" in Form2. The instance of Form2 still exists after the form is closed. Form1 can use the public property to get the data.

正如我所说,有很多例子。您对此有何疑问?

As I said, there are many examples of all that. Do you have any questions about any of that?

建议使用EventHandler类的人似乎有误解。 EventHandler类派生自委托类。 EventHandler类设计用于不同的用途。你想要的是一个回调。您可以使用
创建一个委托,无论您需要什么参数和返回值。这有助于了解它的建议原因,因为它似乎没有必要用于此目的。

Whoever suggested using the EventHandler class seems to have misunderstood. The EventHandler class is derived from the delegate class. The EventHandler class is designed for different uses. What you want is called a callback. You can create a delegate with whatever arguments and return value you need. It would help to know why it was suggested because it seems unnecessary for this purpose.


这篇关于使用事件处理程序在表单之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆