c# winform窗体间的传值

说明:本文讲解两个窗体之间的传值,主要用到两个窗体,form1,form2

1、在form1窗体单击按钮,打开窗体form2,然后把form2中文本框的值传递给form1

form1中的代码:

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 FormToform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //获取值
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fr = new Form2();
            DialogResult rsult = fr.ShowDialog();
            if(rsult==DialogResult.OK)
            {
                //获取窗体2传回来的值
                listBox1.Items.Add(fr.UKind);
                listBox1.Items.Add(fr.UName);
            }
        }
    }
}
窗体form2的代码:

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 FormToform
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //给窗体定义两个属性
     
        public string UKind {
            get { return textBox1.Text; }
            set { textBox1.Text= value; }
        }   
        public string UName
        {
            get { return textBox2.Text; }
            set { textBox2.Text = value; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
2、在form1窗体单击按钮,打开窗体form2,然后给form2中文本框赋值

form1中按钮的代码如下:

  private void button2_Click(object sender, EventArgs e)
        {
            Form2 fr = new Form2();
            fr.UName = "姓名";
            fr.UKind = "类别";
            DialogResult rsult = fr.ShowDialog();
           
        }

 

form2与标题1一样这里不做赘述

======================委托事件方式父子窗口传值================

 

Form1 父窗口

   private void button2_Click(object sender, EventArgs e)
        {
            User u = new User();
            u.UserName = "touna";
            u.Age = 18;
            u.Grade = 94.5m;


            Form3 sb = new Form3(u);
            sb.myRefresh3 += new Form3.Refresh3(RefreshControl3);//绑定事件
            sb.Show();
        }

//方法实现刷新父窗口标签
        private void RefreshControl3(User user)
        {
            //绑定树信息
            this.label2.Text =user.UserName;
        }

子窗口Form3

 

 

public delegate void Refresh3(User user);       //定义委托   

 public event Refresh3 myRefresh3; //定义事件

 User k = new User();
        public Form3(User u)
        {
            InitializeComponent();
           
            k = u;
          
        
        }

   
        private void button1_Click(object sender, EventArgs e)
        {
            close();
         
        }

        private void close()
        {
            User p = new User();
            p.UserName = this.textBox1.Text;
            //关闭的时候执行事件
            myRefresh3(p);//父窗口的刷新方法
            this.Close();      

        }

===============纯委托方式==

父窗口

   private void button2_Click(object sender, EventArgs e)
        {
            Form3 sb = new Form3(RefreshControl3);
  
            sb.Show();
        }

     private void RefreshControl3(User user)
        {
            //绑定树信息
            this.label2.Text =user.UserName;
        }
子窗口Form3

  public partial class Form3 : Form
    {
        public delegate void Refresh3(User user);
        private Refresh3 rrf;
        
        public Form3(Refresh3 rf)
        {
            InitializeComponent();
           
            this.rrf= rf;
      
        }

   
        private void button1_Click(object sender, EventArgs e)
        {
          close()
         
        }
        private void
        {
            User p = new User();
            p.UserName = this.textBox1.Text;
            //关闭的时候执行事件
    
            rrf(p);
         this.Close();

      

        }}

}

你可能感兴趣的:(C#)