[原创]多个窗体之间的参数传递与数据共享(附:UML图)

![[原创]多个窗体之间的参数传递与数据共享(附:UML图) [原创]多个窗体之间的参数传递与数据共享(附:UML图)](http://www.csframework.com/images/seo/SEO-Cshart%E6%BA%90%E7%A0%8111.jpg)
学习内容
/**************************************************************************
* 学习内容:
* 1. 如何打开一个对话框并传入参数。
* 2. 多个窗体之间的参数传递与数据共享
* 3. 如何创建一个单件模式的窗体。
* 4. 对象传递原理与实现
* 简单地说,定义变量就是给对象分配一个内存地址,在对象没有实例化前
* 该内存地址仅表示变量的位置。对象传递可以理解为传递对象指针(Point).
* 如需要更明确内存分配原理,请在网上搜查"Windows下动态内存分配方式"
* 推荐2本学C#的书:《.net本质论》:《C#设计模式》
***************************************************************************/
UML图示

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace www.csframework.com
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenDialog_Click(object sender, EventArgs e)
{
//调用对话框的ExecuteFind静态方法
//打开一个对话框并传入参数。
Form2.ExecuteFind(this, this.richTextBox1);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace www.csframework.com
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenDialog_Click(object sender, EventArgs e)
{
//调用对话框的ExecuteFind静态方法
//打开一个对话框并传入参数。
Form2.ExecuteFind(this, this.richTextBox1);
}
}
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace www.csframework.com
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//由外部传进来的对象.(*指针)
private RichTextBox _RichTextBox = null;
public RichTextBox RichTextBox { get { return _RichTextBox; } set { _RichTextBox = value; } }
//当前窗体实例
private static Form2 _Instance = null;
public static void ExecuteFind(Form owner, RichTextBox richTextBox)
{
//引用设计模式的单件模式。永远只创建一个窗体。
if (_Instance == null) _Instance = new Form2();
_Instance.RichTextBox = richTextBox; //保存由外部传入的变量
if (_Instance.Visible) //如果对话框实例已经打开,设置窗体焦点
_Instance.Activate();
else
_Instance.Show(owner); //第一次打开,显示窗体。owner参数为
//owner参数表示将拥有此窗体的顶级窗口的对象。
}
private void btnFind_Click(object sender, EventArgs e)
{
string s = textBox1.Text;
//调用由外部传进来的对象的方法。
int pos = _RichTextBox.Text.IndexOf(s);
if (pos > 0)
{
RichTextBoxTool.FindFirst(this._RichTextBox, s);
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
_Instance = null; //当关闭窗体的时候,记得将变量设为null
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace www.csframework.com
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//由外部传进来的对象.(*指针)
private RichTextBox _RichTextBox = null;
public RichTextBox RichTextBox { get { return _RichTextBox; } set { _RichTextBox = value; } }
//当前窗体实例
private static Form2 _Instance = null;
public static void ExecuteFind(Form owner, RichTextBox richTextBox)
{
//引用设计模式的单件模式。永远只创建一个窗体。
if (_Instance == null) _Instance = new Form2();
_Instance.RichTextBox = richTextBox; //保存由外部传入的变量
if (_Instance.Visible) //如果对话框实例已经打开,设置窗体焦点
_Instance.Activate();
else
_Instance.Show(owner); //第一次打开,显示窗体。owner参数为
//owner参数表示将拥有此窗体的顶级窗口的对象。
}
private void btnFind_Click(object sender, EventArgs e)
{
string s = textBox1.Text;
//调用由外部传进来的对象的方法。
int pos = _RichTextBox.Text.IndexOf(s);
if (pos > 0)
{
RichTextBoxTool.FindFirst(this._RichTextBox, s);
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
_Instance = null; //当关闭窗体的时候,记得将变量设为null
}
}
}

扫一扫加作者微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网