0

I wanted to create a textbox which has a help label inside the box which then disappears when the box has characters entered into it. I found one way of doing it which involves loading the form with text inside the textbox in the colour grey and then removing it when the user clicks on the box... The problem with this is i wanted to use a string.IsNullOrEmpty(textboxIP) but when the user hasn't typed anything into the box, the program sees the box as not empty as it has the pre-loaded writing in it. This is the code I used to remove the text on user click...

Dim WatermarkIP As String = "Yes"
    Dim WatermarkPing As String = "Yes"

    Private Sub textboxIP_Enter(sender As Object, e As EventArgs) Handles textboxIP.Enter
        If WatermarkIP = "Yes" Then
            textboxIP.Clear()
            textboxIP.ForeColor = Color.Black
            WatermarkIP = "No"
        End If
    End Sub
 Private Sub textboxPing_Enter(sender As Object, e As EventArgs) Handles textboxPing.Enter
        If WatermarkPing = "Yes" Then
            textboxPing.Clear()
            textboxPing.ForeColor = Color.Black
            WatermarkPing = "No"
        End If
    End Sub

Does anyone know of a better way of creating a greyed out help/hint label inside the textbox which IS NOT counted as text inside the box, does not have to be deleted by the user before they can type in the box and is maybe a bit simpler?

Jack Pollock
  • 344
  • 1
  • 13
  • The thing you are describing is called `CueText` or sometimes `CueBanner`. What is wrong with a label *beside* the textbox telling them what the enter? – user3697824 Sep 19 '15 at 18:44
  • @user3697824 Because I am going for a simplistic design... How would i go about using `CueText` or `CueBanner`? Thanks. – Jack Pollock Sep 19 '15 at 18:45
  • 2
    you could look [there](http://stackoverflow.com/questions/4902565/watermark-textbox-in-winforms) ; it's in C# but conversion to VB.Net shouldn't be too difficult (otherwise code-converter can help) – Sehnsucht Sep 19 '15 at 18:58

1 Answers1

0

I've found it finally!

This will give you that watermark text on your textbox controls:

Imports:

Imports System.Runtime.InteropServices

Global Declarations in your main class:

Private Const EM_SETCUEBANNER As Integer = &H1501

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function

Functions in your main class:

Private Sub SetCueText(ByVal control As Control, ByVal text As String)
SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
End Sub

Usage (usually in form_load event):

SetCueText(TextBox1, "blah")
SetCueText(TextBox2, "blahblah")

Hope this helps :)

Credit: http://www.vbforums.com/showthread.php?638105-CueBanner-Watermark-text-for-Textboxes

Jack Pollock
  • 344
  • 1
  • 13