none
Description text in textbox as

    Question

  • Hi,

    I want to put some description/tooltip text within textbox control which will vanish when the focus is on that control. Its similar to that like in the search box above. When the search box is empty it shows "Search MSDN with Bing" and when the contol gets the focus that string vanishes.

    I am using VB2008.
    Thursday, November 26, 2009 2:13 PM

Answers

  • Good ocde from DerStauner -- I thought I'd make a liitle more robust and dynamic  version in case you were interested.  The code below can be plugged into an existing form  -- just paste in the Hint Text region code (its independent of any controls on your form) -- and then add the single line at the bottom of the Load event handler to your Load handler.

    The InitializeHintText method is meant to recursively crawl through your nested controls and add handlers to assist in the HintText functionality as DerStauner wrote it.  I got rid of the Tag property storing the text, but if you want different text for different textboxes then you may want to go back to that style of storage (or use a dictionary to store and manage the hint text).
     
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Other Load code here...
            InitializeHintText(Me)
        End Sub
    
    #Region " Hint Text "
        Private Const HintText As String = "Enter text here"
    
        Private Sub InitializeHintText(ByVal ctrl As Control)
            'Recursive method to reach all nested controls on a form.
            If TypeOf ctrl Is TextBox Then
                AddHandler CType(ctrl, TextBox).GotFocus, AddressOf TextBox_GotFocus
                AddHandler CType(ctrl, TextBox).LostFocus, AddressOf TextBox_LostFocus
                MakeDescription(CType(ctrl, TextBox))
            Else
                For Each c As Control In ctrl.Controls
                    InitializeHintText(c)
                Next
            End If
        End Sub
    
        Private Sub MakeDescription(ByVal txtbox As TextBox)
            If txtbox.Text = "" Then
                txtbox.Text = HintText
                txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Italic)
                txtbox.ForeColor = Color.Gray
            End If
        End Sub
    
        Private Sub RemoveDescription(ByVal txtbox As TextBox)
            txtbox.Text = ""
            txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Regular)
            txtbox.ForeColor = System.Drawing.SystemColors.WindowText
        End Sub
    
        Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
            If sender IsNot Nothing AndAlso TypeOf sender Is TextBox Then
                RemoveDescription(CType(sender, TextBox))
            End If
        End Sub
    
        Private Sub TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
            If sender IsNot Nothing AndAlso TypeOf sender Is TextBox Then
                MakeDescription(CType(sender, TextBox))
            End If
        End Sub
    
    #End Region
    
     
     
     
     
     
    • Marked as answer by Jeff Shan Wednesday, December 02, 2009 8:30 AM
    Friday, November 27, 2009 12:36 PM
  • What you are looking for is called "Hint Text" and there are many ways to do this.  In fact, this capacity is built into Windows and can be accessed through an API in one example I saw the last time I looked into it.  I'm sorry that I don't have any code for you at the moment but you could get a head start by doing a search.  There are many examples out there but some can be quite buggy.  Here's a start:

    http://www.google.com/search?hl=en&q=VB.NET+%22hint+text%22+in+textbox&aq=f&oq=&aqi=
    • Marked as answer by Jeff Shan Wednesday, December 02, 2009 8:30 AM
    Thursday, November 26, 2009 3:44 PM
  • please check the following code from myself

    Public Class Form1
        Private Sub MakeDescription(ByVal txtbox As TextBox)
            If txtbox.Text = "" Then
                txtbox.Text = txtbox.Tag
                txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Italic)
                txtbox.ForeColor = Color.Gray
            End If
        End Sub
        Private Sub RemoveDescription(ByVal txtbox As TextBox)
            txtbox.Text = ""
            txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Regular)
            txtbox.ForeColor = System.Drawing.SystemColors.WindowText
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MakeDescription(TextBox1)
            MakeDescription(TextBox2)
        End Sub
    
        Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
            RemoveDescription(TextBox1)
        End Sub
    
        Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
            MakeDescription(TextBox1)
        End Sub
    
        Private Sub TextBox2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.GotFocus
            RemoveDescription(TextBox2)
        End Sub
    
        Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
            MakeDescription(TextBox2)
        End Sub
    End Class
    the description text is stored in the Tag properties of the textboxes. At start the focus is on a button (Tabstop=1).
    • Marked as answer by Jeff Shan Wednesday, December 02, 2009 8:30 AM
    Thursday, November 26, 2009 5:21 PM

