如何将值从一种形式传递到另一种形式?

时间:2022-04-05 06:59:37

Consider the situation where I have two windows forms, say F1 and F2. After using F1, I have now called F2.ShowDialog(). This puts F2 on the screen as well. Now that both forms are visible, how can I pass data from F1 to F2? Additionally, once F2 (A modal dialog) finishes, how can I return data to F1?

考虑我有两种窗体形式的情况,比如F1和F2。使用F1之后,我现在调用了F2.ShowDialog()。这也将F2放在屏幕上。既然两种形式都可见,我怎样才能将数据从F1传递到F2?此外,一旦F2(模态对话框)完成,我如何将数据返回F1?

10 个解决方案

#1


Has anyone considered simply passing the value to the form in the tag attribute.

有没有人考虑过将值传递给tag属性中的表单。

Form newForm = new form();
newForm.Tag = passValue;
newform.showmodal();

when newform is shown the load (or any other) routine can use the data in tag

当显示newform时,加载(或任何其他)例程可以使用标记中的数据

public void load()
{
  if (this.Tag.length > 0)
  {
     // do something with the data
  }
}

#2


Add this code in the relevant place in your from f1.

将此代码添加到f1中的相关位置。

Form f2 = new Form();
f2.ShowDialog();

HTH

#3


int x=10;
Form1 f2 = new Form1(x);
f2.ShowDialog();

This is wrote in the form who will pass the value. To recive this value you must make new constructor in the recieved form

这是以传递值的形式写的。要重新接收此值,您必须在接收的表单中创建新的构造函数

and that will be like that

那将是那样的

public Form2(int x)
  {
        InitializeComponent();
        this.x = x;
 }

and will be notice in the first form when you create the instance from form2 you have two choice on of them one of them let you to pass value

当你从form2创建实例时,你会在第一个表单中注意到你有两个选择,其中一个让你传递值

#4


Let's say you have a TextBox control in Form1, and you wish to pass the value from it to Form2 and show Form2 modally.

假设您在Form1中有一个TextBox控件,并且您希望将值传递给Form2并以模态方式显示Form2。

In Form1:

private void ShowForm2()
{
    string value = TheTextBox.Text;
    Form2 newForm = new Form2();
    newForm.TheValue = value;
    newForm.ShowDialog();
}

In Form2:

private string _theValue;
public string TheValue 
{ 
    get
    {
        return _theValue;
    }
    set
    {
        _theValue = value; 
        // do something with _theValue so that it
        // appears in the UI

    }
}

This is a very simple approach, and might not be the best for a larger application (in which case you would want to study the MVC pattern or similar). The key point is that you do things in the following order:

这是一种非常简单的方法,对于更大的应用程序可能不是最好的(在这种情况下,您可能希望研究MVC模式或类似方法)。关键是你按以下顺序做事:

  1. Create an instance of the form to show
  2. 创建要显示的表单的实例

  3. Transfer data to that new form instance
  4. 将数据传输到该新表单实例

  5. Show the form
  6. 显示表格

When you show a form modally it will block the code in the calling form until the new form is closed, so you can't have code there that will transfer information to the new form in a simple way (it's possible, but unnecessarily complicated).

当您以模态方式显示表单时,它将阻止调用表单中的代码,直到新表单关闭为止,因此您无法在那里使用能够以简单方式将信息传输到新表单的代码(这可能,但不必要地复杂) 。

[edit: extended the property methods in Form2 to be more clear]

[编辑:扩展Form2中的属性方法更加清晰]

#5


If you just want to push data to your child dialog, consider adding parameters to the child dialog's constructor, then calling ShowDialog(). Passing data the other way, though, is a little trickier.

如果您只想将数据推送到子对话框,请考虑向子对话框的构造函数添加参数,然后调用ShowDialog()。但是,以另一种方式传递数据有点棘手。

#6


let me reframe the question i ve 2 form f1, f2... i cal Form f2 = new Form(); f2.ShowDialog();

让我重新构思我的问题,形式为f1,f2 ... i cal表格f2 = new Form(); f2.ShowDialog();

// now i need to pass the parameter from f1 window to f2 (which is modal dialog box) also return value from f2 form to f1

//现在我需要将参数从f1窗口传递给f2(这是模态对话框)也将值从f2格式返回到f1

//now 'm using varibles that are in common namespace (for both f1 , f2)

//现在使用共同命名空间中的变量(对于f1,f2)

