zoukankan      html  css  js  c++  java
  • 委托窗体之间的传值

    利用委托事件来传递数据
    
    Form1中:一个lable1用来接受Form2中textbox1的信息,button1用来show出Form2
    
    Form1中代码:
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WinTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //单击该按钮时SHOW出第二个窗体
                Form2 fm2 = new Form2();
                fm2.myevent += new Form2.mydelegate(givevalue);//在SHOW出窗体的同时订阅FORM2的事件,调用givevalue()方法.
                fm2.ShowDialog();
            }
            public void givevalue(string text) //用于修改label的方法
            {
                this.label1.Text = text;
            }
        }
    }
    
    
    Form2中:一个textbox1用来输入要传递的值,button1用来触发传递事件
    
    Form2代码:
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WinTest
    {
        public partial class Form2 : Form
        {
            public delegate void mydelegate(string text);//定义一个委托
            public event mydelegate myevent;//定义上诉委托类型的事件
    
            public Form2()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                //在单击该窗体上的按钮时触发事件
                if (myevent != null)
                {
                    myevent(textBox1.Text);
                }
            }
        }
    }
  • 相关阅读:
    C语言I博客作业02
    C语言I博客作业06
    C语言I—2019秋作业第一周作业
    C语言I博客作业09
    C语言I博客作业07
    C语言I博客作业08
    利用 jrebel 热部署\远程调试\远程热部署 springboot项目 服务器上的代码
    java 双因素认证(2FA)TOTP demo
    java File读取文件始终不存在的问题分析
    exchange 2010 的两个错误
  • 原文地址:https://www.cnblogs.com/lierjie/p/3690066.html
Copyright © 2011-2022 走看看