zoukankan      html  css  js  c++  java
  • C#窗体互动

    说白了就是在一个窗体操作另外一个窗体的东西。

    原理是把form2的数据提取出来,利用中间的静态类middle来传递数据,触发事件,调用委托,来修正form1

    效果如下:

    1

    Form1.cs

    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 FormInteractive
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Middle.sendEvent += new Middle.SendMessage(this.DoMethod);
            }
    
            public void DoMethod(string getstr)
            {
                listBox1.Items.Add(getstr);
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 c = new Form2();
                c.Show();
            }
        }
    }

    Middle.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace FormInteractive
    {
        /// <summary>
        /// 这是一个静态类,用于定义委托和事件已经调用事件
        /// </summary>
        public static class Middle
        {
            public delegate void SendMessage(string str);
    
            public static event SendMessage sendEvent;
    
            public static void DoSendMessage(string str)
            {
                sendEvent(str);
            }
    
        }
    }

    Form2.cs

    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 FormInteractive
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //调用Middle类中的DoSendMessage
                Middle.DoSendMessage(textBox1.Text);
                textBox1.Text = ""; //将textBox1清空
                textBox1.Focus();   //将鼠标锁定在textBox1
            }
        }
    }
  • 相关阅读:
    0x55 环形与后效性问题
    0x54 树形DP
    0x53 区间DP
    0x52 背包
    0x51 线性DP
    poj1015 Jury Compromise
    973、863计划取消 国家重点研发计划启动
    中科院院士陈仙辉回母校:英雄不问出处 成功要靠努力和实力来实现
    Resume (Curriculum Vitae)
    阅读linux内核代码的工具-- Source Insight
  • 原文地址:https://www.cnblogs.com/Mysterious/p/3416583.html
Copyright © 2011-2022 走看看