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

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

設(shè)置背景為透明

在項目中添加一個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)擊按鈕,窗體的寬度,單位時像素"),Category("自定義屬性"),DefaultValue(0)]
public int PopWidth { get; set; }
[Browsable(true)]
[Description("點(diǎn)擊按鈕,窗體的高,單位時像素"), 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();
}
}
}
第四步:測試控件


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

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

第二步:添加自定義控件
右鍵選中添加選項卡

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

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

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

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

第三步:拖動1個自定義的控件到測試窗口

第四步:測試自定義控件


總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
WPF自定義控件實(shí)現(xiàn)ItemsControl魚眼效果
這篇文章主要為大家詳細(xì)介紹了WPF如何通過自定義控件實(shí)現(xiàn)ItemsControl魚眼效果,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-01-01
使用C#開發(fā)OPC?Server服務(wù)器源碼解析
OPC?Server服務(wù)器服務(wù)器的開發(fā)比較繁瑣,本示例采用C#提供了一種簡單快速實(shí)現(xiàn)OPCServer的方法,已經(jīng)在工程項目中應(yīng)用,本文對C#開發(fā)OPC?Server服務(wù)器相關(guān)知識給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
在C#中將Word轉(zhuǎn)換為PostScript的代碼實(shí)現(xiàn)
在需要高質(zhì)量打印或跨平臺文檔分發(fā)的場景中,將Word轉(zhuǎn)換為PostScript是專業(yè)選擇,所以本文給大家介紹了如何在 C# 中將 Word 轉(zhuǎn)換為 PostScript,需要的朋友可以參考下2025-09-09
C#如何動態(tài)創(chuàng)建Label,及動態(tài)label事件
這篇文章主要介紹了C#如何動態(tài)創(chuàng)建Label,及動態(tài)label事件,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

