Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文本框水印_C#_Visual Studio 2010 - Fatal编程技术网

C# 文本框水印

C# 文本框水印,c#,visual-studio-2010,C#,Visual Studio 2010,我的程序:只有一个文本框。我正在用C语言编写代码 我的目标:在文本框中显示文本/水印:“请输入您的姓名”。因此,当用户单击文本框时,默认文本/水印将被清除/删除,以便用户可以在文本框中输入自己的姓名 我的问题:我尝试了在线提供的各种代码,但似乎都不适合我。所以,我想我应该在这里问一个简单的代码。我在网上找到了一个代码,但似乎不起作用: using System; using System.Collections.Generic; using System.ComponentModel; usin

我的程序:只有一个文本框。我正在用C语言编写代码

我的目标:在文本框中显示文本/水印:“请输入您的姓名”。因此,当用户单击文本框时,默认文本/水印将被清除/删除,以便用户可以在文本框中输入自己的姓名

我的问题:我尝试了在线提供的各种代码,但似乎都不适合我。所以,我想我应该在这里问一个简单的代码。我在网上找到了一个代码,但似乎不起作用:

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指令或程序集引用?)


如果你对我的目标有任何其他的建议,我将非常感激。我在网上列举了很多例子,但都令人困惑/不起作用。提前感谢您的帮助。:)

刚刚试过这个。在一个新的Windows窗体项目中,它似乎工作得很好

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

文本框没有水印。你可以通过将文本变灰来创建自己的文本,如果他们在文本中添加了内容,则将其变为黑色。你可以始终将前景设置为WhiteMoke或其他内容,然后连接到GotFocus事件以清除/更改颜色。太棒了!切中要害!非常感谢,琼斯!:)看来你在重新发明轮子。为什么不使用
EM_SETCUEBANNER
我认为这个答案不适用于数据绑定。我知道这可能并不理想,但很明显,Smith比C#更新,一个简单的解决方案可能比深入研究数据绑定和导入DLLsI更好。我创建了一个小型GitHub存储库,其中显示了创建水印的所有方法。