水印文本框
我的计划:只有一个文本框。我使用C#语言编写代码
My Program: Has one textbox only. I am writing code using C# Language.
我的目的::要显示文本的文本/水印:'请输入你的名字。所以,当用户点击文本框,默认文本/水印得到明显/删除,以便用户可以在文本框中输入自己的名字
My Aim: To display text/watermark in textbox: 'Please enter your name'. So, when user clicks on the textbox, the default text/watermark gets clear/deleted so that user can enter his name in the textbox.
我的问题: 我试过在网上提供各种代码,但没有人似乎为我工作。所以,我想我应该在这里问了一个简单的代码。我发现一个代码在网上,但似乎并没有工作:
My problem: I tried various codes that are available online but none of them seem to work for me. So, I thought I should ask here for a simple code. I have found a code online but that doesn't seem to work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetWatermark("Enter a text here...");
}
private void SetWatermark(string watermark)
{
textBox1.Watermark = watermark;
}
}
}
错误:
错误1'System.Windows.Forms.TextBox'不包含定义'水印',没有扩展方法'水印接受式的第一个参数System.Windows.Forms.TextBox'可以找到(是否缺少using指令或程序集引用?)
Error 1 'System.Windows.Forms.TextBox' does not contain a definition for 'Watermark' and no extension method 'Watermark' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)
请,如果你有什么我的目标为任何其他建议,我会很感激它。我累了很多例子在线,但一切都是混乱/不起作用。感谢您的帮助提前。 :)
Please, if you have any other suggestions for what I am aiming for, I would really appreciate it. I tired many examples online but all are confusing/don't work. Thanks for your help in advance. :)
刚刚试过了这一点。它似乎是在一个新的Windows窗体项目做工精细。
just tried this out. It seems to work fine in a new Windows Forms project.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.ForeColor = SystemColors.GrayText;
textBox1.Text = "Please Enter Your Name";
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "Please Enter Your Name";
textBox1.ForeColor = SystemColors.GrayText;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
if (textBox1.Text == "Please Enter Your Name")
{
textBox1.Text = "";
textBox1.ForeColor = SystemColors.WindowText;
}
}
}