2004-11-30做過asp.net的人都知道開發(fā)的時(shí)候使用用戶控件很方便,為功能模塊化提供了相當(dāng)大的靈活性。令人高興的是開發(fā)Windows窗體也可以使用用戶控件。這里我們來看" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

在.net應(yīng)用程序中使用用戶控件

系統(tǒng) 1599 0

鄭佐 <chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="11" year="2004"><span lang="EN-US" style="FONT-SIZE: 9pt">2004-11-30</span></chsdate>

做過 asp.net 的人都知道開發(fā)的時(shí)候使用用戶控件很方便,為功能模塊化提供了相當(dāng)大的靈活性。令人高興的是開發(fā) Windows 窗體也可以使用用戶控件。這里我們來看看為用戶控件添加屬性和事件,并實(shí)現(xiàn)把消息發(fā)送到父容器。本文主要是為沒有使用過用戶控件的朋友提供一些參考。

用戶控件的實(shí)現(xiàn)比較簡單,直接從 System.Windows.Forms.UserControl

public class UserControl1 : System.Windows.Forms.UserControl

為了便于測試我在上面添加了一個(gè) TextBox, 并注冊 TextBox TextChanged 事件,

this .textBox1.TextChanged += new System.EventHandler( this .textBox1_TextChanged);

事件處理函數(shù),

private void textBox1_TextChanged( object sender, System.EventArgs e)

{

MessageBox.Show( this .textBox1.Text);

}

這里演示如果控件中文本框的內(nèi)容改變就會(huì)用 MessageBox 顯示當(dāng)前的文本框內(nèi)容。

控件顯示如下:

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype>

用戶控件圖片
在窗體中添加上面的用戶控件,當(dāng)我們改變 textBox 的文本時(shí),可以看到跳出一個(gè)對話框,很簡單吧。

下面來看看對控件添加屬性。

這里定義一個(gè)私有變量。

private string customValue;

添加訪問他的屬性

public string CustomValue

{

get { return customValue;}

set {customValue = value ;}

}

在窗體中使用的時(shí)候像普通控件一樣進(jìn)行訪問,

userControl11.CustomValue = " 用戶控件自定義數(shù)據(jù) ";

通過事件可以傳遞消息到窗體上,在定義之前我們先來寫一個(gè)簡單的參數(shù)類。

public class TextChangeEventArgs : EventArgs

{

private string message;

public TextChangeEventArgs( string message)

{

this .message = message;

}

public string Message

{

get { return message;}

}

}

定義委托為,

public delegate void TextBoxChangedHandle( object sender,TextChangeEventArgs e);

接下去在用戶控件中添加事件,

// 定義事件

public event TextBoxChangedHandle UserControlValueChanged;

為了激發(fā)用戶控件的新增事件,修改了一下代碼,

private void textBox1_TextChanged( object sender, System.EventArgs e)

{

if (UserControlValueChanged != null )

UserControlValueChanged( this , new TextChangeEventArgs( this .textBox1.Text));

}

好了,為了便于在 Csdn 上回答問題,把完整的代碼貼了出來:

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

namespace ZZ.WindowsApplication1

{

public class UserControl1 : System.Windows.Forms.UserControl

{

private System.Windows.Forms.TextBox textBox1;

private string customValue;

private System.ComponentModel.Container components = null ;

public string CustomValue

{

get { return customValue;}

set {customValue = value ;}

}

// 定義事件

public event TextBoxChangedHandle UserControlValueChanged;

public UserControl1()

{

InitializeComponent();

}

protected override void Dispose( bool disposing )

{

if ( disposing )

{

if (components != null )

{

components.Dispose();

}

}

base .Dispose( disposing );

}

#region 組件設(shè)計(jì)器生成的代碼

private void InitializeComponent()

{

this .textBox1 = new System.Windows.Forms.TextBox();

this .SuspendLayout();

this .textBox1.Location = new System.Drawing.Point(12, 36);

this .textBox1.Name = "textBox1";

this .textBox1.TabIndex = 0;

this .textBox1.Text = "textBox1";

this .textBox1.TextChanged += new System.EventHandler( this .textBox1_TextChanged);

this .Controls.Add( this .textBox1);

this .Name = "UserControl1";

this .Size = new System.Drawing.Size(150, 92);

this .ResumeLayout( false );

}

#endregion

private void textBox1_TextChanged( object sender, System.EventArgs e)

{

if (UserControlValueChanged != null )

UserControlValueChanged( this , new TextChangeEventArgs( this .textBox1.Text));

}

}

// 定義委托

public delegate void TextBoxChangedHandle( object sender,TextChangeEventArgs e);

public class TextChangeEventArgs : EventArgs

{

private string message;

public TextChangeEventArgs( string message)

{

this .message = message;

}

public string Message

{

get { return message;}

}

}

}

