C # WinForm to achieve a light-colored display prompt text in the text box when no content

Want to achieve the function code is:

When the text is not input, the appropriate prompt to appear;

When entering text, the text will be prompted to hide.

 

The principle:

First, inheriting the TextBox class, override the three events of the TextBox (OnHandleCreated, OnTextChanged, OnGotFocus)

Second, the increase can not be directly determined in TextBoxChange, easy drawing frequent GDI

Third, the use Timer to delay GDI +, avoiding frequent drawing

Code:

First add a user control  

 

The new UserControl inheriting TextBox    

    public partial class UserControl1 : TextBox

Generating a way to achieve the text length is determined when the text or change the focus change when Text.Length = 0, the need to use GDI drawing displayed on the TextBox text

     0 = I int; 
Private void REDRAW (the TextBox the textBox) { Graphics G; G = Graphics.FromHwnd (textBox.Handle); IF (textBox.Text.Length <= 0 ) { the Font font = new new the Font ( the this .Font.FontFamily , the this .Font.Size, the this .Font.Style); // set the font, size, weight Brush sbrush = new new SolidBrush (Color.Gray); // set the color g.drawString ( " Please enter the text ..... . ", Font, sbrush, . 1 , . 1 ); g.Save (); I = 0 ; } the else { IF (I == 0 ) { g.Clear (textBox.BackColor); the Font font = new new the Font ( the this .font. the FontFamily, the this .Font.Size, the this .Font.Style); // set the font, size, weight Brush sbrush = new new SolidBrush with ( the this .ForeColor); // set the color g.DrawString(Text, font, sbrush, 0, 0); g.Save(); i++; } } }

After I found the text directly in the drawing above the trigger OnTextChanged, it will cause the interface can not update my page to be displayed.

The reason may be the trigger OnTextChanged, the system default GDI drawing speed is slower than I write this method.

So adding a Timer, when the drawing implement delay GDI text changes

        public UserControl1()
        {
            InitializeComponent();
            timer = new Timer();
            timer.Interval = 30;
            timer.Tick += Timer_Tick;
            timer.Enabled = false;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            ReDraw(this);
            timer.Enabled = false;
        }

Finally OnHandleCreated, OnTextChanged, OnGotFocus three events rewrite

When OnHandleCreated achieve load a text box, the default display prompt text.

When OnTextChanged achieve change text, text to determine whether a prompt appears.

When OnGotFocus regain focus after the TextBox form loses focus, it indicated with the text.

        protected override void OnHandleCreated(EventArgs e)
        {
            ReDraw(this);
            base.OnHandleCreated(e);
        }
        protected override void OnTextChanged(EventArgs e)
        {
            ReDraw(this);
            base.OnTextChanged(e);
        }
        protected override void OnGotFocus(EventArgs e)
        {
            ReDraw(this);
            base.OnGotFocus(e);
        }

 

The code also optimized space, we should be able to use methods other alternatives to delay. I thought to add. .

All the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextBoxControler
{
    public partial class UserControl1 : TextBox
    {
        private Timer timer;
        public UserControl1()
        {
            InitializeComponent();
            timer = new Timer();
            timer.Interval = 30;
            timer.Tick += Timer_Tick;
            timer.Enabled = false;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            ReDraw(this);
            timer.Enabled = false;
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            ReDraw(this);
            base.OnHandleCreated(e);
        }
        protected override void OnTextChanged(EventArgs e)
        {
            ReDraw(this);
            base.OnTextChanged(e);
        }
        protected override void OnGotFocus(EventArgs e)
        {
            ReDraw(this);
            base.OnGotFocus(e);
        }
        int i = 0;
        private void ReDraw(TextBox textBox)
        {
            Graphics g;
            g =Graphics.FromHwnd (textBox.Handle);
             IF (textBox.Text.Length <= 0 ) 
            { 
                the Font font = new new the Font ( the this .Font.FontFamily, the this .Font.Size, the this .Font.Style); // set the font , size, thickness 
                Brush sbrush = new new SolidBrush with (Color.Gray); // set the color 
                g.drawString ( " Please enter text ...... " , font, sbrush, . 1 , . 1 ); 
                g.Save (); 
                I = 0 ;
            } 
            The else 
            { 
                IF (I == 0 ) 
                { 
                    g.Clear (textBox.BackColor); 
                    the Font font = new new the Font ( the this .Font.FontFamily, the this .Font.Size, the this .Font.Style); // set the font, size, thickness 
                    Brush sbrush = new new SolidBrush with ( the this .ForeColor); // set the color 
                    g.drawString (the Text, font, sbrush, 0 , 0 ); 
                    g.Save (); 
                    I++;
                }
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/dary/p/11311409.html