如何将数据从一个Windows窗体发送到同一项目中的另一个窗体? [英] How to send data from one Windows Form to another within the same project?

查看:125
本文介绍了如何将数据从一个Windows窗体发送到同一项目中的另一个窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用其中包含更多窗体的Winform应用程序.

我想在两种形式之间发送和接收数据.任何人都可以帮忙吗???????

I am working with an Winform application that has more Forms in it.

I want to send and receive data between two forms. Can anyone help???????

推荐答案

这是有关表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA


为此,一种形式必须知道另一种形式的存在,这通常是一个坏主意,除非一种形式直接打开另一种形式.
如果Form1打开Form2,则有两种传递信息的方法,具体取决于是否用Show或ShowDialog显示Form2:

ShowDialog很简单:在Form2中创建一个(或多个)公共属性,在调用ShowDialog之前将值从Form1加载到属性中,并在返回时根据需要取回它们.

Show很难-在调用Show之前,您需要在Form2中创建一个Form1所属的事件,然后可以通过自定义EventArgs或上述属性将数据从Form2传递到Form1.

在子窗体中:

In order to do this, one form must know about the existance of the other, which is generally a bad idea unless one opens the other directly.
If Form1 opens Form2, then there are two ways to pass information, depending on whether Form2 is displayed with Show or ShowDialog:

ShowDialog is easy: Create a public property (or several) in Form2, load the values into the property from Form1 before calling ShowDialog and fetch them back as necessary when it returns.

Show is harder - you need to create an event in Form2 which Form1 subsribes to before calling Show - you can then pass data from Form2 to Form1 either via a custom EventArgs, or via properties as above.

In the child form:

public partial class frmChild : Form
   {
   // Signal file change happened
   public event EventHandler Changed;

   protected virtual void OnChanged(EventArgs e)
      {
      EventHandler eh = Changed;
      if (eh != null)
         {
         eh(this, e);
         }
      }

   private void DoSomethingToChangeData()
      {
      OnChanged(null);
      }
   }



-----给eh的指示是如果处理程序在空检查之间进行更改
-----和执行.
-----(不太可能,但可能)

-----空检查是为了确保有处理程序.如果不是,最好
-----优雅地忽略它,而不是依靠抛出的异常
-----(NullReferenceException)

在家长表格中:




----- The asign to eh is in case the handler changes between null check
----- and exec.
----- (unlikely, but possible)

----- The null check is to ensure there is a handler. If not, better to
----- ignore it gracefully, than to rely on the thrown exception
----- (NullReferenceException)

In the Parent form:


public frmParent()
    {
    frmChild.Change += new frmChange.ChangeHandler(Changed);
    }

private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    frmChild fd = new frmChild();
    fd.ShowDialog();
    }

//
// Fired when the file is changed at a lower level.
//
private void Changed(object sender, EventArgs e)
    {
    }


您应该具有可以通过引用以两种形式使用的域级对象.表单可供用户与域级别的对象进行交互.表单不应存储"数据或操纵数据.如果您坚持使用这种类型的模型,那么任何给定的表格都可以允许用户与其他表格一样的相同数据进行交互.
You should have a domain level object that can be used via reference in both forms. Forms are there for users to interact with domain level objects. Forms should not "store" data or manipulate data. If you stick to this type of model then any given form can allow the user to interact with the same data any other form can.


这篇关于如何将数据从一个Windows窗体发送到同一项目中的另一个窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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