This is an extension module that adds a method, to both TextBox and ComboBox Controls, that activates the Cue Banner1 internal functionality. For the TextBox, in the two available flavors: the Cue Banner is visible: a) until the control gets focus, b) after the first char is entered.
The ComboBox control removes the Cue Banner as soon as it receives focus or the DropDown
is opened.
The Win32 function SendMessage is used modify the behavior of the Edit
control - the TextBox control and the Edit
child control of a ComboBox, sending the EM_SETCUEBANNER
and CB_SETCUEBANNER
messages, respectively.
1 Sets the textual cue, or tip, that is displayed by
the edit control to prompt the user for information.
Since these are extension methods, each method extends the controls functionality and can be called directly:
' The Cue Banner is visible until the control gets focus
TextBox1.SetCueBanner("MM/DD/YYYY", False)
' The Cue Banner is visible until a character is entered
TextBox1.SetCueBanner("User Name...", True)
ComboBox1.SetCueBanner("Select an item...")
Add a Module to the Project and substitute the code generated by the template with this:
VB.Net version:
Public Module ModExtentions
Private Const EM_SETCUEBANNER As Integer = &H1501
Private Const CB_SETCUEBANNER As Integer = &H1703
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
End Function
<Extension()>
Public Sub SetCueBanner(tbox As TextBox, ByVal text As String, ByVal showOnFocus As Boolean)
If tbox.IsHandleCreated Then SendMessage(tbox.Handle, EM_SETCUEBANNER, If(showOnFocus, 1, 0), text)
End Sub
<Extension()>
Public Sub SetCueBanner(cbo As ComboBox, ByVal text As String)
If cbo.IsHandleCreated Then SendMessage(cbo.Handle, CB_SETCUEBANNER, 0, text)
End Sub
End Module
C# Version:
using System.Runtime.InteropServices;
using System.Windows.Forms;
public static class ExtControls
{
private const int EM_SETCUEBANNER = 0x1501;
private const int CB_SETCUEBANNER = 0x1703;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);
public static void SetCueBanner(this TextBox tbox, string text, bool showOnFocus) {
if (tbox.IsHandleCreated) {
SendMessage(tbox.Handle, EM_SETCUEBANNER, showOnFocus ? 1 : 0, text);
}
}
public static void SetCueBanner(this ComboBox cbo, string text) {
if (cbo.IsHandleCreated) {
SendMessage(cbo.Handle, CB_SETCUEBANNER, 0, text);
}
}
}