Results 1 to 8 of 8

Thread: I want text box to be cleared on click just once...

  1. #1

    Thread Starter
    New Member Gene.H's Avatar
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    10

    Question I want text box to be cleared on click just once...



    On my login form I have a username text box and a password text box...

    The TxtUsername text box is preset with the following:
    Text: Username
    Colour: Gray

    Here is the code for the TxtUsername (on click):
    Private Sub TxtUsername_Click
    TxtUsername.Text = ""
    TxtUsername.ForeColor = Color.Black

    (So it's like the facebook login if your familiar with it)

    When user clicks the text box, the gray text already inside gets erased and user is able to type with black text.

    The problem is that even when the user has typed in the box, every time you click the text box the text still gets erased... I've been googling and experimenting everything I can't find a thing!

    It's like the code should be something like If TxtUsername.Text = Anything but "Username" then dont change it to ""

    Or maybe theres a whole different code to type?

    Thanks in advanced!

  2. #2
    Addicted Member
    Join Date
    Oct 2008
    Posts
    152

    Re: I want text box to be cleared on click just once...

    I would suggest using the textbox_Enter event instead of the click event.

  3. #3
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: I want text box to be cleared on click just once...

    Just add this to your form or copy the sub routines to your textbox's GotFocus and LostFocus

    Code:
        Private Sub TxtUsername_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtUsername.GotFocus
            If TxtUsername.Text.Replace(" ", "").ToLower.Contains("username") Then
                Dim newFont As Font = New Font("Microsoft Sans Serif", 10, FontStyle.Regular)
                TxtUsername.ForeColor = Color.Black
                TxtUsername.Font = newFont
                TxtUsername.Text = ""
            End If
        End Sub
    Code:
        Private Sub TxtUsername_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtUsername.LostFocus
            If TxtUsername.Text.Replace(" ", "").ToLower = "" Then
                Dim newFont As Font = New Font("Microsoft Sans Serif", 8, FontStyle.Italic)
                TxtUsername.ForeColor = Color.Gainsboro 'LightGray is a little bit darker
                TxtUsername.Font = newFont
                TxtUsername.Text = "Username"
            End If
        End Sub

  4. #4
    New Member
    Join Date
    Feb 2010
    Posts
    13

    Re: I want text box to be cleared on click just once...

    Since you will be changing the text color to black once focused, why not check for the color of the text?

    psuedocode:

    if textbox color = gray then
    clear textbox
    textbox color = black
    end if

    just incase for some reason, the user wants his username as "username" :P

  5. #5

    Thread Starter
    New Member Gene.H's Avatar
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    10

    Re: I want text box to be cleared on click just once...

    Quote Originally Posted by Bewl View Post
    Since you will be changing the text color to black once focused, why not check for the color of the text?

    psuedocode:

    if textbox color = gray then
    clear textbox
    textbox color = black
    end if

    just incase for some reason, the user wants his username as "username" :P
    This works, thanks a lot! It's always the smallest thing you don't realize isn't it, ive been trying to figure this out for so long and the solution is so simple xD, thanks again! +1

  6. #6
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: I want text box to be cleared on click just once...

    psuedocode:

    if textbox color = gray then
    clear textbox
    textbox color = black
    end if
    And What about when there is text in the box? then it will still dim*? is this what you want?
    a username should never be username, it's just really bad db'ing

    *The text in the box will still go gray, reguardless of what's in it.
    -The code I gave you is more like a normal username interface, whereby if text is typed in box, then it will stay until it is backspaced or deleted out, in which case, when the box loses focus, it will return to a dim gray word of "username"
    -Also with that pseudo code, the word "username" can't be replaced in the textbox
    Last edited by spyk3; Mar 2nd, 2010 at 09:10 AM.

  7. #7
    New Member
    Join Date
    Feb 2010
    Posts
    13

    Re: I want text box to be cleared on click just once...

    Quote Originally Posted by spyk3 View Post
    And What about when there is text in the box? then it will still dim? is this what you want?
    a username should never be username, it's just really bad db'ing
    Not exactly sure what you mean by "still dim".

  8. #8

    Thread Starter
    New Member Gene.H's Avatar
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    10

    Re: I want text box to be cleared on click just once...

    Quote Originally Posted by spyk3 View Post
    And What about when there is text in the box? then it will still dim*? is this what you want?
    a username should never be username, it's just really bad db'ing

    *The text in the box will still go gray, reguardless of what's in it.
    -The code I gave you is more like a normal username interface, whereby if text is typed in box, then it will stay until it is backspaced or deleted out, in which case, when the box loses focus, it will return to a dim gray word of "username"
    -Also with that pseudo code, the word "username" can't be replaced in the textbox
    And that's why is my register form I have:

    If TxtUser.Text = "Username" Or TxtPass.Text = "Password" Then
    MsgBox "You cannot have "username" or "password" as your login details"
    End If

    So users will never be able to even register as "username"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width