C#之winform基础 ,form1显示form2中textbox中输入的文本,窗体间传值

       慈心积善融学习,技术愿为有情学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。



1、form1UI



2、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 WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //在form1装载时,创建一个form2对象并显示
            //form1与form2一起显示
            Form2 frm2 = new Form2(AppendWord);
            frm2.Show();
        }
        void AppendWord(string word)
        {
            textBox1.Text += word;
        }
    }
}



3、form2UI



扫描二维码关注公众号,回复: 1956071 查看本文章

4、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 WindowsFormsApplication5
{
    //因为默认是private与身为public的form2构造函数不一致,所以必须写public
    public delegate void Del(string word);

    public partial class Form2 : Form
    {
        private Del _del;
        //form2的构造函数要修改
        public Form2(Del del)
        {
            InitializeComponent();
            this._del = del;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

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



5、效果






感恩曾经帮助过 心少朴 的人。
C#优秀,值得学习。Winform,WPF 都可以关注一下,眼界要开阔。
Visual Studio IDE很好用,推荐!
注:此文是自学笔记所生,质量中等,故要三思而后行。新手到此,不可照搬,应先研究其理象数,待能变通之时,自然跳出深坑。

猜你喜欢

转载自blog.csdn.net/yushaopu/article/details/52734823