C# 水印的e是stackoverflow右上角的搜索文本框。水印的缺点是,在填充水印时,您无法判断该值的含义。看看你的屏幕截图,我建议你使用标签控件,直接放在文本框的上方;TextBoxWatermarkExtensionMethod是一个嵌套类我做错了什么?

C# 水印的e是stackoverflow右上角的搜索文本框。水印的缺点是,在填充水印时,您无法判断该值的含义。看看你的屏幕截图,我建议你使用标签控件,直接放在文本框的上方;TextBoxWatermarkExtensionMethod是一个嵌套类我做错了什么?,c#,winforms,textbox,highlight,C#,Winforms,Textbox,Highlight,水印的e是stackoverflow右上角的搜索文本框。水印的缺点是,在填充水印时,您无法判断该值的含义。看看你的屏幕截图,我建议你使用标签控件,直接放在文本框的上方;TextBoxWatermarkExtensionMethod是一个嵌套类我做错了什么?另外,.SetWatermark没有列在自动完成上。我猜,从这个错误来看,您已经将Extensions类放在了另一个类中。你不能这样做。这一定是一流的。一旦你这样做了,并且它编译了,那么SetWatermark就会出现在IntelliSense


水印的e是stackoverflow右上角的搜索文本框。水印的缺点是,在填充水印时,您无法判断该值的含义。看看你的屏幕截图,我建议你使用标签控件,直接放在文本框的上方;TextBoxWatermarkExtensionMethod是一个嵌套类我做错了什么?另外,.SetWatermark没有列在自动完成上。我猜,从这个错误来看,您已经将Extensions类放在了另一个类中。你不能这样做。这一定是一流的。一旦你这样做了,并且它编译了,那么SetWatermark就会出现在IntelliSense中(单击解决方案资源管理器,然后右键单击您的程序,然后添加->类。用代码替换其中的所有内容。更改水印命名空间以匹配您的程序命名空间。双击每个文本框的属性窗口事件列表中的事件Enter()将允许您在TextBox1\u Enter()中写入代码行。)事件处理程序和扩展SelectAll()的功能而无需太多努力。因此,对每个文本框都是这样。请检查我的答案以了解实现水印的即时方法。易于实现和使用。
    private void grapplingText1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {
        grapplingText1.SelectionStart = 0;
        grapplingText1.SelectionLength = grapplingText1.Text.Length;
private TextBox1_Enter(object sender, EventArgs e) {    
    TextBoxTextHighlight(sender, null);
}

private TextBox2_Enter(object sender, EventArgs e) {
    TextBoxTextHighlight(sender, null);
}

private TextBox3_Enter(object sender, EventArgs e) {
    TextBoxTextHighlight(sender, null);
}

// And so forth...

private void TextBoxTextHighlight(object sender, EventArgs e) {
    (sender as TextBox).SelectAll();
}
public partial class CustomTextBox : TextBox {
    public CustomTextBox() 
        : base() {
        this.Enter += new EventHandler(Enter);
    }

    protected virtual Enter(object sender, EventArgs e) {
        this.SelectAll();
    }
}
private void MyTextBox_GotFocus(object sender, EventArgs e)
{
    MyTextBox.SelectionStart = 0;
    MyTextBox.SelectionLength = MyTextBox.Text.Length;
    MyTextBox.Select();
}
this.MyTextBox.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus);
this.MyTextBox2.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus);
this.MyTextBox3.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus);
this.MyTextBox4.GotFocus += new System.EventHandler(this.MyTextBox_GotFocus);
//...
#region
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

#endregion

namespace Watermark {
    public static class TextBoxWatermarkExtensionMethod {
        private const uint ECM_FIRST = 0x1500;
        private const uint EM_SETCUEBANNER = ECM_FIRST + 1;
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
        public static void SetWatermark(this TextBox textBox, string watermarkText) {
            SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermarkText);
        }
    }
}
   internal class WatermarkTextBox : TextBox {
    private const uint ECM_FIRST = 0x1500;
    private const uint EM_SETCUEBANNER = ECM_FIRST + 1;
    private string watermarkText;
    public string WatermarkText {
        get { return watermarkText; }
        set {
            watermarkText = value;
            SetWatermark(watermarkText);
        }
    }
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
    private void SetWatermark(string watermarkText) {
        SendMessage(Handle, EM_SETCUEBANNER, 0, watermarkText);
    }
}
textBoxYourWhatever.SetWatermark("Text that should display");