溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中TextBox的橫線樣式及占位提示怎么實現

發布時間:2022-11-15 09:18:56 來源:億速云 閱讀:165 作者:iii 欄目:開發技術

這篇文章主要介紹了C#中TextBox的橫線樣式及占位提示怎么實現的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#中TextBox的橫線樣式及占位提示怎么實現文章都會有所收獲,下面我們一起來看看吧。

開發環境

.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("設置提示信息")]
        public string Placeholder
        {
            get
            {
                return _Placeholder;
            }
            set
            {
                _Placeholder = value;
                SendMessage(Handle, EM_SETCUEBANNER, 0, _Placeholder);
            }
        }

        private bool _IsLineStyle;
        [Browsable(true)]
        [Description("設置以橫線樣式顯示")]
        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;
        }

實現效果

C#中TextBox的橫線樣式及占位提示怎么實現

代碼解析:Placeholder功能是使用Win APi做的,不得不說,這個方式的確是簡單。一開始是想著可以用字體顏色以及對應的事件做到,但是效果不太完美,因為用這種方式說到底還是對Text屬性的操作,最后獲取的時候還是會有問題,即便經過判斷過濾之后,仍然感覺不太好用,最重要的是:麻煩!

然后就是橫線樣式顯示,這里是使用增加一個Panel控件來實現,其實我一直覺得處理自定義控件的話,將樣式處理放在Paint事件中處理會比較完美,但是TextBox的Paint事件,有點難用。所以還是感覺這種方式簡單、有效!

關于“C#中TextBox的橫線樣式及占位提示怎么實現”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C#中TextBox的橫線樣式及占位提示怎么實現”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女