Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Thắc mắc về Winform

23:39 02-08-2018 446 lượt xem 2 bình luận 13:07 04-08-2018

Mình muốn làm cái Text mờ chữ hiển thị và khi người ta đánh sẽ mất đoạn text đó ( Giống như chỗ đăng ký trên đây á) trên DevExpress thì phải dùng toolbox nào v.

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Dinh Tona đã bình luận 13:07 04-08-2018

Bạn tham khảo class sau đây nha, nó kế thừa từ class chuẩn:

class PHTextBox : System.Windows.Forms.TextBox
    {
        System.Drawing.Color DefaultColor; 
        public string PlaceHolderText {get;set;}
        public PHTextBox(string placeholdertext)
        {
            // get default color of text
           DefaultColor = this.ForeColor;
            // Add event handler for when the control gets focus
            this.GotFocus += (object sender, EventArgs e) => 
            {
                this.Text = String.Empty;
                this.ForeColor = DefaultColor;
            };

            // add event handling when focus is lost
            this.LostFocus += (Object sender, EventArgs e) => {
                if (String.IsNullOrEmpty(this.Text) || this.Text == PlaceHolderText)
                {
                    this.ForeColor = System.Drawing.Color.Gray;
                    this.Text = PlaceHolderText;
                }
                else
                {
                    this.ForeColor = DefaultColor;
                }
            };



            if (!string.IsNullOrEmpty(placeholdertext))
            {
                // change style   
                this.ForeColor = System.Drawing.Color.Gray;
                // Add text
                PlaceHolderText = placeholdertext;
                this.Text = placeholdertext;
            }



        }


    }

Bạn New 1 class với tên PHTextBox .cs sau đó paste khúc này vào, sau đó vô trong file Designer, chỉnh cái tên Textbox đó thành tên class mà bạn vừa New đó.

Sau đó là Run và coi kết quả.

Cái này mình thấy trên Stack. Bạn test hộ nhé :3

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 11:20 03-08-2018

nó là hint. web thì gọi là placeholder

Câu hỏi mới nhất