locked
Passing values from one windows form to another windows form using c#.net

    Question

  • Currently, I am doing a project. I don't know how to pass a value from one windows form to another
    windows form using c#.net. Suppose, I have two form: parent form and child form. Parent form consists of a
    combo box & button and I want to pass selected item of combo box to my child form and that item will be
    showing in a textbox of child form.

    I want immediate help regarding this problem.

    Note: I am using MS visual 2005

    Thank you.
    Thursday, May 22, 2008 9:47 AM

Answers

  •  

    pretty simple. pass in the values you want to pass via the constructor for your child form then set the values in the global variables in that form so your form can access them. Here is an example:

     

    //this is the main form. we are doing this code:

     

    Form2 child = new Form2("Bob", 22); //2 parameters being passed via constructor

    child.Show();

     

    //this is Form2. Modify the constructor:

     

    public void Form2(string name, int age)

    {

       InitializeComponent(); //this is default

       //set the values coming in to the global variables so we can access it in this form throughout the life of this form  

       this._name = name;

       this._age = age;

    }

     

    //declare these globally in form2 (after the form class decleration)

     

    private string _name = String.Empty;

    private int _age;

     

    and thats it. Now say from Button1 on Form2, we want to read those values. Do this:

     

    MessageBox.Show(this._name + " " + this._age.ToString()); //should show "Bob 22" since we passed "Bob" and 22 from the parent form

    Thursday, May 22, 2008 9:58 AM

All replies

  •  

    pretty simple. pass in the values you want to pass via the constructor for your child form then set the values in the global variables in that form so your form can access them. Here is an example:

     

    //this is the main form. we are doing this code:

     

    Form2 child = new Form2("Bob", 22); //2 parameters being passed via constructor

    child.Show();

     

    //this is Form2. Modify the constructor:

     

    public void Form2(string name, int age)

    {

       InitializeComponent(); //this is default

       //set the values coming in to the global variables so we can access it in this form throughout the life of this form  

       this._name = name;

       this._age = age;

    }

     

    //declare these globally in form2 (after the form class decleration)

     

    private string _name = String.Empty;

    private int _age;

     

    and thats it. Now say from Button1 on Form2, we want to read those values. Do this:

     

    MessageBox.Show(this._name + " " + this._age.ToString()); //should show "Bob 22" since we passed "Bob" and 22 from the parent form

    Thursday, May 22, 2008 9:58 AM
  • Hi,

    I have similar question. I am working on project that has 3 child forms and MDI PARENT. In my form1 i have file upload button that captures filepath.In my 2nd form i have few user selected values(strings). My third form has a button which when clicked should actualy take the filepath from first form and values from 2nd form and do modifications to the file. how should i proceed. Your replies will be appreciated.
    Tuesday, August 04, 2009 10:10 AM
  • The best way to proceed would be to start a new thread/question of your own.


    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, August 04, 2009 1:27 PM
  • ahmedilyas
    Thanks a ton....
    Saturday, December 26, 2009 10:16 AM
  • this looks like it would help me, i got the same prob. Gonna try this out.
    Tuesday, September 21, 2010 2:23 AM
  • worked for me. Just what I needed, thanks

    David F

    • Proposed as answer by nmaheshgoud Monday, January 30, 2012 8:08 AM
    Friday, January 27, 2012 12:31 PM
  • Thanks. It helps a lot.
    Thursday, March 01, 2012 10:25 AM
  • This is a Visual Basic forum, NOT a C# forum!

    Please direct your questions to the proper forum.


    Solitaire

    Friday, March 02, 2012 5:58 PM
  • hi ahmedilyas

    if these 2 forms are MDI Childs ,  normal Calling of any MDI CHILD Form uses the Default Constructor , i tried to add Parametrized Constructor, on execution , i tried to call one MDI Child to another , its  calling twice.

    my Code problem:

    FORM -A  and FORM -B    (2 MDI Childs)

    In Form - A :  if Call Form B as :     FormB  objB=new FormB(this);   //     this calling on RadioButton Checked

                                                          objB.ShowDialog();                    //

    it calling FormB , then im returning a String Value to FormA. it returning string Value to FormA.

    it works fine ,  the values passing are getting   but the FormB executes Twice how?. this is Problem.

    Thanks in Advance.

    Wednesday, October 10, 2012 7:43 AM
  • Hi check the below link. this will help you.

    http://dotnetpools.com/Article/ArticleDetiail/?articleId=47
    Saturday, October 20, 2012 3:14 PM