zoukankan      html  css  js  c++  java
  • C# WinFrom 窗体传值

       public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            public string TextBox2Value //将外部不可访问的textBox2.Text封装成属性TextBox1Value
            {
                set { textBox2.Text = value; }
                get { return textBox2.Text; }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.DialogResult = DialogResult.OK;//这里的DialogResult是Form2类对象的属性
            }
    
        }

    委托传值

    using System;
    using System.Windows.Forms;
    
    namespace WindowsForms跨窗体传值大全
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.ChangeText += new ChangeTextHandler(Change_Text);//将事件和处理方法绑在一起,这句话必须放在f2.ShowDialog();前面
                f2.ShowDialog();            
            }
    
           public void Change_Text(string str)
            {
                textBox1.Text = str;
            }  
        }
    }   
    
    
    
    using System;
    using System.Windows.Forms;
    
    namespace WindowsForms跨窗体传值大全
    {
        public delegate void ChangeTextHandler(string str);  //定义委托
    
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            public event ChangeTextHandler ChangeText;  //定义事件
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (ChangeText != null)//判断事件是否为空
                {
                    ChangeText(textBox2.Text);//执行委托实例  
                    this.Close();
                }           
            }    
        }
    }

    Action<>泛型委托和lambda表达式

    //Form1中:
    using System;
    using System.Windows.Forms;
    
    namespace WindowsForms跨窗体传值大全
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.ChangeText = (str) => textBox1.Text = str;//用lambda表达式实现,这句话必须放在f2.ShowDialog();前面
                f2.ShowDialog();            
            }
        }
    }
    
    
    //Form2中:
    using System;
    using System.Windows.Forms;
    
    namespace WindowsForms跨窗体传值大全
    {  
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            public Action<string> ChangeText;//之前的定义委托和定义事件由这一句话代替
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (ChangeText != null)//判断事件是否为空
                {
                    ChangeText(textBox2.Text);//执行委托实例  
                    this.Close();
                }           
            }    
        }
    }
  • 相关阅读:
    Codeforces Round #544 (Div. 3) F2. Spanning Tree with One Fixed Degree
    2020ICPC·小米 网络选拔赛第二场 I Subsequence Pair
    BJTU 1867. try a try, ac is OK
    Codeforces Round #667 (Div. 3) E. Two Platforms
    Educational Codeforces Round 94 (Rated for Div. 2) D. Zigzags
    Educational Codeforces Round 94 (Rated for Div. 2) B. RPG Protagonist
    Codeforces Round #665 (Div. 2) E. Divide Square
    Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree
    [洛谷] P1801 黑匣子
    面向对象中return和break的区别
  • 原文地址:https://www.cnblogs.com/lanyubaicl/p/11021564.html
Copyright © 2011-2022 走看看