1

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;
    }

2 Answers2

2

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.

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • +1 There would be some issues when setting the Textbox's Text property programmatically, but overall this looks like a decent solution. – JDB Feb 19 '13 at 15:06
  • hmmm...good point. Made some changes to reflect your issue. Hope it should work now – Sandy Feb 19 '13 at 15:09
  • @rapsalands - This works great! Thanks! But if I would like to have multipel textboxes with the same effect, but different text in each...how do I solve that? – Björn Svensson Feb 19 '13 at 17:14
  • Read the note in the end of answer. Still I will repeat myself....make _text as public property. Pass a text to set the preset text for the control. Or u can simply pass this text property through constructor and you are done. Hope it helps. – Sandy Feb 19 '13 at 18:35
  • I have now tried both pass the text through constructor and make _text as public property. None of them give me the result I want. If I go by the constructor, the text disappears but when losing focus "Enter your full name" comes back? If I try make _text as public property it doesn't work at all. Do you have any other ideas to make it work? – Björn Svensson Feb 20 '13 at 10:17
0

I solved my problem! Thanks to @rapsalands for providing the code. I have now modified it for my needs.

 public partial class UserControl1 : UserControl
 {
    public UserControl1()
    {
        InitializeComponent();
    }
 }

public partial class CustomTextbox : TextBox
{

    private string defaultText;

    public string DefaultText
    {
        get { return defaultText; }
        set { defaultText = value; }
    }


    private bool _isEmpty = true;


    public CustomTextbox(string myText)
    {

        base.ForeColor = SystemColors.GrayText;
        this.Text = myText;
        this.DefaultText = myText;
        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 = defaultText;
            _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;
    }
}

}