public class TextBoxEx : TextBox { public String PlaceHolderStr { get; set; } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0xF || m.Msg == 0x133) { WmPaint(ref m); } } private void WmPaint(ref Message m) { Graphics g = Graphics.FromHwnd(base.Handle); if (!String.IsNullOrEmpty(this.PlaceHolderStr) && string.IsNullOrEmpty(this.Text)) g.DrawString(this.PlaceHolderStr, this.Font, new SolidBrush(Color.LightGray), 0, 0); } }
然后在Designer.cs即設計器里面實現該重寫的控件
this.ADTextBox = new frmInfoBarCodeFeeDetailed.TextBoxEx();
然后控件屬性就會出現所重寫的屬性了,設置即可
完畢!!!
1. 首先你把一個label控件拖到窗體上。
2. 創建一個准備繼承的類,比如叫MyLabel,派生於Label類
class MyLabel : System.Windows.Forms.Label
{
// 你自己重寫的內容
}
3. 打開Form1.Designer.cs文件,你會看到有這么一行。
private System.Windows.Forms.Label label1;
修改為
private MyLabel label1;
再找到this.label1 = new System.Windows.Forms.Label();
修改為this.label1 = new MyLabel();
然后編譯,運行,此時窗體上的這個控件就是MyLabel類創造出來的了。
________________________________________________
或者這樣也行,創建——Windows控件庫,自己創建一個重寫后的labal控件。
然后再工具箱——選擇項,功能添加進來,就能“一勞永逸”了。
你可以建一個用戶控鍵頁面,編寫你想的寫的代碼,之后在別的頁面只須將其拖入就可以使用了。