i have a form which once a button is clicked opens a new form via showdialog(). i want to send data from this new form back to the original form but am new to programming and have become very confused with the parent child relationships of forms. How would i go about passing the data correctly

any help would be appreciated
thanks

If you are using ShowDialog to display the child form modally then i would recommend exposing the data via a property:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            if(frm2.ShowDialog() == DialogResult.OK)
            {
                string result = frm2.Result;
            }
        }
    }

    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();

        }

        public string Result {get; set;}

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            Result = TextBox1.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }

i can understand what your code is referring to but i cannot work out how to relate the textbox from the original form where the data is being passed to into the code, could you explain how i would give the textboxResult the value of the variable from the child form

thanks

You could make a string like this. It will be usable on any form.

static class Strings { static public string file; }

Then to use it simply Strings.file = ...;

@Game, what you have suggested is the C# equivalent of a global variable. They are very poor OO design, especially as a solution to pass data between forms as it breaks encapsulation.

@rjthomas8, let me talk you through my code to see if ti clears things up:
Form1 creates an instance of Form2 and shows it modally using ShowDialog. At this point, execution on Form1 pauses whilst Form2 is displayed.
Form2 has a TextBox (TextBox1), a Button (ButtonOK) and a Property (Result. When the button is clicked the text of the textbox is copied to the Property, Form2's DialogResult is set to DialogResult.OK and Form2 closes.
With Form2 closed, Form1 resumes execution. It checks the DialogResult returned by Form2 and if it is DialogResult.OK then it processes the result.
In my code, it accesses the Property of Form2 to retrieve the value stored in Result. I just copied it to a string variable. You can do whatever you like with the string that you retrieve.

With that outlined, have a go at putting the result string into your textbox on Form1 and let me know how you get on :)

Edited 6 Years Ago by Geekitygeek: n/a

Comments
very clear and correct with the solution

@ryshad: i used your way and it worked wonders

thank you very much :)

Edited 6 Years Ago by rjthomas8: n/a

If you have used the code as i gave it to you then the 'result' variable is only accessible within the 'if' loop that it is created in. Are you familiar with the scope of varaibles? If you want to use the value of result elsewhere in your code then you need to store it somewhere that will maintain its scope, either at the method level if you are using in the same method, or at the class level if you need to use it in a different method.
If you can post the relevent code for Form1 i can tell you if you are using it in the correct place.

This article has been dead for over six months. Start a new discussion instead.