#7


Consider to use the MVC pattern, i.e. instead of having too much data in forms and passing them around, use model classes that keep to keep the stuff.

考虑使用MVC模式,即不是在表单中包含太多数据并传递它们,而是使用保持这些东西的模型类。

#8


Use a defined Type for your information (class, struct...) and declare a variable of that in Form1

使用定义的Type作为您的信息(class,struct ...)并在Form1中声明一个变量

struct myData
{
    String str1;
    String str2;
}

Public Class Form1
{
  Public myData dat;
}

(Note: the type shouldnt really be public, this is just for the sake of this example) Thus the data sits within Form1. Modify the constructor of Form2 to accept a parameter of type Form1.

(注意:类型不应该是公开的,这只是为了这个例子)因此数据位于Form1中。修改Form2的构造函数以接受Form1类型的参数。

public Form2(Form1 frm1)
{
    mFrm1 = frm1;
    InitializeComponent();
}

Now, when you call form2, send in the very instance of Form1 that's making the call:

现在,当您调用form2时,请发送正在进行调用的Form1实例:

Form2 frm2 = new Form2(this);
frm2.ShowDialog();

Now, when execution has reached Form2, you can access the MyData within Form1:
mFrm1.dat;

现在,当执行到达Form2时,您可以访问Form1中的MyData:mFrm1.dat;

In this way, both instances of Form1 and Form2 will be referencing the data that's in one place. Making changes/updates will be available for both instances of the forms.

这样,Form1和Form2的两个实例都将引用一个位置的数据。对表格的两个实例都可以进行更改/更新。

#9


May be I am late. but all those who may want.

可能是我迟到了。但所有可能想要的人。

In destination form have a constructor defined like this

在目标表单中有一个像这样定义的构造函数

public partial class Destination: Form
{
    string valueAccepted;
    public Destination(string _valuePassed)
    {
        InitializeComponent();
        this.valueAccepted= _valuePassed;
    }
}

and in Source Form call the form like this

并在源表单中调用这样的表单

        Source sourceForm= new Source ("value Passed");
        sourceForm.ShowDialog();

this way "value Passed" is passed from Form Source to Form Destination

这样,“值传递”从表单源传递到表单目标

#10


There are multiple ways to pass data between 2 forms check these links which has example videos to do this

有两种方法可以在两个表单之间传递数据,请检查这些链接,这些链接包含示例视频

-FormToForm Using Properties - http://windowsclient.net/learn/video.aspx?v=108089

-FormToForm使用属性 - http://windowsclient.net/learn/video.aspx?v=108089

#1


Has anyone considered simply passing the value to the form in the tag attribute.

有没有人考虑过将值传递给tag属性中的表单。

Form newForm = new form();
newForm.Tag = passValue;
newform.showmodal();

when newform is shown the load (or any other) routine can use the data in tag

当显示newform时,加载(或任何其他)例程可以使用标记中的数据

public void load()
{
  if (this.Tag.length > 0)
  {
     // do something with the data
  }
}

#2


Add this code in the relevant place in your from f1.

将此代码添加到f1中的相关位置。

Form f2 = new Form();
f2.ShowDialog();

HTH

#3


int x=10;
Form1 f2 = new Form1(x);
f2.ShowDialog();

This is wrote in the form who will pass the value. To recive this value you must make new constructor in the recieved form

这是以传递值的形式写的。要重新接收此值,您必须在接收的表单中创建新的构造函数

and that will be like that

那将是那样的

public Form2(int x)
  {
        InitializeComponent();
        this.x = x;
 }

and will be notice in the first form when you create the instance from form2 you have two choice on of them one of them let you to pass value

当你从form2创建实例时,你会在第一个表单中注意到你有两个选择,其中一个让你传递值

#4


Let's say you have a TextBox control in Form1, and you wish to pass the value from it to Form2 and show Form2 modally.

假设您在Form1中有一个TextBox控件,并且您希望将值传递给Form2并以模态方式显示Form2。

In Form1:

private void ShowForm2()
{
    string value = TheTextBox.Text;
    Form2 newForm = new Form2();
    newForm.TheValue = value;
    newForm.ShowDialog();
}

In Form2:

private string _theValue;
public string TheValue 
{ 
    get
    {
        return _theValue;
    }
    set
    {
        _theValue = value; 
        // do something with _theValue so that it
        // appears in the UI

    }
}

