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

Ask A Question

Stats

  • Questions 343k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is called a "malicious cast" and you can prevent… May 14, 2026 at 5:32 am
  • Editorial Team
    Editorial Team added an answer This is what I ended up using: EditText SearchEditText =… May 14, 2026 at 5:32 am
  • Editorial Team
    Editorial Team added an answer 1) If processes have the same name then I think… May 14, 2026 at 5:32 am

Related Questions

I have a nested table arrangement for displaying data. I want the nested table
I've written a PHP script that handles file downloads, determining which file is being
Im having problems displaying records to my view when passing viewdata to a user
Is it possible to fine-tune directives to Google to such an extent that it
I am working on a website at the moment which is displaying a strange

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.