C#窗口传值(CSDN实例)

//非模式窗体  相较独立
From qform=new Form();       
qform.Show();

//模式窗体  子依赖父
Form qform=new Form();
qform.ShowDialog();

 

父窗体

public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private MyDialog m_dlg;
        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyDialog dlg = new MyDialog(richTextBox1.Text);
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Text = dlg.TextBoxValue;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (m_dlg == null)
            {
                m_dlg = new MyDialog(richTextBox1.Text);
                m_dlg.TextBoxChanged += new EventHandler(m_dlg_textboxchanged);
                m_dlg.FormClosed += new FormClosedEventHandler(
                    (sender2, e2) => { m_dlg = null; }
                );
                m_dlg.Show(this);
            }
            else
            {
                m_dlg.Activate();
            }
        }

        private void m_dlg_textboxchanged(object sender1, EventArgs e1)
        {
            richTextBox1.Text = m_dlg.TextBoxValue;
        }
    }

 

子窗体

public partial class MyDialog : Form
    {
       
        public event EventHandler TextBoxChanged;

        public string TextBoxValue
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        public MyDialog() : this("") { }

        public MyDialog(string Param)
        {
            InitializeComponent();
            TextBoxValue = Param;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            if (TextBoxChanged != null)
            {
                TextBoxChanged(this, e);
            }
                       
        }

        private void MyDialog_Load(object sender, EventArgs e)
        {

        }
    }

你可能感兴趣的:(csdn)