The screenshot below shows an effect that some call cue banner, banner or watermark. When a TextBox does not have focus with the code we see text that when the TextBox is given focus the cue banner text goes away.
Points of interest, the cue banner text will only display when the control does not have focus and has no content. The attached solution was done in VS2013 but the code is valid back to at least VS2008. To use this code in a project add the class project to your solution, change the Framework in the class library to match the Framework used in your solution.
Update 7/24/2016, removed the VS2013 solution in favor of a VS2015 solution which includes extension methods rather than functions and included a C# versions also.
Last edited by kareninstructor; Jul 24th, 2016 at 10:58 AM.
dim firstname as string = "Please enter your first name"
string Code:
dim Lastname as string = "Please enter your Last Name"
txtfirstname.Text Code:
txtfirstname.Text = firstname.toString()
txtLastName.Text Code:
txtLastName.Text = Lastname.toString()
If we cannot , then I hope you wd explain why ? and thanks
Yea, you could always add the coded needed in each form that it would be used, but then if it ever changes you'd need to make the change everywhere else it's used.
So having it in a single file works quite nicely, making the change there would apply the change everywhere else (so to speak).
What kevininstructor could do with this further is make a new TextBox control altogether (inherit the System.Windows.Forms.TextBox control) and add this functionality to it directly, make the WaterMark text a property of the new control.
Originally Posted by kevininstructor
It was never intended for a ComboBox
A component of the ComboBox is an embedded TextBox (Windows.Forms.TextBox) so you could inherit the Windows.Forms.ComboBox control and add this functionality to the TextBox inside it.
--Edit:
Something like this would be great:
vb Code:
Imports System.Runtime.InteropServices
Imports System.Diagnostics
<DebuggerNonUserCode()> _
Public Class WatermarkTextBox
Inherits System.Windows.Forms.TextBox
''' <summary>
'''
''' </summary>
''' <param name="hWnd">Control Handle</param>
''' <param name="msg">SETCUEBANNER</param>
''' <param name="wParam"></param>
''' <param name="lParam">Watermark Text</param>
''' <returns>IntPtr</returns>
''' <remarks></remarks>
Private Declare Auto Function SendMessage Lib "user32.dll" (hWnd As IntPtr, msg As Integer,
wParam As Integer,
<MarshalAs(UnmanagedType.LPWStr)> lParam As String) As IntPtr
Private Const EM_SETCUEBANNER As Integer = &H1501
Private m_WaterMarkText As String
''' <summary>
''' Event that's raised when the Watermark Text is changed
''' </summary>
''' <remarks></remarks>
Public Event WaterMarkTextChanged As EventHandler
Public Sub New()
Me.WaterMarkText = String.Empty
End Sub
''' <summary>
''' Gets/Sets the watermark text to display in the TextBox when empty and focus is lost