您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#中TextBox的橫線樣式及占位提示怎么實(shí)現(xiàn)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇C#中TextBox的橫線樣式及占位提示怎么實(shí)現(xiàn)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
.NET Framework版本:4.5
Visual Studio 2013
public partial class TextBoxP : TextBox { private const int EM_SETCUEBANNER = 0x1501; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam); Panel panel = new Panel(); public TextBoxP() { InitializeComponent(); this.BorderStyle = BorderStyle.FixedSingle; this.Font = new Font("宋體", 12f); } private string _Placeholder; [Browsable(true)] [Description("設(shè)置提示信息")] public string Placeholder { get { return _Placeholder; } set { _Placeholder = value; SendMessage(Handle, EM_SETCUEBANNER, 0, _Placeholder); } } private bool _IsLineStyle; [Browsable(true)] [Description("設(shè)置以橫線樣式顯示")] public bool IsLineStyle { get { return _IsLineStyle; } set { _IsLineStyle = value; SetLineStyle(); } } private void SetLineStyle() { if (_IsLineStyle && !this.Controls.Contains(panel)) { this.BorderStyle = BorderStyle.None; this.SuspendLayout(); panel.Height = 1; panel.Width = this.Width; panel.BorderStyle = BorderStyle.FixedSingle; panel.Location = new Point(0, this.Height - 1); this.Controls.Add(panel); this.ResumeLayout(); this.PerformLayout(); this.SizeChanged += TextBoxP_SizeChanged; this.LocationChanged += TextBoxP_LocationChanged; } else if (!_IsLineStyle) { if (this.Controls.Contains(panel)) { this.Controls.Remove(panel); } this.BorderStyle = BorderStyle.FixedSingle; this.SizeChanged -= TextBoxP_SizeChanged; this.LocationChanged -= TextBoxP_LocationChanged; } if (!string.IsNullOrWhiteSpace(_Placeholder)) { SendMessage(Handle, EM_SETCUEBANNER, 0, _Placeholder); } } void TextBoxP_SizeChanged(object sender, EventArgs e) { panel.Width = this.Width; } void TextBoxP_LocationChanged(object sender, EventArgs e) { panel.Location = new Point(0, this.Height - 1); } }
private void button1_Click(object sender, EventArgs e) { textBoxP1.IsLineStyle = !textBoxP1.IsLineStyle; textBoxP1.BackColor = textBoxP1.IsLineStyle ? SystemColors.Control : Color.White; textBoxP2.IsLineStyle = !textBoxP2.IsLineStyle; textBoxP2.BackColor = textBoxP2.IsLineStyle ? SystemColors.Control : Color.White; }
代碼解析:Placeholder功能是使用Win APi做的,不得不說(shuō),這個(gè)方式的確是簡(jiǎn)單。一開(kāi)始是想著可以用字體顏色以及對(duì)應(yīng)的事件做到,但是效果不太完美,因?yàn)橛眠@種方式說(shuō)到底還是對(duì)Text屬性的操作,最后獲取的時(shí)候還是會(huì)有問(wèn)題,即便經(jīng)過(guò)判斷過(guò)濾之后,仍然感覺(jué)不太好用,最重要的是:麻煩!
然后就是橫線樣式顯示,這里是使用增加一個(gè)Panel控件來(lái)實(shí)現(xiàn),其實(shí)我一直覺(jué)得處理自定義控件的話,將樣式處理放在Paint事件中處理會(huì)比較完美,但是TextBox的Paint事件,有點(diǎn)難用。所以還是感覺(jué)這種方式簡(jiǎn)單、有效!
關(guān)于“C#中TextBox的橫線樣式及占位提示怎么實(shí)現(xiàn)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“C#中TextBox的橫線樣式及占位提示怎么實(shí)現(xiàn)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。