将数据传递给现有表单 [英] Pass data to a existing form

查看:131
本文介绍了将数据传递给现有表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序需要来自另一个表单的数据,我需要通过它而不创建第一个表单的新实例。我怎么能这样做?



ex。

  Form2 f2 = new Form2(); 

f2.showdialog();

当f2显示时,它们是带有项目的列表视图,当我点击一个项目时,我想要项目被发送回第一个窗体而不关闭窗体2并且没有实例化一个新的窗体1.

解决方案

您正在寻找某种

 公共事件EventHandler MyButtonClicked; 

在使用ShowDialog()之前;

  Form2 f2 = new Form2(); 
f2.MyButtonClicked + = f2_MyButtonClicked;
f2.showdialog();

处理事件的地方

 

$ $ $ $ b $ $ $ $
$ $ $ $
$ $ $ b $ $你可以从发件人处获得f2。
Form2 f2 =(Form2)sender;

抛出新的NotImplementedException();
}

当您在Form2中点击您引发活动的按钮时。

  void myButtonClicked(object sender,EventArgs e)
{
if(MyButtonClicked!= null)
{
MyButtonClicked(this,new EventArgs());


code


$ b如果你想了解更多关于事件的信息, at。


http://msdn.microsoft.com/zh-CN/library/aa645739%28v=vs.71%29.aspx


I have a program that needs a data from another form and I need to pass it without creating a new instance of the first form. how can i do that?

ex.

Form2 f2 = new Form2();

f2.showdialog();

when f2 shows, their is an listview with items, when I click on an item, I want the item to be sent back to the first form without closing form2 and without instantiating a new Form1.

解决方案

You are looking for some kind of event.

public event EventHandler MyButtonClicked;

And before you use ShowDialog();

Form2 f2 = new Form2();
f2.MyButtonClicked += f2_MyButtonClicked;
f2.showdialog();

And somewhere to handle the event

void f2_MyButtonClicked(object sender, EventArgs e)
{
    //Here you want to grab your list. You can get f2 from sender.
    Form2 f2 = (Form2)sender;

    throw new NotImplementedException();
}

When you in Form2 click the button you raise the event.

void myButtonClicked(object sender, EventArgs e)
{
    if (MyButtonClicked != null)
    {
        MyButtonClicked(this, new EventArgs()); 
    }
}

If you want to read more on events have a look at.

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx

这篇关于将数据传递给现有表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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