zoukankan      html  css  js  c++  java
  • WinForm 中两个窗口之间传递数据

    方法有很多种,这里介绍项目中使用的两种

    一、通过委托+事件的方法进行传值 (点击Form2中的button1按钮,将会把Form2中的textbox.text 传给Form1中的 lable.text)

    先上效果图

    image

    以下是代码

    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            public delegate void SendDataInvoke(string value);
            public event SendDataInvoke SendData;
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                SendData(textBox1.Text);   
            }
        }
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public delegate void MyInvoke();
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.SendData += new Form2.SendDataInvoke(frm_SendData);
                frm.Show();
            }
    
            private void frm_SendData(string value)
            {
                label1.Text = value;
            }
        }

    二、项目上前任开发所写的方法,通过在Form1定义一个方法,在FORM2里直接调用

    效果一样

    image

    代码

    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form1 frm = (Form1)this.Owner;
                    
                frm.ShowData(textBox1.Text);
    
            }
    }
    
    
    
     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
          public void ShowData(string value)
            {
                label1.Text = value;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                this.AddOwnedForm(frm);
                frm.Show();
            }
           
    }
  • 相关阅读:
    627. Swap Salary
    176. Second Highest Salary
    596. Classes More Than 5 Students
    183. Customers Who Never Order
    181. Employees Earning More Than Their Managers
    182. Duplicate Emails
    175. Combine Two Tables
    620. Not Boring Movies
    595. Big Countries
    HDU 6034 Balala Power! (贪心+坑题)
  • 原文地址:https://www.cnblogs.com/LiuLaoCai/p/3426591.html
Copyright © 2011-2022 走看看