Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 675031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:46:22+00:00 2026-05-14T00:46:22+00:00

Displaying Type here to … until the user enters text into a TextBox is

  • 0

Displaying “Type here to …” until the user enters text into a TextBox is a well-known usability feature nowadays. How would one implement this feature in C#?

My idea is to override OnTextChanged, but the logic to handle the changes of text from and to “Type here” is a bit tricky…

Displaying “Type here” on initialization and removing it on first input is easy, but I want to display the message every time the entered text becomes empty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:46:22+00:00Added an answer on May 14, 2026 at 12:46 am

    What you’re looking for is a TextBox with a “watermark“.

    There’s a sample implementation for C# here, all credits to Wael Alghool.

    The relevant part of his code is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace wmgCMS
    {
        class WaterMarkTextBox : TextBox
        {
            private Font oldFont = null;
            private Boolean waterMarkTextEnabled = false;
    
            #region Attributes 
                private Color _waterMarkColor = Color.Gray;
                public Color WaterMarkColor
                {
                    get { return _waterMarkColor; }
                    set { _waterMarkColor = value; Invalidate();/*thanks to Bernhard Elbl
                                                                  for Invalidate()*/ }
                }
    
                private string _waterMarkText = "Water Mark";
                public string WaterMarkText
                {
                    get { return _waterMarkText; }
                    set { _waterMarkText = value; Invalidate(); }
                }
            #endregion
    
            //Default constructor
            public WaterMarkTextBox()
            {
                JoinEvents(true);
            }
    
            //Override OnCreateControl ... thanks to  "lpgray .. codeproject guy"
            protected override void OnCreateControl() 
            { 
                base.OnCreateControl();
                WaterMark_Toggel(null, null); 
            }
    
            //Override OnPaint
            protected override void OnPaint(PaintEventArgs args)
            {
                // Use the same font that was defined in base class
                System.Drawing.Font drawFont = new System.Drawing.Font(Font.FontFamily,
                    Font.Size, Font.Style, Font.Unit);
                //Create new brush with gray color or 
                SolidBrush drawBrush = new SolidBrush(WaterMarkColor);//use Water mark color
                //Draw Text or WaterMark
                args.Graphics.DrawString((waterMarkTextEnabled ? WaterMarkText : Text),
                    drawFont, drawBrush, new PointF(0.0F, 0.0F));
                base.OnPaint(args);
            }
    
            private void JoinEvents(Boolean join)
            {
                if (join)
                {
                    this.TextChanged += new System.EventHandler(this.WaterMark_Toggel);
                    this.LostFocus += new System.EventHandler(this.WaterMark_Toggel);
                    this.FontChanged += new System.EventHandler(this.WaterMark_FontChanged);
                    //No one of the above events will start immeddiatlly 
                    //TextBox control still in constructing, so,
                    //Font object (for example) couldn't be catched from within
                    //WaterMark_Toggle
                    //So, call WaterMark_Toggel through OnCreateControl after TextBox
                    //is totally created
                    //No doupt, it will be only one time call
    
                    //Old solution uses Timer.Tick event to check Create property
                }
            }
    
            private void WaterMark_Toggel(object sender, EventArgs args )
            {
                if (this.Text.Length <= 0)
                    EnableWaterMark();
                else
                    DisbaleWaterMark();
            }
    
            private void EnableWaterMark()
            {
                //Save current font until returning the UserPaint style to false (NOTE:
                //It is a try and error advice)
                oldFont = new System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style,
                   Font.Unit);
                //Enable OnPaint event handler
                this.SetStyle(ControlStyles.UserPaint, true);
                this.waterMarkTextEnabled = true;
                //Triger OnPaint immediatly
                Refresh();
            }
    
            private void DisbaleWaterMark()
            {
                //Disbale OnPaint event handler
                this.waterMarkTextEnabled = false;
                this.SetStyle(ControlStyles.UserPaint, false);
                //Return back oldFont if existed
                if(oldFont != null)
                    this.Font = new System.Drawing.Font(oldFont.FontFamily, oldFont.Size,
                        oldFont.Style, oldFont.Unit);
            }
    
            private void WaterMark_FontChanged(object sender, EventArgs args)
            {
                if (waterMarkTextEnabled)
                {
                    oldFont = new System.Drawing.Font(Font.FontFamily,Font.Size,Font.Style,
                        Font.Unit);
                    Refresh();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm displaying prices on a DataGridView (on a WinForms application developped in C# on
I have a partial view (ascx) for displaying the top x articles of a
I have 2 ways of displaying ListView data. I have a class for each
Just to give you a quick overview, just trying to create somewhat of a
i have the following map of the world: <!-- copyright (c) 2009 Google inc.
example if i have follow table company one ( cid = 1 ) following
MVVM pattern is implemented in my Silverlight4 application. Originally, I worked with ObservableCollection of
hi i have a function that worked fine using jQuery 1.3.2 but now i
I'm almost positive that there is a stupid reason this is not working, but
I am aware that this is somewhat a re-post, but I feel like re-posting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.