tags:

views:

69

answers:

2

Hi everyone. I got this problem:

private void loadStringToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StringLoader frmStringLoader = new StringLoader();
            string test = frmStringLoader.Result;
            frmStringLoader.ShowDialog();
            MessageBox.Show(test.ToString());
        }

And the StringLoader Form:

 public partial class StringLoader : Form
    {

        private string result;
        public StringLoader()
        {
            InitializeComponent();
        }

        public string Result
        {
            get { return result; }
        }

        private void btnLoadString_Click(object sender, EventArgs e)
        {
            if ((txtString.Text != string.Empty))
            {
                result = txtString.Text;
            }
            this.Close();
        }
    }
}

This thing is gaving me a nullReferenceException (I know).

How to handle this thing? I just want to open a form, write a text and click a button to send the data back to the caller and close the form.

Thanks.

+2  A: 

You're setting your result before the dialog opens. Try reversing the two lines of code to look like this:

        frmStringLoader.ShowDialog();
        string test = frmStringLoader.Result;
msergeant
+1  A: 

You're grabbing the result before showing the form! Try

private void loadStringToolStripMenuItem_Click(object sender, EventArgs e)
    {
        StringLoader frmStringLoader = new StringLoader();
        frmStringLoader.ShowDialog();
        string test = frmStringLoader.Result;
        MessageBox.Show(test.ToString());
    }
Dan Blanchard
I just can't believe it. I used to think that if I hit the OK button in the child form the data wouldn't pass. But this things is super very easy. Why I didn't tried it before?. Thank you (both of you) for your help.
This make me think on something: Why the string did her job? When I hit the Ok button, It closes the form, isn't it? So why the data is passed?
I don't know if I understand your question correctly. Are you asking why you can access frmStringLoader.Result before the dialog is shown?
msergeant