6

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?

GWLlosa
  • 23,995
  • 17
  • 79
  • 116
Arunachalam
  • 5,417
  • 20
  • 52
  • 80
  • +1 - I don't see why this was downvoted... It may be poorly worded, but it's not a bad question. Arunachalam (or someone with appropriate permissions) should reword this question to be more understandable. – Mark Carpenter May 04 '09 at 16:54

10 Answers10

3

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
  }
}
LPL
  • 16,827
  • 6
  • 51
  • 95
woodseynz
  • 31
  • 1
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

Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
2

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

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

HTH

Anand Shah
  • 14,575
  • 16
  • 72
  • 110
1

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]

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
0

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

Tushar Vengurlekar
  • 7,649
  • 8
  • 33
  • 48
0

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.

Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
0

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)

0

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.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
0

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.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
MoSlo
  • 2,780
  • 4
  • 33
  • 37
-1

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

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Suneet
  • 165
  • 4