This is a very simple approach, and might not be the best for a larger application (in which case you would want to study the MVC pattern or similar). The key point is that you do things in the following order:

这是一种非常简单的方法,对于更大的应用程序可能不是最好的(在这种情况下,您可能希望研究MVC模式或类似方法)。关键是你按以下顺序做事:

  1. Create an instance of the form to show
  2. 创建要显示的表单的实例

  3. Transfer data to that new form instance
  4. 将数据传输到该新表单实例

  5. Show the form
  6. 显示表格

When you show a form modally it will block the code in the calling form until the new form is closed, so you can't have code there that will transfer information to the new form in a simple way (it's possible, but unnecessarily complicated).

当您以模态方式显示表单时,它将阻止调用表单中的代码,直到新表单关闭为止,因此您无法在那里使用能够以简单方式将信息传输到新表单的代码(这可能,但不必要地复杂) 。

[edit: extended the property methods in Form2 to be more clear]

[编辑:扩展Form2中的属性方法更加清晰]

#5


If you just want to push data to your child dialog, consider adding parameters to the child dialog's constructor, then calling ShowDialog(). Passing data the other way, though, is a little trickier.

如果您只想将数据推送到子对话框,请考虑向子对话框的构造函数添加参数,然后调用ShowDialog()。但是,以另一种方式传递数据有点棘手。

#6


let me reframe the question i ve 2 form f1, f2... i cal Form f2 = new Form(); f2.ShowDialog();

让我重新构思我的问题,形式为f1,f2 ... i cal表格f2 = new Form(); f2.ShowDialog();

// now i need to pass the parameter from f1 window to f2 (which is modal dialog box) also return value from f2 form to f1

//现在我需要将参数从f1窗口传递给f2(这是模态对话框)也将值从f2格式返回到f1

//now 'm using varibles that are in common namespace (for both f1 , f2)

//现在使用共同命名空间中的变量(对于f1,f2)

#7


Consider to use the MVC pattern, i.e. instead of having too much data in forms and passing them around, use model classes that keep to keep the stuff.

考虑使用MVC模式,即不是在表单中包含太多数据并传递它们,而是使用保持这些东西的模型类。

#8


Use a defined Type for your information (class, struct...) and declare a variable of that in Form1

使用定义的Type作为您的信息(class,struct ...)并在Form1中声明一个变量

struct myData
{
    String str1;
    String str2;
}

Public Class Form1
{
  Public myData dat;
}

(Note: the type shouldnt really be public, this is just for the sake of this example) Thus the data sits within Form1. Modify the constructor of Form2 to accept a parameter of type Form1.

(注意:类型不应该是公开的,这只是为了这个例子)因此数据位于Form1中。修改Form2的构造函数以接受Form1类型的参数。

public Form2(Form1 frm1)
{
    mFrm1 = frm1;
    InitializeComponent();
}

Now, when you call form2, send in the very instance of Form1 that's making the call:

现在,当您调用form2时,请发送正在进行调用的Form1实例:

Form2 frm2 = new Form2(this);
frm2.ShowDialog();

Now, when execution has reached Form2, you can access the MyData within Form1:
mFrm1.dat;

现在,当执行到达Form2时,您可以访问Form1中的MyData:mFrm1.dat;

In this way, both instances of Form1 and Form2 will be referencing the data that's in one place. Making changes/updates will be available for both instances of the forms.

这样,Form1和Form2的两个实例都将引用一个位置的数据。对表格的两个实例都可以进行更改/更新。

#9


May be I am late. but all those who may want.

可能是我迟到了。但所有可能想要的人。

In destination form have a constructor defined like this

在目标表单中有一个像这样定义的构造函数

public partial class Destination: Form
{
    string valueAccepted;
    public Destination(string _valuePassed)
    {
        InitializeComponent();
        this.valueAccepted= _valuePassed;
    }
}

and in Source Form call the form like this

并在源表单中调用这样的表单

        Source sourceForm= new Source ("value Passed");
        sourceForm.ShowDialog();

this way "value Passed" is passed from Form Source to Form Destination

这样,“值传递”从表单源传递到表单目标

#10


There are multiple ways to pass data between 2 forms check these links which has example videos to do this

有两种方法可以在两个表单之间传递数据,请检查这些链接,这些链接包含示例视频

-FormToForm Using Properties - http://windowsclient.net/learn/video.aspx?v=108089

-FormToForm使用属性 - http://windowsclient.net/learn/video.aspx?v=108089