Search code examples
winformsc#-4.0textboxonclick

Make text disappear in textbox on click event, and show the same text again when no focus on it?


I have 6 Textbox in a registration form with some preset text. When I click in the Textbox for Name then preset text Enter your full name should disappear...If I then click on the Textbox for Email without writing anything in Name Textbox, then Enter your full name should appear again. This event should happen for all my Textbox, but their should be different text in each Textbox...Not Enter your full name in all of them. Can someone please help me with this?

The code I have right now makes it possible to clear the Textbox as I click on them, using GotFocus event.

 private void textBox1_GotFocus(Object sender, EventArgs e)
 {
     textBox1.Clear();
 }

In the textboxes, I have preset text....I want that exact preset text for each textbox to come back whenever I am clicking outside that Textbox. I've heard something about a "Placeholder" ?

Here's how it looks now with an extra constructor. I can't figure out what I'm doing wrong?

 public partial class CustomTextbox : TextBox
 {
    private const string _text = @"Enter your full name";
    private bool _isEmpty = true;

    public CustomTextbox()
    {

        base.ForeColor = SystemColors.GrayText;
        Text = _text;
        Leave += LeaveTextBox;
        Enter += EnterTextBox;
        TextChanged += TextChangedTextBox;
    }
    public CustomTextbox(string tempText)
    {

        base.ForeColor = SystemColors.GrayText;
        Text = tempText;
        Leave += LeaveTextBox;
        Enter += EnterTextBox;
        TextChanged += TextChangedTextBox;
    }

Solution

  • Try creating a custom Class inheriting from Textbox. To achieve this, create a new Usercontrol. Delete the base class name and place Textbox there. Delete anything in designer if giving compilation error. Build your project. You should see a new control in toolbox. Use it and you are good to go. Here is the code for Usercontol.cs

    public partial class CustomTextbox : TextBox
    {
        private const string _text = @"Enter your full name";
        private bool _isEmpty = true;
    
        public CustomTextbox()
        {
            InitializeComponent();
            base.ForeColor = SystemColors.GrayText;
            Text = _text;
            Leave += LeaveTextBox;
            Enter += EnterTextBox;
            TextChanged += TextChangedTextBox;
        }
    
        private void TextChangedTextBox(object sender, EventArgs e)
        {
            _isEmpty = string.IsNullOrEmpty(Text);
        }
    
        public override sealed string Text
        {
            set
            {
                base.Text = value;
            }
        }
    
        private void LeaveTextBox(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Text))
            {
                Text = _text;
                _isEmpty = true;
                base.ForeColor = SystemColors.GrayText;
            }
            else
            {
                _isEmpty = false;
            }
        }
    
        private void EnterTextBox(object sender, EventArgs e)
        {
            if (_isEmpty)
                Text = string.Empty;
    
            base.ForeColor = SystemColors.ControlText;
        }
    }
    

    Let me know if you need any further information. As a not, you can also make _text as a public property and use it to set the desired text property.

    Hope it helps.