美文网首页
C#窗体之间传值

C#窗体之间传值

作者: 幻凌风 | 来源:发表于2017-08-13 17:22 被阅读185次

一、接口实现窗体之间传值

接口传值.png

1、定义接口IMessageOn

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloMessage
{
    public interface IMessageOn
    {
        void RecieveMsg(string strMsg);
    }
}

2、定义子窗体FormA、FormB、FormC

(1)窗体FormA定义的接口IMessageOn类型的集合MessageOnList,并在构造器中初始化集合,最后夺按钮单击事件中进行传值
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 HelloMessage
{
    public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();

            //初始化集合
            MessageOnList = new List<IMessageOn>();
        }

        //接口的集合
        public List<IMessageOn> MessageOnList { get; set; }

        private void ButtonInterface_Click(object sender, EventArgs e)
        {
            foreach (var messageOn in MessageOnList)
            {
                messageOn.RecieveMsg("我是窗体A");
            }
        }
    }
}

(2)窗体FormB实现接口

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 HelloMessage
{
    public partial class FormB : Form,IMessageOn
    {
        public FormB()
        {
            InitializeComponent();
        }

        public void RecieveMsg(string strMsg)
        {
            this.TextBoxB.Text = strMsg + DateTime.Now.ToString();
        }
    }
}

(3)窗体FormC实现接口

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 HelloMessage
{
    public partial class FormC : Form,IMessageOn
    {
        public FormC()
        {
            InitializeComponent();
        }

        public void RecieveMsg(string strMsg)
        {
            this.TextBoxC.Text = strMsg + DateTime.Now.ToString();
        }
    }
}

3、主窗体MainForm弹出所有子窗体FormA、FormB、FormC

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 HelloMessage
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void ButtonOpenForm_Click(object sender, EventArgs e)
        {
            FormA formA = new FormA();
            formA.Show();

            FormB formB = new FormB();
            //用接口的形式注册观察者
            formA.MessageOnList.Add(formB);
            formB.Show();

            FormC formC = new FormC();
            formA.MessageOnList.Add(formC);
            formC.Show();

        }
    }
}

要点:FormB和FormC都实现了接口的方法,并且都在主窗体(中介)里添加到了FormA的接口类型的集合IMessageOnList中。所以,当FormA在自己的按钮事件里遍历MessageOnList集合对象的时候,实际上用的是FormB和FormC的引用实例,给其传值后,FormB和FormC窗体的TextBox进行接收。

二、委托实现窗体之间传值

委托传值.png

1、窗体MainForm客户定义委托类型,在按钮方法中将FormB和FormC中的方法挂接给FormA中的委托类型的属性SendMesgDelInstance

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 HelloMessage
{
    //定义委托类型
    public delegate void SendMesgDel(string str);
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void ButtonOpenForm_Click(object sender, EventArgs e)
        {
            FormA formA = new FormA();
            formA.Show();

            FormB formB = new FormB();
            //把窗体B的方法放到窗体A的委托中去
            formA.SendMesgDelInstance += formB.RecieveMsgDel;
            formB.Show();

            FormC formC = new FormC();
            formA.SendMesgDelInstance += formC.RecieveMsgDel;
            formC.Show();

        }
    }
}

2、窗体FormA中定义委托类型的属性,并在按钮事件中调用该委托,将TextBox中的值进行传递

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 HelloMessage
{

    public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }

        #region 委托实现传值

        //定义委托类型的属性
        public SendMesgDel SendMesgDelInstance { get; set; }
        private void ButtonDelegate_Click(object sender, EventArgs e)
        {
            SendMesgDelInstance.Invoke(this.TextBoxA.Text);
        }
        #endregion
    }
}

3、FormB和FormC窗体中的方法响应FormA的委托

