在C#中使用者控制項與表單間的訊息傳遞

來源:互聯網
上載者:User

如果在c#中實現使用者定義控制項內的事件掛勾到調用的表單事件中

           我們都知道在asp.net開發中,如果使用使用者定義控制項可以有效進行程式的模組化。其實在.net  的winform中也是可以使用的。
細節如下:1、建立應用程式windowsapplication1。2、添加新的使用者控制項userlogin。(user)3、定義使用者屬性
      //定義屬性
  public string username
  {
   get{return username;}
   set{username=value;}
  }
  public string password
  {
   get{return password;}
   set{password=value;}
  }
            4、定義委託
 //定義委託
 public delegate void btnokclickeventhander(object sender,eventargs e);
 public delegate void btncancelclickeventhander(object sender,eventargs e);
            5、定義事件
  //定義事件
  public event btnokclickeventhander btnokclick;
  public event btncancelclickeventhander btncancelclick
6、事件實現
private void textboxuid_textchanged(object sender, system.eventargs e)
  {
   username=this.textboxuid.text;
  }  private void textboxpwd_textchanged(object sender, system.eventargs e)
  {
   password=this.textboxpwd.text;
  }  private void buttonok_click(object sender, system.eventargs e)
  {
   if (btnokclick!=null)
     btnokclick(this,e);
  }  private void buttoncancel_click(object sender, system.eventargs e)
  {
   if (btncancelclick!=null)
    btncancelclick(this,e);
  }
7、在form1的winform中實現對使用者控制項事件的調用,訊息的接收。
protected void okclick(object send,system.eventargs e)
  {
   messagebox.show("uid:"+userlogin1.username+";pwd:"+userlogin1.password,"usermessage");
  }  protected void cancelclick(object send,system.eventargs e)
  {
   this.close();
  }
8.按f5運行(result)附1(windowsapplication1原始碼)
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;namespace windowsapplication1
{
 /// <summary>
 /// form1 的摘要說明。
 /// </summary>
 public class form1 : system.windows.forms.form
 {
  private system.windows.forms.panel panel1;
  private windowsapplication1.userlogin userlogin1;
  /// <summary>
  /// 必需的設計器變數。
  /// </summary>
  private system.componentmodel.container components = null;  public form1()
  {
   //
   // windows 表單設計器支援所必需的
   //
   initializecomponent();
   userlogin1.btnokclick+=new btnokclickeventhander(okclick);
   userlogin1.btncancelclick+=new btncancelclickeventhander(cancelclick);
   //
   // todo: 在 initializecomponent 調用後添加任何建構函式代碼
   //
  }  /// <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.panel1 = new system.windows.forms.panel();
   this.userlogin1 = new windowsapplication1.userlogin();
   this.panel1.suspendlayout();
   this.suspendlayout();
   //
   // panel1
   //
   this.panel1.controls.add(this.userlogin1);
   this.panel1.dock = system.windows.forms.dockstyle.fill;
   this.panel1.location = new system.drawing.point(0, 0);
   this.panel1.name = "panel1";
   this.panel1.size = new system.drawing.size(216, 101);
   this.panel1.tabindex = 1;
   //
   // userlogin1
   //
   this.userlogin1.dock = system.windows.forms.dockstyle.fill;
   this.userlogin1.location = new system.drawing.point(0, 0);
   this.userlogin1.name = "userlogin1";
   this.userlogin1.size = new system.drawing.size(216, 101);
   this.userlogin1.tabindex = 0;
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(216, 101);
   this.controls.add(this.panel1);
   this.name = "form1";
   this.text = "form1";
   this.panel1.resumelayout(false);
   this.resumelayout(false);  }
  #endregion  /// <summary>
  /// 應用程式的主進入點。
  /// </summary>
  [stathread]
  static void main()
  {
   application.run(new form1());
  }
  protected void okclick(object send,system.eventargs e)
  {
   messagebox.show("uid:"+userlogin1.username+";pwd:"+userlogin1.password,"usermessage");
  }  protected void cancelclick(object send,system.eventargs e)
  {
   this.close();
  } }
}
 附2(userlogin原始碼)
