关于不同窗体之间的控件查询 - 方式一
==============================================================
新建一个类 取名为 : UcFormCollector.cs
==============================================================
using System;
using System.Collections;
using System.Windows.Forms;
namespace QueryForm1
{
//查询窗体的方式
public enum UcQueryType{ Form , Control }
/// <summary>
/// 窗体收集者(采用数组收集方式)
/// </summary>
public sealed class UcFormCollector
{
private static ArrayList m_FormList = new ArrayList();
//窗体数组集合
public static ArrayList FormList
{
get
{
return m_FormList;
}
}
/// <summary>
/// 添加窗体
/// </summary>
/// <param name="p_Form">当前窗体</param>
public static void AddForm( Form p_Form )
{
if( m_FormList.Contains( p_Form.Handle ) ){ return ; }
m_FormList.Add( p_Form.Handle );
AttachEvent( p_Form );
}
/// <summary>
/// 移除窗体
/// </summary>
/// <param name="p_Form">当前窗体</param>
public static void RemoveForm( Form p_Form )
{
m_FormList.Remove( p_Form.Handle );
}
/// <summary>
/// 查询窗体
/// </summary>
/// <param name="p_FormName">窗体的名称</param>
/// <returns>返回查找到的窗体,否则返回null</returns>
public static Form QueryForm( string p_FormName )
{
IEnumerator myEnumerator = m_FormList.GetEnumerator();
Form currForm = null;
Form findForm = null;
while( myEnumerator.MoveNext() )
{
currForm = ( Form ) Form.FromHandle( ( System.IntPtr ) myEnumerator.Current ) ;
if( currForm.Name.ToLower() == p_FormName.ToLower() )
{
findForm = currForm;
}
}
return findForm;
}
/// <summary>
/// 查询窗体上的控件
/// </summary>
/// <param name="p_FormName">窗体的名称</param>
/// <param name="p_ControlName">窗体上控件的名称</param>
/// <returns>返回查找到的控件,否则返回null</returns>
public static Control QueryControl( string p_FormName , string p_ControlName )
{
Form currForm = QueryForm( p_FormName );
Control findControl = null;
if( currForm != null )
{
foreach( Control ctr in currForm.Controls )
{
if( ctr.Name.ToLower() == p_ControlName.ToLower() )
{
findControl = ctr;
}
}
}
return findControl;
}
/// <summary>
/// 在添加的窗体事件委托事件
/// </summary>
/// <param name="p_CurrForm"></param>
private static void AttachEvent( Form p_CurrForm )
{
p_CurrForm.Closed += new EventHandler( OnFormClosedRemoveItem );
}
//当窗体关闭后释放,窗体集合里的窗体
private static void OnFormClosedRemoveItem( object sender, System.EventArgs e )
{
RemoveForm( ( Form ) sender );
}
}
}
==============================================================
新建窗体 取名为 : Form1.cs
==============================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace QueryForm1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//查询方式
UcQueryType QueryType = new UcQueryType();
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
UcFormCollector.AddForm( this );
Thread t1 = new Thread( new ThreadStart( ShowForm2 ) );
Thread t2 = new Thread( new ThreadStart( ShowForm3 ) );
t1.Start();
t2.Start();
}
private void ShowForm2()
{
Application.Run( new Form2() );
}
private void ShowForm3()
{
Application.Run( new Form3() );
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 40);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "FormName";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 88);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "ControlName";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "查询";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// radioButton1
//
this.radioButton1.Checked = true;
this.radioButton1.Location = new System.Drawing.Point(24, 136);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(136, 24);
this.radioButton1.TabIndex = 3;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "查询窗体(Form)";
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(24, 160);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(192, 24);
this.radioButton2.TabIndex = 4;
this.radioButton2.Text = "查询窗体上的控件(Control)";
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "窗体与控件查询(窗体收集方式)";
this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run( new Form1() );
}
private void button1_Click(object sender, System.EventArgs e)
{
string FormName = textBox1.Text.Trim();
string ControlName = textBox2.Text.Trim();
string str = "";
str = "QueryType : " + QueryType.ToString() + "/n/n";
if( QueryType == UcQueryType.Form )
{
Form currForm = UcFormCollector.QueryForm( FormName );
if( currForm == null )
{
str += "Object is not found!";
}
else
{
str += "Text is : /"" + currForm.Text + "/"";
}
}
else if( QueryType == UcQueryType.Control )
{
Control currControl = UcFormCollector.QueryControl( FormName , ControlName );
if( currControl == null )
{
str += "Object is not found!";
}
else
{
str += "Text is : /"" + currControl.Text + "/"";
}
}
MessageBox.Show( str );
}
private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
{
QueryType = UcQueryType.Form;
}
private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
{
QueryType = UcQueryType.Control;
}
}
}
==============================================================
新建窗体 取名为 : Form2.cs
==============================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace QueryForm1
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
UcFormCollector.AddForm( this );
UcFormCollector.AddForm( this ); //重复无效
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(56, 64);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "被查询窗体2的文本框:textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(192, 23);
this.button1.TabIndex = 1;
this.button1.Text = "被查询窗体2的按钮:button1";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "被查询窗体2";
this.ResumeLayout(false);
}
#endregion
}
}
==============================================================
新建窗体 取名为 : Form3.cs
==============================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace QueryForm1
{
/// <summary>
/// Form3 的摘要说明。
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form3()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
UcFormCollector.AddForm( this );
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(56, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "被查询窗体3的文本框:textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(200, 23);
this.button1.TabIndex = 1;
this.button1.Text = "被查询窗体3的按钮:button1";
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form3";
this.Text = "被查询窗体3";
this.ResumeLayout(false);
}
#endregion
}
}
==============================================================