Implementing a Cue Banner to a textbox in windows xp with custom color
-
Hello, I am planning to implement cue banner for Text boxes in .net. I want this cue text to work till user enters some text in the box, that is cue text should not disappear if user clicks on text box. I got this example, but I have two problems with this.
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;public class Form1
{private void Form1\_Load(System.Object sender, System.EventArgs e) { CueBannerText.SetCueText(TextBox1, "Enter Name here"); } public Form1() { Load += Form1\_Load; }
}
public static class CueBannerText
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]
string lParam)
{
}
[DllImport("user32", EntryPoint = "FindWindowExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);private const int EM\_SETCUEBANNER = 0x1501; public static void SetCueText(Control cntrl, string text) { if (cntrl is ComboBox) { IntPtr Edit\_hWnd = FindWindowEx(cntrl.Handle, IntPtr.Zero, "Edit", null); if (!(Edit\_hWnd == IntPtr.Zero)) { SendMessage(Edit\_hWnd, EM\_SETCUEBANNER, 1, text); } } else if (cntrl is TextBox) { SendMessage(cntrl.Handle, EM\_SETCUEBANNER, 1, text); } }
}
The above code is working good in Windows 7 and above. But in windows xp it is not working properly. In windows xp the cue text disappears when we click on text box. Secondly I want to change the color of cuetext (currently it is in grey). Is it possible to achieve my requirements using current code? Or Is there any other suitable methods for me?
-
Hello, I am planning to implement cue banner for Text boxes in .net. I want this cue text to work till user enters some text in the box, that is cue text should not disappear if user clicks on text box. I got this example, but I have two problems with this.
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;public class Form1
{private void Form1\_Load(System.Object sender, System.EventArgs e) { CueBannerText.SetCueText(TextBox1, "Enter Name here"); } public Form1() { Load += Form1\_Load; }
}
public static class CueBannerText
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]
string lParam)
{
}
[DllImport("user32", EntryPoint = "FindWindowExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);private const int EM\_SETCUEBANNER = 0x1501; public static void SetCueText(Control cntrl, string text) { if (cntrl is ComboBox) { IntPtr Edit\_hWnd = FindWindowEx(cntrl.Handle, IntPtr.Zero, "Edit", null); if (!(Edit\_hWnd == IntPtr.Zero)) { SendMessage(Edit\_hWnd, EM\_SETCUEBANNER, 1, text); } } else if (cntrl is TextBox) { SendMessage(cntrl.Handle, EM\_SETCUEBANNER, 1, text); } }
}
The above code is working good in Windows 7 and above. But in windows xp it is not working properly. In windows xp the cue text disappears when we click on text box. Secondly I want to change the color of cuetext (currently it is in grey). Is it possible to achieve my requirements using current code? Or Is there any other suitable methods for me?
Windows XP is now well out of support so you are on your own when trying to implement new features on it. This feature is only officially supported in Vista and beyond, as described at EM_SETCUEBANNER message (Windows)[^]. If you wish to change colours and other behaviours then you will need to write your own implementation, or subclass the control.