C#(Winfrom)自定義控件--組合控件方式
本例是制作一個(gè)簡(jiǎn)單的自定義控件,然后用一個(gè)簡(jiǎn)單的測(cè)試程序,
對(duì)于初學(xué)者來(lái)說(shuō),本例子比較簡(jiǎn)單,只能起到拋石引玉的效果。
我也是在學(xué)習(xí)當(dāng)中,今后會(huì)將自己所學(xué)的逐步寫出來(lái)和大家交流共享。
創(chuàng)建自定義控件
第一步:創(chuàng)建控件庫(kù)項(xiàng)目:MyControl

第二步:從工具箱中拖出1個(gè)TextBox和Button控件

設(shè)置背景為透明

在項(xiàng)目中添加一個(gè)Windows窗體程序



第三步:添加處理程序代碼
PopControls中定義自定義控件的屬性及button事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyControls
{
public partial class PopControls : UserControl
{
public static string s;
public PopControls()
{
InitializeComponent();
}
//定義自定義的屬性
[Browsable(true)]
[Description("點(diǎn)擊按鈕,窗體的寬度,單位時(shí)像素"),Category("自定義屬性"),DefaultValue(0)]
public int PopWidth { get; set; }
[Browsable(true)]
[Description("點(diǎn)擊按鈕,窗體的高,單位時(shí)像素"), Category("自定義屬性"), DefaultValue(0)]
public int PopHeight { get; set; }
[Browsable(true)]
[Description("窗體的Text屬性"), Category("自定義屬性"), DefaultValue(0)]
public string PopText { get; set; }
private void btnok_Click(object sender, EventArgs e)
{
FrmPop frm = new FrmPop(PopWidth,PopHeight,PopText);
frm.ShowDialog();
txtnode.Text = s;
}
}
}
FrmPop窗體代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyControls
{
public partial class FrmPop : Form
{
public FrmPop(int PopWidth,int PopHeight,string PopText)
{
InitializeComponent();
this.ClientSize = new System.Drawing.Size(PopWidth, PopHeight);
this.Text = PopText;
}
private void FrmPop_Load(object sender, EventArgs e)
{
for (int i = 0; i < 50; i++)
{
//隨機(jī)數(shù)
string s = Guid.NewGuid().ToString("N");
listBoxGuid.Items.Add(s);
}
}
private void listBoxGuid_DoubleClick(object sender, EventArgs e)
{
PopControls.s = listBoxGuid.SelectedItem.ToString();
this.Close();
}
}
}
第四步:測(cè)試控件


雙擊listbox中的數(shù)據(jù)

第五步:查看成生的控件文件,到該項(xiàng)目文件目錄下的bin->debug中可找到。
使用自定義控件
第一步:創(chuàng)建Windows窗體程序

第二步:添加自定義控件
右鍵選中添加選項(xiàng)卡

在自定義控件下右鍵點(diǎn)擊選擇項(xiàng),彈框如下圖:

點(diǎn)擊瀏覽,找到自定義控件項(xiàng)目–bin–debug–Mycontrols.dll文件

選中控件文件 Mycontrols.dll ,單擊“打開”按鈕,回到自定義工具箱,系統(tǒng)會(huì)默認(rèn)把你剛才選中的控件打上 勾

返回vs編輯器,可看到工具箱中發(fā)現(xiàn)PopControls控件:

第三步:拖動(dòng)1個(gè)自定義的控件到測(cè)試窗口

第四步:測(cè)試自定義控件


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# 向Word中設(shè)置/更改文本方向的方法(兩種)
在一般情況下word中輸入的文字都是橫向的,今天小編給大家?guī)?lái)兩種方法來(lái)設(shè)置更改文本方向的方法,非常不錯(cuò),對(duì)c# word 更改文本方向的知識(shí)感興趣的朋友一起看看吧2016-08-08
C# VB 實(shí)現(xiàn)10進(jìn)制 16進(jìn)制之間互相轉(zhuǎn)換
如何將10進(jìn)制轉(zhuǎn)成16進(jìn)制,又如何將16進(jìn)制數(shù)轉(zhuǎn)成10進(jìn)制,本文將介紹C#和VB實(shí)現(xiàn)代碼,需要了解的朋友可以參考下2012-11-11

