7

In my application I have one text box for entering user name. If text is empty i want to show "Enter User name here" in same text box in gray color. Is there any property like this for text box. Like in Firefox browser if URL field is empty it will show "Go to a web site" In gray color

Thanks

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
Royal Pinto
  • 183
  • 2
  • 3
  • 10
  • possible duplicate of [How do I fill an empty textbox with default text?](http://stackoverflow.com/questions/5178247/how-do-i-fill-an-empty-textbox-with-default-text), [Watermark TextBox in WinForms](http://stackoverflow.com/questions/4902565/watermark-textbox-in-winforms/) – Cody Gray - on strike Aug 12 '11 at 06:10

6 Answers6

9

I know this may be an old thread, but I thought I'd answer it just in case anyone else stumbled across it.

First declare the following (you may need to import System.Runtime.InteropServices)

<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

Then call the following (change as needed):

SendMessage(Me.textbox1.Handle, &H1501, 0, "Enter User name here")
Grahamvs
  • 669
  • 6
  • 16
  • Great answer, Grahamvs! That's something I was looking for. I'm having issues trying to show the hint on TWO different textboxes in the form_load event, though. Any tips? – Sev09 Feb 18 '14 at 16:09
  • @Sev09 Can you post your code? Try `SendMessage(Me.textbox2.Handle, &H1501, 0, "Your second message here")`. I'd recommend creating a new question, and adding a comment with @Grahamvs so I can see it. I'd also recommend creating a new class, inheriting from textbox, and adding the code in there, as it would make it a lot easier to re-use the code – Grahamvs Feb 18 '14 at 17:16
  • here's the link: http://stackoverflow.com/questions/21863124/create-a-textbox-hint-message-for-two-different-textboxeswatermark – Sev09 Feb 18 '14 at 19:10
  • The issues seems to be that it was a multiline textbox. Thanks again for your initial answer. – Sev09 Feb 18 '14 at 20:20
1

you can subclass TextBox and override WndProc

Public Class TextBoxPlaceHolder
    Inherits TextBox
    Private Const WM_PAINT As Int32 = &HF
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WM_PAINT AndAlso Me.TextLength = 0 Then
            Using g = Me.CreateGraphics
                g.DrawString("Enter User name here", Me.Font, Brushes.Gray, 1, 1)
            End Using
        End If
    End Sub
End Class
Ivan Ferrer Villa
  • 2,129
  • 1
  • 26
  • 23
  • 1
    Thanks! This was great and I wanted to use it multiple times, so I created a property in this class: Private Hint As String Public WriteOnly Property HintText() As String Set(Value As String) Hint = Value End Set End Property Then I overrode the hard coded text above with: g.DrawString(Hint, Me.Font, Brushes.Gray, 1, 1) From the calling form, I set the hint using: Form1.TextBoxPlaceHolder1.HintText = "Enter Desired Name" – cardmagik Jan 22 '18 at 01:47
1

Assuming you mean Windows Forms, take a look at this question.
Basically, you need to call a WinAPI SendMessage function for the control with EM_SETCUEBANNERvalue.

Community
  • 1
  • 1
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
1

I really like this solution from CodeProject.com: http://www.codeproject.com/KB/edit/TextBoxHint.aspx?display=Print

What's really nice is the slick fade out as the user types in his/her text into the field. It's pretty darn easy to implement and looks great.

nlinus
  • 641
  • 5
  • 12
0

Awesome work Ivan and cardmagik. Create a usercontrol and use the below code.

Public Class RichTextBoxPlaceHolder
    Inherits RichTextBox
    Private Const WM_PAINT As Int32 = &HF
    Private mstrHint As String = "Enter text"

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WM_PAINT AndAlso Me.TextLength = 0 Then
            Using g = Me.CreateGraphics
                g.DrawString(mstrHint, Me.Font, Brushes.Gray, 1, 1)
            End Using
        End If
    End Sub

    Public Property Hint() As String
        Get
            Return mstrHint
        End Get
        Set(ByVal value As String)
            mstrHint = value
        End Set
    End Property

End Class

Screenshot:

enter image description here

Isidoros Moulas
  • 520
  • 3
  • 10
-1

Try something along the lines of this:

(onkeyup)
If TextBox.Text = "" Then
  TextBox.Text = "Enter username here..."
  TextBox.ForeColor = <your chosen color here>
Else 
  TextBox.ForeColor = <normal color here>
End If
Jared Ng
  • 4,891
  • 2
  • 19
  • 18
  • A bad idea. Doesn't handle mouse clicks (right-click -> context menu -> cut shouldn't be available), `Text` will return this placeholder as *actual text* (you'd want it to return `string.Empty`), etc. There are many drawbacks to this. – Dan Abramov Aug 12 '11 at 05:56
  • Thanks for the feedback. I wasn't intending to provide a solution to cover all use cases, since an explanation on exactly how to cover every edge case would be far too long. My intent was just to give Royal a push in the right direction, ie. the idea of changing the element ForeColor whenever the text (potentially) changes. – Jared Ng Aug 12 '11 at 06:02
  • Thanks but is there any property for this. I don't want to write code for this bcz as per Dan Abramov lot of validations i have to do.(If property is not there i will add label beside text box.) – Royal Pinto Aug 12 '11 at 06:08
  • 1
    If it's not a huge part of your UI design I would say just go with adding the label beside the text box. It's significantly easier. – Jared Ng Aug 12 '11 at 06:16