如何使用户进入文本框时,文本消失?C#

如何使用户进入文本框时,文本消失?C#,c#,winforms,textbox,C#,Winforms,Textbox,Visual C#.NET: 因此,我尝试使用该代码使其在(Windows窗体)文本框中显示文本,然后当文本框获得焦点时(当用户在其中单击时),文本消失。这样,它看起来就像一个在文本框中有标签的表单 那么,为什么代码不起作用,或者有更好的方法吗?这里有一个关于如何在文本框中添加水印的教程 使用textBox1.GotFocus事件 textBox1.GotFocus += textBox1_GotFocus;//at the designer, constructor or form load.

Visual C#.NET:

因此,我尝试使用该代码使其在(Windows窗体)文本框中显示文本,然后当文本框获得焦点时(当用户在其中单击时),文本消失。这样,它看起来就像一个在文本框中有标签的表单


那么,为什么代码不起作用,或者有更好的方法吗?

这里有一个关于如何在文本框中添加水印的教程


使用
textBox1.GotFocus
事件

textBox1.GotFocus += textBox1_GotFocus;//at the designer, constructor or form load...

private void textBox1_GotFocus(object sender, EventArgs e)
{
    textBox1.Clear();//clear the text.
}
//更新:


如果您已经这样做了,那么问题一定在其他地方,此代码应该运行时没有任何问题。

它不起作用,因为默认情况下文本框不会触发回发,如果没有回发,服务器端事件不会处理


您最好的选择是使用客户端javascript来处理此功能,因为在每个文本框事件上往返到服务器对用户来说既浪费又烦人。

+1到@RexM的指针指向最简单的,也可能是最好的答案

有一种更为复杂的方法,它松散地基于。它允许自定义显示文本的颜色,但可能存在其他一些问题

我真的建议使用RexM的答案。无论如何,以下是另一种方法的代码供参考:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MyControls
{
    public class HintedTextBox : TextBox
    {
        public HintedTextBox() : base() { ResetHintColor(); }

        [Description("The color of the hint text to display"),
        Category("Appearance")]
        public Color HintColor { get; set; }
        // Default value handling for HintColor
        private Color DefaultHintColor { get { return Color.LightGray; } }
        public void ResetHintColor() { HintColor = Color.LightGray; }
        public bool ShouldSerializeHintColor() { return !HintColor.Equals(DefaultHintColor); }

        [Description("The textual hint to display in the textbox"),
        Category("Behavior"),
        Localizable(true)]
        public string HintText
        {
            get { return m_hintText; }
            set
            {
                if (m_hintText != value)
                {
                    m_hintText = value;
                    UpdateHintTextState(true);
                }
            }
        }
        private string m_hintText = "";

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            HasFocus = true;
            UpdateHintTextState();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            HasFocus = false;
            UpdateHintTextState();
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            UpdateHintTextState();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 15) // WM_PAINT
            {
                PaintHintText();
            }
        }

        private bool DisplayHintText { get; set; }

        private bool HasFocus { get; set; }

        private void PaintHintText()
        {
            if (DisplayHintText)
            {
                using (Graphics g = Graphics.FromHwnd(this.Handle))
                using (SolidBrush b = new SolidBrush(HintColor))
                {
                    StringFormat sf = new StringFormat();
                    switch (this.TextAlign)
                    {
                        case HorizontalAlignment.Center:
                            sf.Alignment = StringAlignment.Center;
                            break;
                        case HorizontalAlignment.Right:
                            sf.Alignment = StringAlignment.Far;
                            break;
                        default:
                            sf.Alignment = StringAlignment.Near;
                            break;
                    }
                    g.DrawString(HintText, Font, b, ClientRectangle, sf);
                }
            }
        }

        private void UpdateHintTextState() { UpdateHintTextState(false); }
        private void UpdateHintTextState(bool forceInvalidate)
        {
            bool prevState = DisplayHintText;

            if (HintText.Length == 0)
                DisplayHintText = false;
            else if (Text.Length != 0)
                DisplayHintText = false;
            else
                DisplayHintText = !HasFocus;

            if (DisplayHintText != prevState || forceInvalidate)
                Invalidate();
        }
    }
}

这是asp.net吗?如果是这样的话,你可以用javascript来做。什么是“不工作”?我看不出代码有任何问题,前提是文本框在开始时用“电子邮件地址”进行了初始化,如果你不介意人们在单击/tab返回后输入的内容被吹走。它应该可以工作,我自己测试过它。。。你一定忘了分配事件处理程序之类的东西。如果我是你,我也会改变颜色。例如,如果默认文本为黑白相间,我将以浅灰色而不是黑色显示您的自定义帮助文本。我相信事件名称是
GotFocus
,而不是
GetFocus
。此解决方案将不起作用,因为他使用的是WinForms,而不是ASP.NETno:@icemanind:No,当然,这段代码可以在windows窗体上运行,我自己测试过。您可以测试它
public Form1(){InitializeComponent();textBox1.GotFocus+=(发件人,e)=>{textBox1.Clear();};textBox1.LostFocus+=(发件人,e)=>{if(textBox1.Text==string.Empty)textBox1.Text=“电子邮件地址”};
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MyControls
{
    public class HintedTextBox : TextBox
    {
        public HintedTextBox() : base() { ResetHintColor(); }

        [Description("The color of the hint text to display"),
        Category("Appearance")]
        public Color HintColor { get; set; }
        // Default value handling for HintColor
        private Color DefaultHintColor { get { return Color.LightGray; } }
        public void ResetHintColor() { HintColor = Color.LightGray; }
        public bool ShouldSerializeHintColor() { return !HintColor.Equals(DefaultHintColor); }

        [Description("The textual hint to display in the textbox"),
        Category("Behavior"),
        Localizable(true)]
        public string HintText
        {
            get { return m_hintText; }
            set
            {
                if (m_hintText != value)
                {
                    m_hintText = value;
                    UpdateHintTextState(true);
                }
            }
        }
        private string m_hintText = "";

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            HasFocus = true;
            UpdateHintTextState();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            HasFocus = false;
            UpdateHintTextState();
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            UpdateHintTextState();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 15) // WM_PAINT
            {
                PaintHintText();
            }
        }

        private bool DisplayHintText { get; set; }

        private bool HasFocus { get; set; }

        private void PaintHintText()
        {
            if (DisplayHintText)
            {
                using (Graphics g = Graphics.FromHwnd(this.Handle))
                using (SolidBrush b = new SolidBrush(HintColor))
                {
                    StringFormat sf = new StringFormat();
                    switch (this.TextAlign)
                    {
                        case HorizontalAlignment.Center:
                            sf.Alignment = StringAlignment.Center;
                            break;
                        case HorizontalAlignment.Right:
                            sf.Alignment = StringAlignment.Far;
                            break;
                        default:
                            sf.Alignment = StringAlignment.Near;
                            break;
                    }
                    g.DrawString(HintText, Font, b, ClientRectangle, sf);
                }
            }
        }

        private void UpdateHintTextState() { UpdateHintTextState(false); }
        private void UpdateHintTextState(bool forceInvalidate)
        {
            bool prevState = DisplayHintText;

            if (HintText.Length == 0)
                DisplayHintText = false;
            else if (Text.Length != 0)
                DisplayHintText = false;
            else
                DisplayHintText = !HasFocus;

            if (DisplayHintText != prevState || forceInvalidate)
                Invalidate();
        }
    }
}