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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/818930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:25:54  来源:igfitidea点击:

How can I pass values from one form to another?

c#winforms

提问by Arunachalam

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

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

回答by Mark Carpenter

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()。但是,以另一种方式传递数据有点棘手。

回答by Anand Shah

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

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

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

HTH

HTH

回答by Anand Shah

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

让我重新定义问题,我有 2 个表单 f1, f2... i cal Form 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)

回答by Anand Shah

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

这是以 who will pass the value 的形式写的。要接收此值,您必须以接收的形式创建新的构造函数

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 创建实例时,您会在第一个表单中注意到您有两个选择,其中一个让您传递值

回答by Erich Kitzmueller

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 模式,即不要在表单中有太多数据并将它们传递出去,而是使用保持数据的模型类。

回答by Fredrik M?rk

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:

在 Form1 中:

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

In Form2:

在 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. Transfer data to that new form instance
  3. Show the form
  1. 创建要显示的表单实例
  2. 将数据传输到该新表单实例
  3. 显示表格

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 中的属性方法,使其更加清晰]

回答by MoSlo

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

为您的信息使用定义的类型(类、结构...)并在 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 Form1that'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 的两个实例都将引用同一位置的数据。对表单的两个实例都可以进行更改/更新。

回答by Suneet

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

有多种方法可以在 2 个表单之间传递数据,请检查这些链接,其中包含可以执行此操作的示例视频

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

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

回答by woodseynz

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

有没有人考虑过简单地将值传递给标签属性中的表单。

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
  }
}

回答by Tushar Vengurlekar

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

这种方式“传递的值”从表单源传递到表单目标