关于不同窗体之间的控件查询 - 方式二(事件与回调)

关于不同窗体之间的控件查询 - 方式二

==============================================================
新建类 取名为 : UcFormQueryExtender.cs
==============================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace QueryForm2
{

 //查询事件委托
 public delegate void QueryObjectEventHandler( UcQueryObjectProp p_QueryOp );
 //回调事件委托
 public delegate void CallBackEventHandler( object p_Object );

 /// <summary>
 /// 窗体控件查询扩展器( 采用事件注册回调方式)
 /// </summary>
 public sealed class UcFormQueryExtender
 {
  public static event QueryObjectEventHandler Query;

  //查询
  public static void DoQuery( UcQueryObjectProp p_QueryOp )
  {
   if( Query != null )
   {
    Query( p_QueryOp );
   }
  }

  public static event CallBackEventHandler CallBack;
 
  //回调
  public static void DoCallBack( object p_Object )
  {
   if( CallBack != null )
   {
    CallBack( p_Object );
   }
  }

  //公共的对象查询方法
  public static void FindObject( Form p_CurrForm , UcQueryObjectProp p_QueryOp )
  {

   //

   UcQueryType QueryType  = p_QueryOp.QueryType;
   string FormName    = p_QueryOp.FormName;
   string ControlName   = p_QueryOp.ControlName;

   //

   object FindObject = null;

   if( FormName.ToLower() != p_CurrForm.Name.ToLower() ){ return ; }

   if( QueryType == UcQueryType.Form ) //查询窗体
   {
    FindObject = p_CurrForm;
   }
   else if( QueryType == UcQueryType.Control ) //查询控件
   {
    foreach( Control ctr in p_CurrForm.Controls )
    {
     if( ctr.Name.ToLower() == ControlName.ToLower() )
     {
      FindObject = ctr;
     }
    }
   }

   UcFormQueryExtender.DoCallBack( FindObject );
  
  }

 }

 //查询窗体的方式
 public enum UcQueryType{ Form , Control }

 //查询参数的结构(以结构体方式传递参数)
 public struct UcQueryObjectProp
 {
  public UcQueryType QueryType;
  public string FormName;
  public string ControlName;
 }

}

==============================================================
新建窗体 取名为 : Form1.cs
==============================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace QueryForm2
{
 /// <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 调用后添加任何构造函数代码
   //

   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)
  {

   UcQueryObjectProp QueryOp = new UcQueryObjectProp();

   QueryOp.QueryType = QueryType;
   QueryOp.FormName = textBox1.Text.Trim();
   QueryOp.ControlName = textBox2.Text.Trim();

   UcFormQueryExtender.CallBack += new CallBackEventHandler( CallBack );

   UcFormQueryExtender.DoQuery( QueryOp );

   UcFormQueryExtender.CallBack -= new CallBackEventHandler( CallBack );

  }

  private void CallBack( object p_Object )
  {
   string str = "";

   str = "QueryType : " + QueryType.ToString() + "/n/n";

   if( p_Object == null )
   {
    str += "Object is not found!";
   }
   else
   {
    if( QueryType == UcQueryType.Form )
    {
     str += "Text is : /"" + ( ( Form ) p_Object ).Text + "/"";
    }
    else if( QueryType == UcQueryType.Control )
    {
     str += "Text is : /"" + ( ( Control ) p_Object ).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 QueryForm2
{
 /// <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 调用后添加任何构造函数代码
   //
   
   UcFormQueryExtender.Query += new QueryObjectEventHandler( UcFormQueryExtender_OnQuery );

  }

  private void UcFormQueryExtender_OnQuery( UcQueryObjectProp p_QueryOp  )
  {
   UcFormQueryExtender.FindObject( this , p_QueryOp );
  }

  /// <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(208, 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(208, 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 QueryForm2
{
 /// <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 调用后添加任何构造函数代码
   //

   UcFormQueryExtender.Query += new QueryObjectEventHandler( UcFormQueryExtender_OnQuery );

  }

  private void UcFormQueryExtender_OnQuery( UcQueryObjectProp p_QueryOp  )
  {
   UcFormQueryExtender.FindObject( this , p_QueryOp );
  }

  /// <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(208, 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(208, 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
 }
}
==============================================================

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值