原理:两个窗体绑定同一个静态对象。这里只是为了说明问题,并没进行静态对象进行清理方面的工作。
类结构图:
程序运行效果如图:
具体实现:
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(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();
}
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();
}
}
}
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(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();
}
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(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();
}
private void DetailForm_Load(object sender, EventArgs e)
{
//设置绑定
textBox1.DataBindings.Add("Text", StaticClass.ModuleClass, "Test", false, DataSourceUpdateMode.OnPropertyChanged);
}
}
}
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(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();
}
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; }
}
}
}
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; }
}
}
}
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());
}
}
}
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());
}
}
}