【C#中的自定義控件中的屬性、事件及一些相關特性的總結】
在上面這篇博文中我們知道了我們所使用的控件中的屬性、事件是如何定義的,然後我們就可以在其基礎上進行控件的二次開發了。
添加類庫 添加組件
引用添加框架 System.Windows.Forms
using System.Windows.Forms
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyUIControls
{
public partial class SuperTextBox : TextBox
{
public SuperTextBox()
{
InitializeComponent();
}
public SuperTextBox(IContainer container)
{
container.Add(this);
InitializeComponent();
}
/// <summary>
/// 單獨的非空檢查
/// </summary>
/// <returns></returns>
public int BeginCheckEmpty()
{
if (this.Text.Trim() == "")
{
this.errorProvider.SetError(this, "必填項不能爲空!");
return 0;//爲空時候直接返回
}
else
{
this.errorProvider.SetError(this, string.Empty); //清除小圓點錯誤顯示
return 1;
}
}
/// <summary>
/// 通用的正則表達式驗證複雜數據
/// </summary>
/// <param name="regularExpress">驗證用的正則表達式</param>
/// <param name="errorMsg">驗證錯誤提示信息</param>
/// <returns>返回0代表驗證不通過,1代表驗證通過</returns>
public int BeginCheckData(string regularExpress, string errorMsg)
{
if (BeginCheckEmpty() == 0) return 0;//如果爲空,則直接返回
if (string.IsNullOrEmpty(regularExpress)) return 1;//如果不驗證則直接返回
//正則表達式驗證
Regex objRegex = new Regex(regularExpress, RegexOptions.IgnoreCase);
if (!objRegex.IsMatch(this.Text))
{
this.errorProvider.SetError(this, errorMsg);
return 0;
}
else
{
this.errorProvider.SetError(this, string.Empty);//清除錯誤顯示
return 1;
}
}
/// <summary>
/// 驗證正數(正整數或精確到兩位的小數)
/// </summary>
/// <param name="errorMsg"></param>
/// <returns></returns>
public int BeginCheckDataIsPositive(string errorMsg)
{
return this.BeginCheckData(@"^\d+(.\d{1,2})?$", errorMsg);
}
/// <summary>
/// 驗證純數字
/// </summary>
/// <returns></returns>
public int BeginCheckDataIsNumber()
{
return this.BeginCheckData(@"^[0-9]*$", "必須爲數字!");
}
}
}
其他項目可通過工具箱添加組件dll
2.自定義用戶控件
添加類庫 添加用戶控件
如添加lebel 修改autosize borderSize fixedSingle text爲空 拖放label 修改backcolor背景色等,兩個label標籤疊加,添加百分比label
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 MyUIControls
{
public partial class VerticalProcessBar : UserControl
{
public VerticalProcessBar()
{
InitializeComponent();
}
/// <summary>
/// 【進度條長度】
/// </summary>
private int pHeight = 240;
public int PHeight
{
get { return pHeight; }
set
{
if (value < 100 | value > 500)
{
MessageBox.Show("value值必須在100-500之間。", "信息提示");
}
else
{
this.LblLong.Height = value;
this.LblShort.Height = value;
this.pHeight = value;
}
}
}
/// <summary>
/// 【進度條寬度】
/// </summary>
private int pWidth = 15;
public int PWidth
{
get { return pWidth; }
set
{
if (value < 10 | value > 50)
{
MessageBox.Show("value值必須在10-50之間。", "信息提示");
}
else
{
this.LblLong.Width = value;
this.LblShort.Width = value;
}
}
}
/// <summary>
/// 【進度值】
/// </summary>
private double pValue = 0;
public double PValue
{
get { return pValue; }
set
{
if (value > 100.00 | value < 0)
{
MessageBox.Show("value值必須在0-100之間。" + pValue, "信息提示");
}
else
{
//顯示當前進度圖像
double processValue = (this.pHeight / 100.00) * value;//計算當前進度對應的進度條高度
this.LblShort.Height = LblLong.Height - System.Convert.ToInt32(processValue);
this.lblPercent.Text = value.ToString() + "%";//顯示進度百分比
this.pValue = value; //保存當前進度的百分比(爲保存到後臺做準備,比如上位機定時採樣)
}
}
}
#region 其他屬性設置:進度百分比文字、進度條顏色、進度條邊框
/// <summary>
/// 是否顯示進度百分比文字
/// </summary>
private bool isShowPercent = true;
public bool IsShowPercent
{
get { return isShowPercent; }
set {
isShowPercent = value;
this.lblPercent.Visible = value;
}
}
/// <summary>
/// 【進度條顯示的顏色】
/// </summary>
private System.Drawing.Color pColor =
System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
public System.Drawing.Color PColor
{
get { return pColor; }
set
{
pColor = value;
this.LblLong.BackColor = value;
}
}
/// <summary>
/// 【進度條的邊框樣式】
/// </summary>
private System.Windows.Forms.BorderStyle borderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
public System.Windows.Forms.BorderStyle PBorderStyle
{
get { return borderStyle; }
set
{
if (value == BorderStyle.FixedSingle)
{
this.LblLong.BorderStyle = value;
this.LblShort.BorderStyle = value;
}
else if (value == BorderStyle.Fixed3D)
{
this.LblShort.BorderStyle = value;
this.LblLong.BorderStyle = BorderStyle.None;
}
else
{
this.LblLong.BorderStyle = value;
this.LblShort.BorderStyle = value;
}
this.borderStyle = value;//可以不寫
}
}
#endregion
}
}