using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.data;
using system.windows.forms;namespace windowsapplication1
{
 /// <summary>
 /// usercontrol1 的摘要說明。
 /// </summary>
 /// //定義委託
 public delegate void btnokclickeventhander(object sender,eventargs e);
 public delegate void btncancelclickeventhander(object sender,eventargs e);
 public class userlogin : system.windows.forms.usercontrol
 {
  private string username,password;
  private system.windows.forms.groupbox groupbox1;
  private system.windows.forms.button buttoncancel;
  private system.windows.forms.button buttonok;
  private system.windows.forms.textbox textboxpwd;
  private system.windows.forms.label label2;
  private system.windows.forms.label label1;
  private system.windows.forms.textbox textboxuid;
  /// <summary>
  /// 必需的設計器變數。
  /// </summary>
  private system.componentmodel.container components = null;  public userlogin()
  {
   // 該調用是 windows.forms 表單設計器所必需的。
   initializecomponent();   // todo: 在 initializecomponent 調用後添加任何初始化  }  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }  #region 組件設計器產生的程式碼
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器
  /// 修改此方法的內容。
  /// </summary>
  private void initializecomponent()
  {
   this.groupbox1 = new system.windows.forms.groupbox();
   this.buttoncancel = new system.windows.forms.button();
   this.buttonok = new system.windows.forms.button();
   this.textboxpwd = new system.windows.forms.textbox();
   this.label2 = new system.windows.forms.label();
   this.label1 = new system.windows.forms.label();
   this.textboxuid = new system.windows.forms.textbox();
   this.groupbox1.suspendlayout();
   this.suspendlayout();
   //
   // groupbox1
   //
   this.groupbox1.controls.add(this.buttoncancel);
   this.groupbox1.controls.add(this.buttonok);
   this.groupbox1.controls.add(this.textboxpwd);
   this.groupbox1.controls.add(this.label2);
   this.groupbox1.controls.add(this.label1);
   this.groupbox1.controls.add(this.textboxuid);
   this.groupbox1.location = new system.drawing.point(8, 2);
   this.groupbox1.name = "groupbox1";
   this.groupbox1.size = new system.drawing.size(200, 96);
   this.groupbox1.tabindex = 6;
   this.groupbox1.tabstop = false;
   //
   // buttoncancel
   //
   this.buttoncancel.location = new system.drawing.point(112, 71);
   this.buttoncancel.name = "buttoncancel";
   this.buttoncancel.size = new system.drawing.size(56, 20);
   this.buttoncancel.tabindex = 11;
   this.buttoncancel.text = "&cancel";
   this.buttoncancel.click += new system.eventhandler(this.buttoncancel_click);
   //
   // buttonok
   //
   this.buttonok.location = new system.drawing.point(32, 71);
   this.buttonok.name = "buttonok";
   this.buttonok.size = new system.drawing.size(56, 20);
   this.buttonok.tabindex = 10;
   this.buttonok.text = "&ok";
   this.buttonok.click += new system.eventhandler(this.buttonok_click);
   //
   // textboxpwd
   //
   this.textboxpwd.location = new system.drawing.point(72, 43);
   this.textboxpwd.name = "textboxpwd";
   this.textboxpwd.passwordchar = *;
   this.textboxpwd.size = new system.drawing.size(112, 21);
   this.textboxpwd.tabindex = 9;
   this.textboxpwd.text = "";
   this.textboxpwd.textchanged += new system.eventhandler(this.textboxpwd_textchanged);
   //
   // label2
   //
   this.label2.location = new system.drawing.point(9, 42);
   this.label2.name = "label2";
   this.label2.size = new system.drawing.size(64, 23);
   this.label2.tabindex = 8;
   this.label2.text = "password:";
   this.label2.textalign = system.drawing.contentalignment.middlecenter;
   //
   // label1
   //
   this.label1.location = new system.drawing.point(9, 14);
   this.label1.name = "label1";
   this.label1.size = new system.drawing.size(64, 23);
   this.label1.tabindex = 7;
   this.label1.text = "username:";
   this.label1.textalign = system.drawing.contentalignment.middlecenter;
   //
   // textboxuid
   //
   this.textboxuid.location = new system.drawing.point(72, 15);
   this.textboxuid.name = "textboxuid";
   this.textboxuid.size = new system.drawing.size(112, 21);
   this.textboxuid.tabindex = 6;
   this.textboxuid.text = "";
   this.textboxuid.textchanged += new system.eventhandler(this.textboxuid_textchanged);
   //
   // userlogin
   //
   this.controls.add(this.groupbox1);
   this.name = "userlogin";
   this.size = new system.drawing.size(216, 104);
   this.groupbox1.resumelayout(false);
   this.resumelayout(false);  }
  #endregion
  //定義屬性
  public string username
  {
   get{return username;}
   set{username=value;}
  }
  public string password
  {
   get{return password;}
   set{password=value;}
  }
  
  //定義事件
  public event btnokclickeventhander btnokclick;
  public event btncancelclickeventhander btncancelclick;
  private void textboxuid_textchanged(object sender, system.eventargs e)
  {
   username=this.textboxuid.text;
  }  private void textboxpwd_textchanged(object sender, system.eventargs e)
  {
   password=this.textboxpwd.text;
  }  private void buttonok_click(object sender, system.eventargs e)
  {
   if (btnokclick!=null)
     btnokclick(this,e);
  }  private void buttoncancel_click(object sender, system.eventargs e)
  {
   if (btncancelclick!=null)
    btncancelclick(this,e);
  }
  
 }
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.