Winform-TextBox實現 placeholder


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控件。
然后再工具箱——選擇項,功能添加進來,就能“一勞永逸”了。

你可以建一個用戶控鍵頁面,編寫你想的寫的代碼,之后在別的頁面只須將其拖入就可以使用了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM