在两种形式之间传递数据 [英] Passing data between two forms

查看:109
本文介绍了在两种形式之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体(mainform)上有一个列表框,另一个窗体(form1)上有两个文本框.单击按钮后,我希望读取来自两个文本框的数据并以mainform形式显示在ListBox上.
我该如何实现?

I have one form(mainform) with a listbox on it and another form(form1) with two TextBoxes. Upon the click of a button I want the data from the two textboxes to be read and displayed on he ListBox in form mainform.
How can I achieve that?

Thanks in advance!

推荐答案

这将取决于您希望form1的工作方式:

如果按下按钮时form1也关闭,则设置两个属性以提供对TextBox内容的访问并从mainForm中读取它们.我建议在这种情况下使用form1.ShowDialog,而不是form1.Show.

如果未关闭form1,则最好的方法是在form1中设置一个由mainForm处理的事件.
form1引发事件,并通过EventArgs提供数据:

形式1:

It will depend on how you want the form1 to work:

If form1 closes as well when you press the button, then set up two properties which provide access to the TextBox content and read them from mainForm. I would suggest using form1.ShowDialog in this case, rather than form1.Show.

If form1 doesn''t close, then the best way is to set up an event in form1, which mainForm handles.
form1 throws the event, and provides the data via the EventArgs:

In form1:

public partial class form1 : 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)

在mainForm中:


----- The assign 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 mainForm:

public mainForm()
    {
    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)
    {
    }


*******************************************
您可以通过EventArgs参数以这种方式传递数据:
形式1:


***************************************
You can pass data in this way via the EventArgs parameter:
form1:

public partial class form1 : Form
    {
    public event EventHandler<ChangedArgs> Changed;

    private void butGo_Click(object sender, EventArgs e)
        {
       EventHandler ch = Changed;
       if (ch != null)
          {
          ch(this, new ChangedArgs(tbData.Text));
          }
        }
    }

public partial class ChangedArgs : EventArgs
    {
    public string strData;
    public ChangedArgs(string str)
        {
        strData = str;
        }
    }



mainForm:



mainForm:

public partial class mainFrom : Form
    {
    form1 otherForm = new form1();
    private void mainForm_Load(object sender, EventArgs e)
        {
        otherForm.Changed += new EventHandler<ChangedArgs>(Changed);
        otherForm.Show();
        }

    private void Changed(object sender, ChangedArgs e)
        {
        if (e != null)
            {
            tbData.Text = e.strData;
            }
        }
    }


看看 [ ^ ]


实例化form1之后,将mainform的引用传递给form1.您可以通过定义公共属性,字段,方法或什至在Form1的构造函数中实现此目的(然后在实例化期间而不是之后). Mainform的类型需要具有一个公开属性,以公开ListBox.

干杯!
After instantiating form1 pass a reference of mainform to form1. You can achieve that by defining a public property, field or by a method or even in the constructor of Form1 (then it would be during instantiation and not after). The type of mainform needs to have a public property that exposes the ListBox.

Cheers!


这篇关于在两种形式之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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