-1

// Add Reference using System.Runtime.InteropServices;

    private const int EM_SETCUEBANNER = 0x1501;
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
    private void Form1_Load(object sender, EventArgs e)
    {
        SendMessage(textBox_1.Handle, EM_SETCUEBANNER, 0, "long line");
        SendMessage(textBox_2.Handle, EM_SETCUEBANNER, 0, "short line");
    }
  • 1
    Does this answer your question? [How to change the font color in the textbox in C#?](https://stackoverflow.com/questions/2813522/how-to-change-the-font-color-in-the-textbox-in-c) – Trevor Oct 28 '21 at 18:29
  • @zaggler ; No ThankS. How to change the font color in the textbox in C#? I search placeholder watermark.. – sebahattin küçük Oct 28 '21 at 22:23

1 Answers1

0

If you want TextBox with placeholder and be able customize placeholder color - you shouldn't use SendMessage and EM_SETCUEBANNER. They always make placeholder with default gray color.

To get desired result, you should create your own HintTextBox (or anyway you want to name it). Example below taken from @Reza Aghaei answer (here) and edited a bit.

Code:

HintTextBox.cs

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

namespace MyWinFormsApp
{
    public class HintTextBox : TextBox
    {
        public HintTextBox()
        {
            Width = 150; // Some default width
            Multiline = true; // Multiline by default    
        }

        private string hint = "Input some text..."; // Default placeholder text
        private Color hintColor = Color.Red; // Default placeholder color

        // Set another Hint text through this property
        public string Hint
        {
            get => hint;
            set
            {
                hint = value;
                Invalidate();
            }
        }

        // Set placeholder color you wish through this property
        public Color HintColor
        {
            get => hintColor;
            set
            {
                hintColor = value;
                Invalidate();
            }
        }

        // Drawing placeholder on WM_PAINT message
        protected override void WndProc(ref Message message)
        {
            base.WndProc(ref message);

            if (message.Msg == 15) // WM_PAINT or 0xF
            {
                if (!Focused &&
                    string.IsNullOrEmpty(Text) &&
                    !string.IsNullOrEmpty(Hint))
                {
                    using (Graphics g = CreateGraphics())
                    {
                        TextRenderer.DrawText(g, Hint, Font,
                                              ClientRectangle, hintColor, BackColor,
                                              TextFormatFlags.Top | TextFormatFlags.Left);
                    }
                }
            }
        }
    }
}

Usage example:

private void Form1_Load(object sender, EventArgs e)
{
    // Create two HintTextBoxes with some placeholder and color
    HintTextBox hintTextBox1 = new HintTextBox();
    // Customize placeholder color through HintColor property
    hintTextBox1.HintColor = Color.Magenta;
    // Set placeholder with a text you wish
    hintTextBox1.Hint = "My awesome hint";

    // Some another HintTextBox
    HintTextBox hintTextBox2 = new HintTextBox();
    hintTextBox2.HintColor = Color.SeaGreen;
    hintTextBox2.Hint = "My another hinted TextBox...";

    // For example, I put both HintTextBoxes to FlowLayoutPanel
    FlowLayoutPanel flowLayoutPanel = new FlowLayoutPanel();
    flowLayoutPanel.Dock = DockStyle.Fill;
    flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
    flowLayoutPanel.Controls.Add(hintTextBox1);
    flowLayoutPanel.Controls.Add(hintTextBox2);

    // Add FlowLayoutPanel with 2 HintTextBoxes on a Form
    this.Controls.Add(flowLayoutPanel);
}

Result:

enter image description here

Auditive
  • 1,607
  • 1
  • 7
  • 13