Click here to Skip to main content
15,740,797 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have 2 textboxes in my login form, obviously for the user name and password.

Now instead of putting a label called User Name and Password beside the textboxes, I want to put it inside, similar to the Search textbox in CP.

So what I did was to write the code for the GotFocus and LostFocus events.
This worked on the UserName, however I am having problem with the Password.

I have tried setting the UseSystemPasswordChar property of the password textbox on the code but I got a Error creating window handle.
It seems you can only set that property on the design time.

My question is, do you guys know of any other way of doing this? Thanks for the help.

UPDATE:

Below is the code. It seems that the issue here is when I put txtPassword.Text = "";. It doesn't have an error when I remove this line but the Password is not removed from the textbox.

C#
private void txtPassword_GotFocus(object sender, EventArgs e)
        {
            if (txtPassword.Text.Trim().ToUpper() == "PASSWORD")
            {
                txtPassword.ForeColor = Color.Black;
                txtPassword.Text = "";
                txtPassword.UseSystemPasswordChar = true;
            }
        }
Posted
Updated 11-Jul-11 23:04pm
v3
Comments
Dalek Dave 12-Jul-11 4:06am    
Edited for Readability.

Use MaskedTextBox[^].
Set its Mask Property[^] to A when not on focus to show alphanumeric and change it into * or -whatever character you likes- when it got focus.

Hope this helps
 
Share this answer
 
v2
Comments
Dalek Dave 12-Jul-11 4:06am    
Good Call.
DanHodgson88 12-Jul-11 4:44am    
nice answer
I think you are trying to do something very, very wrong here, that has nothing to do with UseSystemPasswordChar :
private void butShowPWChars_Click(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = false;
    }
private void butHidePWChars_Click(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = true;
    }
Works exactly as I expect: Hidden, text shows as blobs, shown, it is readable again.
If I type "Hello " then hide the data, I get 6 blobs. If I then add "There." I see twelve blobs.
Show the text again, and I see "Hello There.".
At no time to I get a handle problem.

Post the code fragment you are trying to use to do this: I think your problem may be more fundamental.


[edit]Updated to answer Musefan - OriginalGriff[/edit]

"Perhaps the error is caused when changing while the control has focus? Which sounds like is what the OP is trying. I see your button clicks will take the focus"

I tried this as well:
private void tbPassword_Enter(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = false;
    }
private void tbPassword_Leave(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = true;
    }
Still no handle problems.
 
Share this answer
 
v2
Comments
musefan 12-Jul-11 4:29am    
Perhaps the error is caused when changing while the control has focus? Which sounds like is what the OP is trying. I see your button clicks will take the focus
OriginalGriff 12-Jul-11 4:40am    
I tried that: see updated answer.
musefan 12-Jul-11 4:44am    
Then I will have to agree it sounds like some dodgy code that we would need to see
samir.abda 12-Jul-11 5:04am    
I have updated my question. Please see. Thank you.
OriginalGriff 12-Jul-11 5:21am    
I just tried it, and I'm not getting your problem at all. Mind you, I had to add a handler for GotFocus manually, as it isn't available in the designer, just via intellisense.
For the record: I set the textbox password to "PASSWORD", the ForeColor to LightGrey, and UseSystemPasswordChar to false.
Cut'n'paste your code into the GotFocus event handler, and it works fine.
Where does your code differ? Why didn't you use the Enter and Leave events instead? That way you could set the grey/false/prompt back as well...
An interesting question. I have not done before but one thing that comes to mind would be to create a third standard text box which is in the same position as the password box

Lets say...

TextBoxUsername
TextBoxPassword
TextBoxPlainPassword

Initially, you start with TextBoxPlainPassword visible and TextBoxPassword hidden. Then you add a GotFocus event to TextBoxPlainPassword and a LostFocus event to TextBoxPassword.

Now, in your GotFocus event you do the following...

1. Hide TextBoxPlainPassword
2. Show TextBoxPassword
3. Set Focus to TextBoxPassword

...and on the LostFocus event you do...

1. Detect if TextBoxPassword is empty
2. if Empty then:
- Hide TextBoxPassword
- Show TextBoxPlainPassword
3. Else:
- Do nothing


I know it may be a bit of a hack but it sounds like it should work if you don't find a better way
 
Share this answer
 
Comments
Uday P.Singh 12-Jul-11 4:25am    
interesting one! my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900