简体   繁体   English

从子窗体传回数据时出现问题

[英]Problem passing data back from a child form

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: 和StringLoader形式:

 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). 这件事给了我一个nullReferenceException(我知道)。

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. 谢谢。

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;

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());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM