1 Replies - 10125 Views - Last Post: 16 February 2017 - 03:02 AM

#1 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Setting CueBanner Text Of Your TextBoxes

Posted 01 March 2016 - 12:21 PM

Here is an easy way to add CueBanner text in your TextBoxes. A CueBanner is used for instances where you want a grayed out text displayed in the TextBox to indicate to the user what is suppose to be typed in the TextBox. For example, a TextBox that is used for typing a search word into may have something like "Search Text..." or similar text in it until the user sets focus to the TextBox or until the user starts typing a word in it.

Here is a small helper class that you can add to your project and use to set the CueBanner text of any single line TextBox. This does not work for a multiline TextBox, nor does it work on a RichTextBox.
Imports System.Runtime.InteropServices

Public Class TextboxHelper
    Private Const EM_SETCUEBANNER As Integer = &H1501

    <DllImport("user32.dll", EntryPoint:="SendMessageW")> _
    Private Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As UInteger, <MarshalAs(UnmanagedType.LPTStr)> ByVal lParam As String) As Integer
    End Function

    ''' <summary>Shows grayed text in the textbox to indicate what the textbox is for.</summary>
    ''' <param name="TxBx">The textbox to set the cuebanner text in.</param>
    ''' <param name="QBannerText">The cuebanner text to show in the textbox.</param>
    ''' <param name="Clearonfocus">True to clear the cuebanner text when the textbox receives focus, otherwise False.</param>
    Public Shared Sub SetCueBanner(ByVal TxBx As TextBox, ByVal QBannerText As String, ByVal Clearonfocus As Boolean)
        Dim showfocused As UInteger = 1
        If Clearonfocus Then showfocused = 0
        SendMessageW(TxBx.Handle, EM_SETCUEBANNER, showfocused, QBannerText)
    End Sub
End Class

In the Form you can use it like this to set the CueBanner of a TextBox.
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Font = New Font(TextBox1.Font.FontFamily, 9, FontStyle.Bold)
        TextboxHelper.SetCueBanner(TextBox1, "Enter Search Text Here...", False)
    End Sub

End Class

Attached Image

Is This A Good Question/Topic? 1
  • +

Replies To: Setting CueBanner Text Of Your TextBoxes

#2 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,610
  • Joined: 07-September 13

Re: Setting CueBanner Text Of Your TextBoxes

Posted 16 February 2017 - 03:02 AM

Maybe worth noting that this only works if your project has Visual Styles turned on.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1