I need to have a watermark on both of my form using this

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

I tried to use this to both form but I got an error saying

"Ambiguity between namespace.TextBoxWatermarkExtensionMethod.ECM_FIRST and namespace.TextBoxWatermarkExtensionMethod.ECM_FIRST."

I think this class must not duplicate. But how to call it between the two form.

Recommended Answers

All 5 Replies

It's a static method, you just need it in one file and it will be callable anywhere in your code.

How to call it? I put it inside a class. How to call the class?

It's an extension method, so it's available to any textbox.

What do you mean?

You call it like you would any class method of the class it extends. Since it is extending TextBox you'd just use myTextBox.SetWatermark("My text");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.