c# 不同窗体之间传值和调用

1.子窗体事件刷新父窗体界面值

   子窗体定义委托和事件  

 //声明一个委托
        public delegate void DisplayUpdateDelegate(string str);
        //声明事件
        public event DisplayUpdateDelegate ShowUpdate;
        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ShowUpdate != null)
            {
                ShowUpdate(textBox1.Text.ToString());
            }
        }


父窗体 

   private void button3_Click(object sender, EventArgs e)
        {
            Form2 _frm2 = new Form2();
            _frm2.ShowUpdate += new Form2.DisplayUpdateDelegate(ShowMessage);
            _frm2.Show();
        }

        private void ShowMessage(string str)
        {
            textBox1.Text = str;
        }

2.父窗体的事件刷新子窗体的值

   父窗体代码

  

  public delegate void ChangeSonWindowDelegate(string str);
        ChangeSonWindowDelegate _changeSonWindow;
        private void button1_Click(object sender, EventArgs e)
        {

            Form2 _frm2 = new Form2();
             _changeSonWindow = new ChangeSonWindowDelegate(_frm2.ShowMessage);
            _frm2.Show();

        }
     
        private void button2_Click(object sender, EventArgs e)
        {

            _changeSonWindow(textBox1.Text);

        }

子窗体代码 

        public void ShowMessage(string str)
        {
            textBox1.Text = str;

        }

  3.父窗体调用子窗体的方法。子窗体一定得是显示状态。

    父窗体

   

        Form2 _frm;
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = 5000;
            timer1.Enabled = true;
            _frm = new Form2();            
            _frm.ShowDialog();        

        }    

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            _frm.WriteLog("调用子窗体方法2222");
        }

子窗体

        public void WriteLog(string str)
        {
            Console.WriteLine(str);
        }

4.子窗体值->父窗体 

       父窗体    

  private void button1_Click(object sender, EventArgs e)
        {
            Form2 myFrm = new Form2();           
            if(myFrm.ShowDialog()==DialogResult.OK)
            {
                string boyName = myFrm.BoyName;
                int age = myFrm.Age;
            }
        }

       子窗体       

 private string _boyName;
        private int _age;

        public string BoyName { get => _boyName; set => _boyName = value; }
        public int Age { get => _age; set => _age = value; }

        private void button1_Click(object sender, EventArgs e)
        {
            BoyName = "jim";
            Age = 18;
            this.DialogResult = DialogResult.OK;
        }

5.跳出子窗体,在子窗体上实时显示信息

  父窗体

   ShowRunning(true);
   ShowRunningMsg("正在扫描中,请耐心等待......");

关闭子窗体 ShowRunning(false);

private delegate void ShowRunningDelegate(bool isShow);

        private void ShowRunning(bool isShow)
        {
            ShowRunningDelegate _showRunning = new ShowRunningDelegate(ShowRunningMethod);
            this.Invoke(_showRunning, new object[] { isShow});

        }

        FrmRunning _FrmRunning;
        public delegate void ShowRunningMsgDelegate(string msg);
        ShowRunningMsgDelegate ShowRunningMsg;
        private void ShowRunningMethod(bool isShow)
        {
            if (isShow)
            {          
               
                _FrmRunning = new FrmRunning();                
                ShowRunningMsg = new ShowRunningMsgDelegate(_FrmRunning.ShowRuningMsg);

                _FrmRunning.Show();
            }
            else
            {
                if (_FrmRunning != null)
                {
                    _FrmRunning.Close();
                }
            }

        }

子窗体
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BlueToothATE
{
    public partial class FrmRunning : Form
    {
        public FrmRunning()
        {
            InitializeComponent();
            
        }

        private void FrmRunning_Load(object sender, EventArgs e)
        {

        }

       public void ShowRuningMsg(string msg)
       {
            lblMsg.Text = msg;
        }



    }
}
原文地址:https://www.cnblogs.com/ike_li/p/5887804.html