如何在C#窗体中添加水印到文本框? [英] How to add watermark to textbox in C# windows form?

查看:239
本文介绍了如何在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 = "";
}





我的尝试:



i不知道如何添加水印所以我用点击事件做了这个但是它不能令人满意......有没有其他方法可以做到这一点?



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?

推荐答案

查看这些现成的答案



CodeProject:用于桌面应用程序的WaterMark TextBox / a> [ ^ ]

c# - 文本框水印 - 堆栈溢出 [ ^ ]

Windows中的WaterMark TextBox应用 [ ^ ]
Check out these ready-made answers

CodeProject:WaterMark TextBox For Desktop Applications /a>[^]
c# - Watermark for Textbox - Stack Overflow[^]
WaterMark TextBox In Windows Applications [^]


在Windows窗体上创建2个相同的文本框。将它们命名为RealTextBox(将选项卡索引设置为1)和WatermarkTextBox(将选项卡索引设置为0,将前颜色设置为灰色)。将WatermarkTextBox正好放在RealTextBox的顶部,以便后者不可见。为每个文本框创建一个TextChanged事件(参见下面的代码)和一个字符串字段watermark来保存水印文本。为WatermarkTextBox创建Click事件。当用户单击文本框时,此事件确保水印文本和插入符保持不变。



加载表单时,WatermarkTextBox显示水印文本和插入符号在它前面闪烁。当键入第一个字符时,将其复制到RealTextBox,将WatermarkTextBox设置为不可见,并将插入符号放在RealTextBox中可以继续输入的字符之后。



如果删除RealTextBox中的文本,表单将返回初始状态。



我的代码解决方案:



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:

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


这篇关于如何在C#窗体中添加水印到文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