1

I am creating a login form by using Visual Basic. Is it possible for me to load a specific text in a textbox with greyish color and uneditable? (Just like the effect in Youtube search bar) What I manage to found on the internet is just hide function and change the color of the text.

Sorry for my confusing title.

Sukasa
  • 19
  • 5
  • If I'm understanding your request, what you are talking about is officially known as a "cue banner". If you search for that term then you'll find lots of matches. You can implement it yourself with a bit of unmanaged code or you can use a control that someone else created with that functionality built in. There would be many such ready-made controls around. – jmcilhinney Apr 29 '20 at 02:08

3 Answers3

1

Add this Module (ModExtentions) to the Project: it adds an extension method, SetCueBanner(), to TextBox Controls.
Specifying True or False, changes the cue banner behavior:

  • False: the cue banner is visible until the control gets focus,
  • True, the cue banner is visible until the first char is entered.

This internal functionality is activated sending an EM_SETCUEBANNER message to the Control.

Use it like this:

' The Cue Banner is visible until the control gets focus
TextBox1.SetCueBanner("Some text...", False)

' The Cue Banner is visible until a character is entered
TextBox1.SetCueBanner("Some text...", True)

The Module where the extension method is defined:

Imports System.Runtime.InteropServices

Public Module ModExtentions
    Private Const EM_SETCUEBANNER As Integer = &H1501

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    End Function

    <Extension()>
    Public Sub SetCueBanner(tbox As TextBox, ByVal text As String, ByVal showOnFocus As Boolean)
        SendMessage(tbox.Handle, EM_SETCUEBANNER, If(showOnFocus, 1, 0), text)
    End Sub
End Module
Jimi
  • 29,621
  • 8
  • 43
  • 61
0

Set the TextBox Enabled property to false. You can put in code from code but the user can't type in the box.

Mary
  • 14,926
  • 3
  • 18
  • 27
  • That was my first thought but I think that the OP is looking for a prompt that then disappears when you enter text. They mention the YouTube search bar and that contains the word "Search" until you start typing in it. – jmcilhinney Apr 29 '20 at 02:28
0

Set up your gray text in the Form.Load. The use the Enter and Leave events to change it to normal and back again.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.ForeColor = Color.Gray
    TextBox1.Text = "Search term here..."
End Sub

Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
    TextBox1.ForeColor = Color.Black
    TextBox1.Text = ""
End Sub

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    If String.IsNullOrWhiteSpace(TextBox1.Text) Then
        TextBox1.ForeColor = Color.Gray
        TextBox1.Text = "Search term here..."
    End If
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27
  • The drawback there is that you're actually setting the `Text` of the control and any code that uses that property will get that prompt text if you haven't entered anything else. By using a genuine cue banner, you avoid modifying the control's actual data. For instance, you couldn't use your code if the control was bound. – jmcilhinney Apr 29 '20 at 05:19