0

I'm making a program which requires a login system. What I want to do is have a textbox which contains the word 'Username' and when the user clicks it, the 'Username' text is overwritten. For example, Spotify use it on their login screen:

enter image description here

My question is, how do I do this?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • I'd look into third-party controls like Infragistics. The out-of-the-box .NET controls can't do this. – rory.ap Oct 07 '15 at 14:01
  • It would be very easy to implement this on a Winform. Basically, you would set the initial text of the textbox to "Username" with a foreground color of gray. When the user selects the textbox, the text would disappear and the foreground color would turn to black. – ChicagoMike Oct 07 '15 at 14:28
  • See [Watermark TextBox in WinForms](http://stackoverflow.com/q/4902565/719186). It's also called "Cue Banner" or "Ghost Text". – LarsTech Oct 07 '15 at 14:42
  • I think everyone is overthinking this problem. See my answer below. – Lews Therin Oct 07 '15 at 14:46
  • @Sylverac Your current solution does not handle the possibility that a user *tabs* into the control. And what if the user enters the textbox and exits without writing their user name? – LarsTech Oct 07 '15 at 14:49
  • @LarsTech the asker specified that the user clicks the text box. While I agree that you need to code for those considerations, I was merely answering the question he asked. You could also use the `TextBox1.Enter` event if you wanted to handle tabs. – Lews Therin Oct 07 '15 at 15:00
  • Thanks for all your comments guys, @LarsTech gave me the answer though. I tried both of the suggestions below but unfortunately none of them worked. – James Angus Oct 07 '15 at 18:20
  • @JamesAngus What didn't work? I ran that code I provided and it behaved as you explained in your question... – Lews Therin Oct 16 '15 at 18:38

2 Answers2

0

Set the Text property of the TextBox that you are using for the username to the string "Username". Then, in the TextBox's Click event, change it to a blank string. Like so:

Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
    TextBox1.Text = ""
End Sub 

Edit: As @LarsTech mentioned, this does not address if the user tabs into the TextBox. If you wanted to account for that too, use the TextBox's Enter event instead:

Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
    TextBox1.Text = ""
End Sub
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
0

I agree, using Textbox1.Enter is the easiest solution. On top you can also catch the case of no text entered via TextBox1.Leave like that

Private Sub TextBoxLeaveHandle() Handles TextBox1.Leave
    If TextBox1.Text = "" Then
        TextBox1.Text = "Username"
    End If
End Sub

Sometimes it can also be useful to use the TextBox.SelectAll() function as it not immediately remove the entire text but (obviously from the name) select the entire text so you can overwrite it with your first keypress.