Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello friends,

i want to develop an application which have twitter like textboxes and having default text which disappears when we enter in the textbox.

following is an image of it-
http://www.freeimagehosting.net/e6f8a[^]
Posted
Updated 13-Oct-11 4:09am
v2

1 solution

You can do something like this, create a new form with 2 text boxes on it, this is just a quick demo, you would have to play with the events etc. to get it do to what you want. You would also be better to create a new class that inherits from textbox and then use it anywhere you want the functionality;

Here text box 1 has some default text entered, on a keypress it clear to allow user entry. If the user moves off the control leaving it empty, it repopulates the text field with the default string, likewise if the user enters the form, and it is already been modified the cursors is placed at the end of the users previous entry (you could of course select all text or whatever)

C#
public partial class Form1 : Form
    {

        private String defaultText = "Enter Some Text";

        public Form1()
        {
            InitializeComponent();

            textBox1.Text = defaultText;
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(defaultText))
            {
                textBox1.SelectionStart = 0;
            }
            else
            {
                textBox1.SelectionStart = textBox1.Text.Length;
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text.Length <= 0)
            {
                textBox1.Text = defaultText;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox1.Text.Equals(defaultText))
            {
                textBox1.Text= String.Empty;
            }
        }
    }
 
Share this answer
 
Comments
Jαved 14-Oct-11 3:13am    
Thanks DaveAuld.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900