CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2006
    Posts
    27

    passing data from Form2 to Form1

    Hi,
    I have 2 forms , Form1 and Form2. When I click on the "Ok" buttom of the Form 1, it creates an instance of Form2 as below

    Form2 newform = new Form2();
    newform.Show();


    Form2 has a textbox which accepts input from user. I am able to get the Form2 and its textbox, but I am having problems of passing data from Form2 to Form1. Sending data from Form1 to Form2 is not a problem. But here it is the reverse case. How do i pass the data in textbox of Form2 to Form1 ??

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: passing data from Form2 to Form1

    set the "Modifiers" property of the textbox to "Public", then...

    Code:
    newform.ShowDialog();
    string dataFromForm = newform.textBox1.Text;
    newform.Dispose();

  3. #3
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Thumbs up Re: passing data from Form2 to Form1

    Quote Originally Posted by sangfroid
    Hi,
    I have 2 forms , Form1 and Form2. When I click on the "Ok" buttom of the Form 1, it creates an instance of Form2 as below

    Form2 newform = new Form2();
    newform.Show();


    Form2 has a textbox which accepts input from user. I am able to get the Form2 and its textbox, but I am having problems of passing data from Form2 to Form1. Sending data from Form1 to Form2 is not a problem. But here it is the reverse case. How do i pass the data in textbox of Form2 to Form1 ??
    Use delegate and events to send info from form2 to form1
    Have a look into the attached file....

    Hope this is what you are looking for..
    MMH...
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: passing data from Form2 to Form1

    It depends on how you want Form2 to behave.

    If you want it to behave like a FileOpen dialog (i.e. stops all input into Form1 until it's closed) use eclipsed4utoo's suggestion but with one change :

    DONT MAKE MEMBER VARIABLES PUBLIC

    This breaks encapsulation. You should instead have a public property which returns the text in the text box e.g.

    Code:
    public class Form2 : Form
    {
        private TextBox _textBox;
    
        public string TextBoxText
        {
            get
            {
                return _textBox.Text;
            }
        }
    }
    However if you want Form2 to open and stay open whilst the user does things with Form1 then you should use MMH's suggestion of having an event.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Jan 2007
    Posts
    491

    Re: passing data from Form2 to Form1

    You can also send data through the constructor of Form2...
    Code:
    Form2 form2 = new Form2("hi", "sup?", "bye");
    using(form2)
    {
       form2.ShowDialog();
    }

  6. #6
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: passing data from Form2 to Form1

    Quote Originally Posted by Talikag
    You can also send data through the constructor of Form2...
    Code:
    Form2 form2 = new Form2("hi", "sup?", "bye");
    using(form2)
    {
    form2.ShowDialog();
    }
    He wants to send data from form2 to form1 and not vice-versa.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured