• 使用数据绑定实现多窗口间的数据同步


    原理:两个窗体绑定同一个静态对象。这里只是为了说明问题,并没进行静态对象进行清理方面的工作。

    类结构图


     程序运行效果如图



    具体实现
    MainForm.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        
    /// <summary>
        
    /// 主窗体
        
    /// </summary>

        public class MainForm:Form
        
    {
            
    private Button button1;
            
    private Label label1;

            
    private TextBox textBox1;

            
    public MainForm()
            
    {
                InitializeComponent();
            }


            
    private void InitializeComponent()
            
    {
                
    this.textBox1 = new System.Windows.Forms.TextBox();
                
    this.button1 = new System.Windows.Forms.Button();
                
    this.label1 = new System.Windows.Forms.Label();
                
    this.SuspendLayout();
                
    // 
                
    // textBox1
                
    // 
                this.textBox1.Location = new System.Drawing.Point(11612);
                
    this.textBox1.Name = "textBox1";
                
    this.textBox1.Size = new System.Drawing.Size(16421);
                
    this.textBox1.TabIndex = 0;
                
    // 
                
    // button1
                
    // 
                this.button1.Location = new System.Drawing.Point(17347);
                
    this.button1.Name = "button1";
                
    this.button1.Size = new System.Drawing.Size(7523);
                
    this.button1.TabIndex = 1;
                
    this.button1.Text = "显示主详细窗体";
                
    this.button1.UseVisualStyleBackColor = true;
                
    this.button1.Click += new System.EventHandler(this.button1_Click);
                
    // 
                
    // label1
                
    // 
                this.label1.AutoSize = true;
                
    this.label1.Location = new System.Drawing.Point(2415);
                
    this.label1.Name = "label1";
                
    this.label1.Size = new System.Drawing.Size(5312);
                
    this.label1.TabIndex = 2;
                
    this.label1.Text = "测试内容";
                
    // 
                
    // MainForm
                
    // 
                this.ClientSize = new System.Drawing.Size(29282);
                
    this.Controls.Add(this.label1);
                
    this.Controls.Add(this.button1);
                
    this.Controls.Add(this.textBox1);
                
    this.Name = "MainForm";
                
    this.Text = "主窗体";
                
    this.Load += new System.EventHandler(this.MainForm_Load);
                
    this.ResumeLayout(false);
                
    this.PerformLayout();

            }


            
    private void MainForm_Load(object sender, EventArgs e)
            
    {
                
    //设置绑定
                textBox1.DataBindings.Add("Text", StaticClass.ModuleClass, "Test"false, DataSourceUpdateMode.OnPropertyChanged);
            }


            
    private void button1_Click(object sender, EventArgs e)
            
    {
                
    new DetailForm().Show();
            }

        }

    }



     DetailForm.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        
    public class DetailForm:Form
        
    {
            
    private Label label1;
            
    private TextBox textBox1;

            
    public DetailForm()
            
    {
                InitializeComponent();
            }

        
            
    private void InitializeComponent()
            
    {
                
    this.textBox1 = new System.Windows.Forms.TextBox();
                
    this.label1 = new System.Windows.Forms.Label();
                
    this.SuspendLayout();
                
    // 
                
    // textBox1
                
    // 
                this.textBox1.Location = new System.Drawing.Point(12412);
                
    this.textBox1.Name = "textBox1";
                
    this.textBox1.Size = new System.Drawing.Size(15621);
                
    this.textBox1.TabIndex = 0;
                
    // 
                
    // label1
                
    // 
                this.label1.AutoSize = true;
                
    this.label1.Location = new System.Drawing.Point(2715);
                
    this.label1.Name = "label1";
                
    this.label1.Size = new System.Drawing.Size(5312);
                
    this.label1.TabIndex = 1;
                
    this.label1.Text = "测试内容";
                
    // 
                
    // DetailForm
                
    // 
                this.ClientSize = new System.Drawing.Size(29272);
                
    this.Controls.Add(this.label1);
                
    this.Controls.Add(this.textBox1);
                
    this.Name = "DetailForm";
                
    this.Text = "详细窗口";
                
    this.Load += new System.EventHandler(this.DetailForm_Load);
                
    this.ResumeLayout(false);
                
    this.PerformLayout();

            }


            
    private void DetailForm_Load(object sender, EventArgs e)
            
    {
                
    //设置绑定
                textBox1.DataBindings.Add("Text", StaticClass.ModuleClass, "Test"false, DataSourceUpdateMode.OnPropertyChanged);
            }

        }

    }

     ModuleClass.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace WindowsApplication1
    {
        
    /// <summary>
        
    /// 数据实体类
        
    /// </summary>

        public class ModuleClass
        
    {
            
    private string test;

            
    /// <summary>
            
    /// 获取或设置测试属性
            
    /// </summary>

            public string Test
            
    {
                
    get return test; }
                
    set { test = value; }
            }

        }

    }

    StaticClass.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace WindowsApplication1
    {
        
    /// <summary>
        
    /// 提供全局访问变量
        
    /// </summary>

        public class StaticClass
        
    {
            
    private static ModuleClass module = new ModuleClass();

            
    /// <summary>
            
    /// 获取模型对象
            
    /// </summary>

            public static ModuleClass ModuleClass
            
    {
                
    get return module; }
            }

        }

    }


    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        
    static class Program
        
    {
            
    /// <summary>
            
    /// 应用程序的主入口点。
            
    /// </summary>

            [STAThread]
            
    static void Main()
            
    {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
    false);
                Application.Run(
    new MainForm());
            }

        }

    }
  • 相关阅读:
    There was an internal API error
    MD5加密
    CentOS 7 最小化安装简单配置
    Dalvik 虚拟机操作码
    BugkuCTF~Web~WriteUp
    BugkuCTF~代码审计~WriteUp
    Android 个人手机通讯录开发
    Android SQLite 数据库学习
    Android 程序结构
    2018~第三届南宁市网络安全技术大赛~nnctf~write-up
  • 原文地址:https://www.cnblogs.com/icoolno1/p/537540.html
Copyright © 2020-2023  润新知