(1)FormB
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 HelloMessage
{
    public partial class FormB : Form
    {
        public FormB()
        {
            InitializeComponent();
        }

        //委托的方法
        public void RecieveMsgDel(string strMsg)
        {
            this.TextBoxB.Text = strMsg + DateTime.Now.ToString();
        }
    }
}
(2)FromC
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 HelloMessage
{
    public partial class FormC : Form
    {
        public FormC()
        {
            InitializeComponent();
        }

        //委托的方法
        public void RecieveMsgDel(string strMsg)
        {
            this.TextBoxC.Text = strMsg + DateTime.Now.ToString();
        }
    }
}

三、事件实现窗体传值

委托传值,因为委托类型是公开的(Public),并不安全,所以用事件进行传值更加合适。

事件传值.png

1、窗体FormA中定义事件SendMesgDelEvent,并在按钮事件中触发该事件,进行传值

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 HelloMessage
{

    public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();

        }


        #region 事件实现传值
        //定义事件
        public event SendMesgDel SendMesgDelEvent;
        private void ButtonEvent_Click(object sender, EventArgs e)
        {
            SendMesgDelEvent(this.TextBoxA.Text);
        }
        #endregion


    }
}

2、窗体FormB和FormC定义响应事件的方法

(1)FormB
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 HelloMessage
{
    public partial class FormB : Form
    {
        public FormB()
        {
            InitializeComponent();
        }

        //事件的方法
        public void RecieveMsgDelEvent(string strMsg)
        {
            this.TextBoxB.Text = strMsg + DateTime.Now.ToString();
        }
    }
}
(2)FormC
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 HelloMessage
{
    public partial class FormC : Form
    {
        public FormC()
        {
            InitializeComponent();
        }

        //事件的方法
        public void RecieveMsgDelEvent(string strMsg)
        {
            this.TextBoxC.Text = strMsg + DateTime.Now.ToString();
        }
    }
}

3、MainForm中将FormB和FormC的方法挂接到FormA的事件上

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 HelloMessage
{
    //定义委托类型
    public delegate void SendMesgDel(string str);
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void ButtonOpenForm_Click(object sender, EventArgs e)
        {
            FormA formA = new FormA();
            formA.Show();

            FormB formB = new FormB();
            formA.SendMesgDelEvent += formB.RecieveMsgDelEvent;
            formB.Show();

            FormC formC = new FormC();
            formA.SendMesgDelEvent += formC.RecieveMsgDelEvent;
            formC.Show();

        }
    }
}

相关文章

  • C#窗体之间传值

    一、接口实现窗体之间传值 1、定义接口IMessageOn 2、定义子窗体FormA、FormB、FormC (1...

  • C#窗体实时传值

    1、功能展示 有时需要将子界面的内容传递到父界面,方法有好几种。经常用的是通过委托实现。具体的效果如下: 【说明】...

  • C#窗体传值方法总结

    窗体传值是在学习窗体应用程序时碰到的一类比较常见的问题,现将窗体传值方法做了一点总结,方法如下: <1>声明全局变...

  • UI框架学习笔记(UGUI)

    多个场景中加载相同的UI窗体? 各个UI脚本之间传值,易出现“紧耦合”,导致项目“可复用性”降低? 多个“弹出窗体...

  • C#使用委托在窗体间传值

    任务:在窗体1点击按钮显示窗体2,在窗体2点击按钮将该窗体TextBox的值传递给窗体1的Label 直接看代码(...

  • 组件通信

    vue传值可分为父子之间传值、兄弟组件之间传值、跨代组件之间传值 1.父子之间传值:可以使用$emit/props...

  • python项目实战:pyQT5 实现窗体之间传值

    本节分享一个pyQT5 实现窗体之间传值,希望能够帮助到大家 准备 一个MainWindow和一个WidgetFo...

  • winform窗体间传值

    推荐使用第6与第7方法 1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体F...

  • vue通信、传值的多种方式

    组件之间传值方式 页面间之间传值方式

  • C#如何创建MDI子窗体

    C#中怎样创建MDI子窗体?在MDI主窗体的【属性】对话框中,将MDI主窗体的IsMdiContainer属性设为...

网友评论

      本文标题:C#窗体之间传值

      本文链接:https://www.haomeiwen.com/subject/wzlerxtx.html