使用時(shí)要在窗體中注冊上面的事件,比較簡單都貼源代碼了,

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace ZZ.WindowsApplication1

{

public class Form1 : System.Windows.Forms.Form

{

private WindowsApplication1.UserControl1 userControl11;

private System.ComponentModel.Container components = null ;

public Form1()

{

InitializeComponent();

userControl11.CustomValue = " 用戶控件自定義數(shù)據(jù) ";

userControl11.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);

}

protected override void Dispose( bool disposing )

{

if ( disposing )

{

if (components != null )

{

components.Dispose();

}

}

base .Dispose( disposing );

}

#region Windows 窗體設(shè)計(jì)器生成的代碼

private void InitializeComponent()

{

this .userControl11 = new WindowsApplication1.UserControl1();

this .SuspendLayout();

this .userControl11.Location = new System.Drawing.Point(8, 8);

this .userControl11.Name = "userControl11";

this .userControl11.Size = new System.Drawing.Size(150, 84);

this .userControl11.TabIndex = 0;

this .AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this .ClientSize = new System.Drawing.Size(292, 193);

this .Controls.Add( this .userControl11);

this .Name = "Form1";

this .Text = "Form1";

this .ResumeLayout( false );

}

#endregion

[STAThread]

static void <place w:st="on">Main</place>()

{

Application.Run( new Form1());

}

private void userControl11_UserControlValueChanged( object sender, TextChangeEventArgs e)

{

MessageBox.Show(" 當(dāng)前控件的值為 :" + e.Message);

}

}

}

另外需要?jiǎng)討B(tài)加載,就把控件添加在容器的 Controls 集合就行了,下面是在構(gòu)造函數(shù)中添加控件,

public Form1()

{

InitializeComponent();

UserControl1 uc = new UserControl1();

uc.CustomValue = " 動(dòng)態(tài)加載的用戶控件 ";

uc.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);

this .Controls.Add(uc);

}

另外從 VS.net 中的工具箱中拖動(dòng)用戶控件到窗體上,如果是第一次需要編譯一下項(xiàng)目。



在.net應(yīng)用程序中使用用戶控件


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91伦理视频| 奇米第四色影视 | 国内精品福利在线视频 | 国产在线观看91 | 99热久久国产综合精品久久国产 | 性生活视频免费观看 | 天天干天天操天天 | 夜夜资源 | 亚洲国产欧美在线观看 | 日本不卡视频在线视频观看 | 黄页成人免费网站 | 伊人第一页 | 久久精品亚洲精品国产色婷 | 国产极品嫩模大尺度福利视频 | 蜜桃综合 | 午夜爱爱网 | 久久精品国产99国产精品免费看 | 国产成人综合精品 | 精品视频在线观看一区二区三区 | 四虎a级欧美在线观看 | 奇米第四色首页 | 成人毛片18女人毛片 | 久青草资源福利视频 | 欧美一级成人一区二区三区 | 亚洲欧洲日韩国产一区二区三区 | 欧美一区二区三区高清视频 | 国产麻豆永久视频 | 亚洲欧洲精品在线 | 嘿咻嘿咻免费区在线观看吃奶 | 亚洲国产成人精品久久 | 一级女性全黄生活片免费 | 久久伊人免费视频 | 特级全黄一级毛片视频 | 国内色视频| 日本不卡三级 | 神马影院在线观看我不卡 | 中文字幕欧美日韩在线不卡 | 国内亚州视频在线观看 | 国产精品手机视频 | 久久久久这里只有精品 | 快射视频欧美 |