none
How to make 'Transparent' text in text box RRS feed

  • Question

  • So you know when you open Google.com and in the search bar it says "Search google or Type a URL", 

    How would you do that with a text box in Visual Basic, have grayish text in a text box and then when the user starts to type in it the text disappears.

    Wednesday, December 5, 2018 9:56 PM

Answers

  • Here you go. works for TextBox and ComboBox when not DropDownList.

    Add a new code module to your project and insert the following code.

    Imports System.Runtime.InteropServices
    
    Public Module CueBannerTextCodeExtensions
        Public Enum WaterMark
            ' Hide cue text when entering control
            Hide = 0
            ' Show cue text when entering control until user begins to type
            Show = 1
        End Enum
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> lParam As String) As Int32
        End Function
    
        <DllImport("user32", EntryPoint:="FindWindowExA", ExactSpelling:=True, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Private Function FindWindowEx(hWnd1 As IntPtr, hWnd2 As IntPtr, lpsz1 As String, ByVal lpsz2 As String) As IntPtr
        End Function
        Private Const EM_SETCUEBANNER As Integer = &H1501
        ''' <summary>
        ''' Used to place shadow text into a TextBox or ComboBox when control does not have focus
        ''' </summary>
        ''' <param name="pControl">Name of control</param>
        ''' <param name="pText">Shadow text to show when control does not have focus</param>
        ''' <remarks>
        ''' Some might call this a watermark affect
        ''' </remarks>
        <Runtime.CompilerServices.Extension>
        Public Sub SetCueText(pControl As Control, pText As String)
    
            If TypeOf pControl Is ComboBox Then
                Dim editHWnd = FindWindowEx(pControl.Handle, IntPtr.Zero, "Edit", Nothing)
                If Not editHWnd = IntPtr.Zero Then
                    SendMessage(editHWnd, EM_SETCUEBANNER, 0, pText)
                End If
            ElseIf TypeOf pControl Is TextBox Then
                SendMessage(pControl.Handle, EM_SETCUEBANNER, 0, pText)
            End If
        End Sub
        ''' <summary>
        ''' Used to place shadow text into a TextBox or ComboBox when control does not have focus
        ''' </summary>
        ''' <param name="pControl">Name of control</param>
        ''' <param name="pText">Shadow text to show when control does not have focus</param>
        ''' <param name="pValue">show water mark upon entering control or not</param>
        ''' <remarks>
        ''' Some might call this a watermark affect
        ''' </remarks>
        <Runtime.CompilerServices.Extension>
        Public Sub SetCueText(pControl As Control, pText As String, pValue As WaterMark)
    
            If TypeOf pControl Is ComboBox Then
                Dim editHWnd As IntPtr = FindWindowEx(pControl.Handle, IntPtr.Zero, "Edit", Nothing)
                If Not (editHWnd = IntPtr.Zero) Then
                    SendMessage(editHWnd, EM_SETCUEBANNER, CInt(Fix(pValue)), pText)
                End If
            ElseIf TypeOf pControl Is TextBox Then
                SendMessage(pControl.Handle, EM_SETCUEBANNER, CInt(Fix(pValue)), pText)
            End If
        End Sub
    End Module
    

    Usage

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            firstNameTextBox.SetCueText("Enter first name")
            lastNameTextBox.SetCueText("Enter last name")
        End Sub
    End Class

    Sample for above


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Frozen_Nova Thursday, December 6, 2018 12:09 AM
    Wednesday, December 5, 2018 11:24 PM
    Moderator

All replies

  • Wednesday, December 5, 2018 10:08 PM
  • Here you go. works for TextBox and ComboBox when not DropDownList.

    Add a new code module to your project and insert the following code.

    Imports System.Runtime.InteropServices
    
    Public Module CueBannerTextCodeExtensions
        Public Enum WaterMark
            ' Hide cue text when entering control
            Hide = 0
            ' Show cue text when entering control until user begins to type
            Show = 1
        End Enum
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> lParam As String) As Int32
        End Function
    
        <DllImport("user32", EntryPoint:="FindWindowExA", ExactSpelling:=True, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Private Function FindWindowEx(hWnd1 As IntPtr, hWnd2 As IntPtr, lpsz1 As String, ByVal lpsz2 As String) As IntPtr
        End Function
        Private Const EM_SETCUEBANNER As Integer = &H1501
        ''' <summary>
        ''' Used to place shadow text into a TextBox or ComboBox when control does not have focus
        ''' </summary>
        ''' <param name="pControl">Name of control</param>
        ''' <param name="pText">Shadow text to show when control does not have focus</param>
        ''' <remarks>
        ''' Some might call this a watermark affect
        ''' </remarks>
        <Runtime.CompilerServices.Extension>
        Public Sub SetCueText(pControl As Control, pText As String)
    
            If TypeOf pControl Is ComboBox Then
                Dim editHWnd = FindWindowEx(pControl.Handle, IntPtr.Zero, "Edit", Nothing)
                If Not editHWnd = IntPtr.Zero Then
                    SendMessage(editHWnd, EM_SETCUEBANNER, 0, pText)
                End If
            ElseIf TypeOf pControl Is TextBox Then
                SendMessage(pControl.Handle, EM_SETCUEBANNER, 0, pText)
            End If
        End Sub
        ''' <summary>
        ''' Used to place shadow text into a TextBox or ComboBox when control does not have focus
        ''' </summary>
        ''' <param name="pControl">Name of control</param>
        ''' <param name="pText">Shadow text to show when control does not have focus</param>
        ''' <param name="pValue">show water mark upon entering control or not</param>
        ''' <remarks>
        ''' Some might call this a watermark affect
        ''' </remarks>
        <Runtime.CompilerServices.Extension>
        Public Sub SetCueText(pControl As Control, pText As String, pValue As WaterMark)
    
            If TypeOf pControl Is ComboBox Then
                Dim editHWnd As IntPtr = FindWindowEx(pControl.Handle, IntPtr.Zero, "Edit", Nothing)
                If Not (editHWnd = IntPtr.Zero) Then
                    SendMessage(editHWnd, EM_SETCUEBANNER, CInt(Fix(pValue)), pText)
                End If
            ElseIf TypeOf pControl Is TextBox Then
                SendMessage(pControl.Handle, EM_SETCUEBANNER, CInt(Fix(pValue)), pText)
            End If
        End Sub
    End Module
    

    Usage

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            firstNameTextBox.SetCueText("Enter first name")
            lastNameTextBox.SetCueText("Enter last name")
        End Sub
    End Class

    Sample for above


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Frozen_Nova Thursday, December 6, 2018 12:09 AM
    Wednesday, December 5, 2018 11:24 PM
    Moderator