简体   繁体   中英

How can I pass values from one form to another?

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 ?

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

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

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

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

HTH

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

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.

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:

  1. Create an instance of the form to show
  2. Transfer data to that new form instance
  3. Show the form

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]

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

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.

let me reframe the question i ve 2 form 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

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

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

Use a defined Type for your information (class, struct...) and declare a variable of that in 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.

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 frm2 = new Form2(this);
frm2.ShowDialog();

Now, when execution has reached Form2, you can access the MyData within Form1:
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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM