|
-
Feb 27th, 2010, 01:54 PM
#1
Thread Starter
New Member
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!
-
Feb 27th, 2010, 03:19 PM
#2
Addicted Member
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.
-
Feb 27th, 2010, 03:34 PM
#3
Addicted Member
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
Last edited by spyk3; Feb 27th, 2010 at 03:39 PM.
-
Feb 27th, 2010, 03:38 PM
#4
New Member
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
-
Feb 28th, 2010, 03:12 PM
#5
Thread Starter
New Member
Re: I want text box to be cleared on click just once...
 Originally Posted by Bewl
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
-
Mar 1st, 2010, 05:55 PM
#6
Addicted Member
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.
-
Mar 1st, 2010, 09:01 PM
#7
New Member
Re: I want text box to be cleared on click just once...
 Originally Posted by spyk3
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".
-
Mar 4th, 2010, 06:46 AM
#8
Thread Starter
New Member
Re: I want text box to be cleared on click just once...
 Originally Posted by spyk3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|