原理:两个窗体绑定同一个静态对象。这里只是为了说明问题,并没进行静态对象进行清理方面的工作。
类结构图:
![](https://www.cnblogs.com/images/cnblogs_com/icoolno1/4.gif)
程序运行效果如图:
![](https://www.cnblogs.com/images/cnblogs_com/icoolno1/3.gif)
具体实现:
MainForm.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 主窗体
/// </summary>
public class MainForm:Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Button button1;
private Label label1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private TextBox textBox1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public MainForm()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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(116, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(164, 21);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(173, 47);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
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(24, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 2;
this.label1.Text = "测试内容";
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(292, 82);
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();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void MainForm_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置绑定
textBox1.DataBindings.Add("Text", StaticClass.ModuleClass, "Test", false, DataSourceUpdateMode.OnPropertyChanged);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
new DetailForm().Show();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DetailForm.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class DetailForm:Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Label label1;
private TextBox textBox1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DetailForm()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(124, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(156, 21);
this.textBox1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(27, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 1;
this.label1.Text = "测试内容";
//
// DetailForm
//
this.ClientSize = new System.Drawing.Size(292, 72);
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();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void DetailForm_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置绑定
textBox1.DataBindings.Add("Text", StaticClass.ModuleClass, "Test", false, DataSourceUpdateMode.OnPropertyChanged);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ModuleClass.cs
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 数据实体类
/// </summary>
public class ModuleClass
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string test;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取或设置测试属性
/// </summary>
public string Test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return test; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ test = value; }
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
StaticClass.cs
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 提供全局访问变量
/// </summary>
public class StaticClass
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private static ModuleClass module = new ModuleClass();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取模型对象
/// </summary>
public static ModuleClass ModuleClass
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return module; }
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
static class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}