Results 1 to 6 of 6

Thread: Cue banner text

  1. #1

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Cue banner text

    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.

    Name:  13.jpg
Views: 1006
Size:  33.9 KB
    Attached Files Attached Files
    Last edited by kareninstructor; Jul 24th, 2016 at 10:58 AM.

  2. #2
    Junior Member Alaa Ben Fatma's Avatar
    Join Date
    Dec 2014
    Location
    Tunisia
    Posts
    15

    Re: Cue banner text

    I Guess That We Can Do It using these Two Codes :
    string Code:
    1. dim firstname as string = "Please enter your first name"
    string Code:
    1. dim Lastname as string = "Please enter your Last Name"
    txtfirstname.Text Code:
    1. txtfirstname.Text = firstname.toString()
    txtLastName.Text Code:
    1. txtLastName.Text = Lastname.toString()
    If we cannot , then I hope you wd explain why ? and thanks

  3. #3

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Cue banner text

    Quote Originally Posted by Alaa Ben Fatma View Post
    I Guess That We Can Do It using these Two Codes :
    string Code:
    1. dim firstname as string = "Please enter your first name"
    string Code:
    1. dim Lastname as string = "Please enter your Last Name"
    txtfirstname.Text Code:
    1. txtfirstname.Text = firstname.toString()
    txtLastName.Text Code:
    1. txtLastName.Text = Lastname.toString()
    If we cannot , then I hope you wd explain why ? and thanks
    I invite you to try run the project then if there is still a question by all means ask again.

  4. #4
    Member
    Join Date
    Aug 2004
    Location
    Sao Paulo / Brazil
    Posts
    38

    Re: Cue banner text

    Nice code. Thanks for the submission.
    Im sorry it doesn't work in a combobox when its a DropDownList.
    --------------------------------------
    All your base are belong to us.

  5. #5

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Cue banner text

    Quote Originally Posted by cbuosi View Post
    Nice code. Thanks for the submission.
    Im sorry it doesn't work in a combobox when its a DropDownList.
    It was never intended for a ComboBox

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Cue banner text

    Quote Originally Posted by Alaa Ben Fatma View Post
    I Guess That We Can Do It using these Two Codes :
    string Code:
    1. dim firstname as string = "Please enter your first name"
    string Code:
    1. dim Lastname as string = "Please enter your Last Name"
    txtfirstname.Text Code:
    1. txtfirstname.Text = firstname.toString()
    txtLastName.Text Code:
    1. 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.

    Quote Originally Posted by kevininstructor View Post
    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:
    1. Imports System.Runtime.InteropServices
    2. Imports System.Diagnostics
    3.  
    4. <DebuggerNonUserCode()> _
    5. Public Class WatermarkTextBox
    6.     Inherits System.Windows.Forms.TextBox
    7.  
    8.     ''' <summary>
    9.     '''
    10.     ''' </summary>
    11.     ''' <param name="hWnd">Control Handle</param>
    12.     ''' <param name="msg">SETCUEBANNER</param>
    13.     ''' <param name="wParam"></param>
    14.     ''' <param name="lParam">Watermark Text</param>
    15.     ''' <returns>IntPtr</returns>
    16.     ''' <remarks></remarks>
    17.     Private Declare Auto Function SendMessage Lib "user32.dll" (hWnd As IntPtr, msg As Integer,
    18.                                                                 wParam As Integer,
    19.                                                                 <MarshalAs(UnmanagedType.LPWStr)> lParam As String) As IntPtr
    20.  
    21.     Private Const EM_SETCUEBANNER As Integer = &H1501
    22.  
    23.     Private m_WaterMarkText As String
    24.  
    25.     ''' <summary>
    26.     ''' Event that's raised when the Watermark Text is changed
    27.     ''' </summary>
    28.     ''' <remarks></remarks>
    29.     Public Event WaterMarkTextChanged As EventHandler
    30.  
    31.     Public Sub New()
    32.         Me.WaterMarkText = String.Empty
    33.     End Sub
    34.  
    35.     ''' <summary>
    36.     ''' Gets/Sets the watermark text to display in the TextBox when empty and focus is lost
    37.     ''' </summary>
    38.     ''' <value></value>
    39.     ''' <returns></returns>
    40.     ''' <remarks></remarks>
    41.     Public Property WaterMarkText As String
    42.         Get
    43.             Return m_WaterMarkText
    44.         End Get
    45.         Set(value As String)
    46.             If m_WaterMarkText <> value.Trim Then
    47.  
    48.                 'Store new value
    49.                 m_WaterMarkText = value.Trim
    50.  
    51.                 'Set the CueText/Watermark
    52.                 SendMessage(Me.Handle, EM_SETCUEBANNER, 0I, m_WaterMarkText)
    53.  
    54.                 'Raise the changed event
    55.                 Call OnWaterMarkTextChanged(EventArgs.Empty)
    56.             End If
    57.         End Set
    58.     End Property
    59.  
    60.     ''' <summary>
    61.     ''' Routine for actually raising the event
    62.     ''' </summary>
    63.     ''' <param name="e"></param>
    64.     ''' <remarks></remarks>
    65.     Protected Overridable Sub OnWaterMarkTextChanged(e As EventArgs)
    66.         RaiseEvent WaterMarkTextChanged(Me, e)
    67.     End Sub
    68.  
    69. End Class
    Last edited by JuggaloBrotha; Feb 4th, 2015 at 12:11 PM.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width