Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void Login_Load(object sender, EventArgs e)
{
	textBox1.Text = "Email";
	textBox1.ForeColor = Color.Gray;
	textBox2.Text = "Password";
	textBox2.ForeColor = Color.Gray;
	panel8.Visible = false;
	panel7.Visible = false;


	// textBox1.ForeColor = Color.Gray;

	//textBox2.ForeColor = Color.Gray;

}
private void textBox1_Click(object sender, EventArgs e)
{
	panel8.Visible = true;
	panel7.Visible = false;
	textBox1.Text = "";
}

private void textBox2_Click(object sender, EventArgs e)
{
	panel8.Visible=false;
	panel7.Visible = true;
	textBox2.Text = "";
}


What I have tried:

i don't know how to add watermark so i did this with click events but it is not satisfiable...is there any other way to do that?
Posted
Updated 18-Nov-18 3:13am
v3
Comments
Patrice T 31-Mar-16 8:50am    
What you want to do with watermark in a textbox ?
Patrice T 31-Mar-16 8:56am    
I found, I didn't know this name in context. :)
Member 10549697 31-Mar-16 11:25am    
its like normal textbox having tex as "type here..."

 
Share this answer
 
Comments
Member 10549697 31-Mar-16 11:36am    
Thank You sir! the third link worked...it was easy explanation..really thanks
Karthik_Mahalingam 31-Mar-16 11:38am    
welcome :)
Create 2 identical text boxes on a windows form. Name them "RealTextBox" (set tab index to 1) and "WatermarkTextBox" (set tab index to 0, set fore color to gray). Position WatermarkTextBox exactly on top of RealTextBox, so that the latter is not visible. Create a "TextChanged event" for each text box (see code below) and a string field "watermark" to hold the watermark text. Create a "Click event" for the WatermarkTextBox. When the text box is clicked by the user, this event makes sure that the watermark text and the caret remain the same.

When the form is loaded, the WatermarkTextBox shows the watermark text and the caret is blinking in front of it. When the first character is typed, it is copied to the RealTextBox, the WatermarkTextBox is set to "invisible" and the caret is positioned after the character in the RealTextBox where the typing can be continued.

If the text in the RealTextBox is removed, the form goes back to the initial state.

My solution in code:

C#
using System;
using System.Windows.Forms;

namespace WatermarkTextBox
{
    public partial class TestForm : Form
    {
        // The placeholder text:
        string watermark = "Type here...";

        public TestForm()
        {
            InitializeComponent();            
            WatermarkTextBox.Text = watermark;
            WatermarkTextBox.Select(0, 0);
        }

        // The 2 TextChanged events:
        private void WatermarkTextBox_TextChanged(object sender, EventArgs e)
        {
            RealTextBox.Text = WatermarkTextBox.Text.Substring(0, 1);
            WatermarkTextBox.Visible = false;
            RealTextBox.Select(1, 0);
        }

        private void RealTextBox_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(RealTextBox.Text))
            {
                WatermarkTextBox.Text = watermark;
                WatermarkTextBox.Visible = true;
                WatermarkTextBox.Focus();
                WatermarkTextBox.Select(0, 0);
            }
        }

        private void WatermarkTextBox_Click(object sender, EventArgs e)
        {
            WatermarkTextBox.Text = watermark;
            WatermarkTextBox.Select(0, 0);
        }
    }
}
 
Share this answer
 
v2

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