All replies

  • What you are looking for is called "Hint Text" and there are many ways to do this.  In fact, this capacity is built into Windows and can be accessed through an API in one example I saw the last time I looked into it.  I'm sorry that I don't have any code for you at the moment but you could get a head start by doing a search.  There are many examples out there but some can be quite buggy.  Here's a start:

    http://www.google.com/search?hl=en&q=VB.NET+%22hint+text%22+in+textbox&aq=f&oq=&aqi=
    • Marked as answer by Jeff Shan Wednesday, December 02, 2009 8:30 AM
    Thursday, November 26, 2009 3:44 PM
  • please check the following code from myself

    Public Class Form1
        Private Sub MakeDescription(ByVal txtbox As TextBox)
            If txtbox.Text = "" Then
                txtbox.Text = txtbox.Tag
                txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Italic)
                txtbox.ForeColor = Color.Gray
            End If
        End Sub
        Private Sub RemoveDescription(ByVal txtbox As TextBox)
            txtbox.Text = ""
            txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Regular)
            txtbox.ForeColor = System.Drawing.SystemColors.WindowText
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MakeDescription(TextBox1)
            MakeDescription(TextBox2)
        End Sub
    
        Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
            RemoveDescription(TextBox1)
        End Sub
    
        Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
            MakeDescription(TextBox1)
        End Sub
    
        Private Sub TextBox2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.GotFocus
            RemoveDescription(TextBox2)
        End Sub
    
        Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
            MakeDescription(TextBox2)
        End Sub
    End Class
    the description text is stored in the Tag properties of the textboxes. At start the focus is on a button (Tabstop=1).
    • Marked as answer by Jeff Shan Wednesday, December 02, 2009 8:30 AM
    Thursday, November 26, 2009 5:21 PM
  • Good ocde from DerStauner -- I thought I'd make a liitle more robust and dynamic  version in case you were interested.  The code below can be plugged into an existing form  -- just paste in the Hint Text region code (its independent of any controls on your form) -- and then add the single line at the bottom of the Load event handler to your Load handler.

    The InitializeHintText method is meant to recursively crawl through your nested controls and add handlers to assist in the HintText functionality as DerStauner wrote it.  I got rid of the Tag property storing the text, but if you want different text for different textboxes then you may want to go back to that style of storage (or use a dictionary to store and manage the hint text).
     
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Other Load code here...
            InitializeHintText(Me)
        End Sub
    
    #Region " Hint Text "
        Private Const HintText As String = "Enter text here"
    
        Private Sub InitializeHintText(ByVal ctrl As Control)
            'Recursive method to reach all nested controls on a form.
            If TypeOf ctrl Is TextBox Then
                AddHandler CType(ctrl, TextBox).GotFocus, AddressOf TextBox_GotFocus
                AddHandler CType(ctrl, TextBox).LostFocus, AddressOf TextBox_LostFocus
                MakeDescription(CType(ctrl, TextBox))
            Else
                For Each c As Control In ctrl.Controls
                    InitializeHintText(c)
                Next
            End If
        End Sub
    
        Private Sub MakeDescription(ByVal txtbox As TextBox)
            If txtbox.Text = "" Then
                txtbox.Text = HintText
                txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Italic)
                txtbox.ForeColor = Color.Gray
            End If
        End Sub
    
        Private Sub RemoveDescription(ByVal txtbox As TextBox)
            txtbox.Text = ""
            txtbox.Font = New Font("microsoft sans serif", 8, FontStyle.Regular)
            txtbox.ForeColor = System.Drawing.SystemColors.WindowText
        End Sub
    
        Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
            If sender IsNot Nothing AndAlso TypeOf sender Is TextBox Then
                RemoveDescription(CType(sender, TextBox))
            End If
        End Sub
    
        Private Sub TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
            If sender IsNot Nothing AndAlso TypeOf sender Is TextBox Then
                MakeDescription(CType(sender, TextBox))
            End If
        End Sub
    
    #End Region
    
     
     
     
     
     
    • Marked as answer by Jeff Shan Wednesday, December 02, 2009 8:30 AM
    Friday, November 27, 2009 12:36 PM