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

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

在.net應用程序中使用用戶控件

系統 1672 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 的人都知道開發的時候使用用戶控件很方便,為功能模塊化提供了相當大的靈活性。令人高興的是開發 Windows 窗體也可以使用用戶控件。這里我們來看看為用戶控件添加屬性和事件,并實現把消息發送到父容器。本文主要是為沒有使用過用戶控件的朋友提供一些參考。

用戶控件的實現比較簡單,直接從 System.Windows.Forms.UserControl

public class UserControl1 : System.Windows.Forms.UserControl

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

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

事件處理函數,

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

{

MessageBox.Show( this .textBox1.Text);

}

這里演示如果控件中文本框的內容改變就會用 MessageBox 顯示當前的文本框內容。

控件顯示如下:

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

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

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

這里定義一個私有變量。

private string customValue;

添加訪問他的屬性

public string CustomValue

{

get { return customValue;}

set {customValue = value ;}

}

在窗體中使用的時候像普通控件一樣進行訪問,

userControl11.CustomValue = " 用戶控件自定義數據 ";

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

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;

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

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 組件設計器生成的代碼

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;}

}

}

}

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

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 = " 用戶控件自定義數據 ";

userControl11.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);

}

protected override void Dispose( bool disposing )

{

if ( disposing )

{

if (components != null )

{

components.Dispose();

}

}

base .Dispose( disposing );

}

#region Windows 窗體設計器生成的代碼

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(" 當前控件的值為 :" + e.Message);

}

}

}

另外需要動態加載,就把控件添加在容器的 Controls 集合就行了,下面是在構造函數中添加控件,

public Form1()

{

InitializeComponent();

UserControl1 uc = new UserControl1();

uc.CustomValue = " 動態加載的用戶控件 ";

uc.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);

this .Controls.Add(uc);

}

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



在.net應用程序中使用用戶控件


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 另类五月天| 99久久九九爱看免费直播 | v视界成人影院在线视频 | 波多野结衣a∨免费观看 | 成人免费看黄网站无遮挡 | 国产成人综合自拍 | 欧美日韩成人影院 | 日本精品一区二区三区四区 | 欧美亚洲国产一区 | 暖暖日本在线播放 | 久久精品国产一区 | 在线中文天堂 | 青青久操视频 | 一区二区三区四区国产 | 欧美午夜精品久久久久免费视 | 久久91精品国产91久久小草 | 亚洲欧美日韩激情在线观看 | 天堂最新资源在线 | 免费看黄在线网站 | 午夜影院在线观看 | 成人黄性视频 | 91精品国产91久久综合 | 丁香花成人另类小说 | 一区二区在线看 | 激情综合欧美 | 久久福利电影 | 极品嫩模私拍后被潜在线观看 | 欧美日本一区 | 久久久久久久成人 | 黄色成年在线观看 | 精品久久久久久久久久久久久久 | 欧美偷偷操| 亚洲aⅴ | 日韩精品无码一区二区三区 | 天天做天天爱天天影视综合 | 婷婷国产精品 | 91精品欧美成人 | 亚洲精品一区二区三区在线观看 | 色网站综合 | 国产三级在线 | 亚洲毛片网站 |