c # simple form by value

C # pass value for the form, the method may be used to delegate. Two new forms form1 and form2,

There is a label1 form1 and Button1;

There is a textbox1 form2 and Button1;

Click form2 realize the value of button1 form2 in textbox1, and pass form1 in label1;

 

Specific code as follows:

In the form1:

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(showMsg);
            form2.Show();
        }

        private void showMsg(string str)
        {
            label1.Text = str;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
View Code

In the form2

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

namespace WindowsFormsApp1
{
    public delegate void DelText(string str);
    public partial class Form2 : Form
    {
        public DelText _del;
        public Form2(DelText del)
        {
            this._del = del;
            InitializeComponent();
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            this._del(textBox1.Text);
        }
    }
}
View Code

 

 

Guess you like

Origin www.cnblogs.com/xifengmo/p/11032572.html