Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I have the following code for a password box and when the user clicks it, I'd like for the text "Password" to be removed and a password character to be used.
VB
Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
       With TextBox2
           If .Text = "Password" Then
               .Text = Nothing
               .ForeColor = Color.Black
               .UseSystemPasswordChar = True
           End If
       End With
   End Sub


Whenever I click textbox2, I get the error

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll

Additional information: Error creating window handle.


on the line
VB
.UseSystePasswordChar = True


I have no idea why this is happening as I've never encountered it before.
How can I make this work?

-Rixterz
Posted
Comments
Sergey Alexandrovich Kryukov 3-Nov-14 18:28pm    
This is not related to password. You are not showing the offending code. Most likely, your method is used as an event handler for the event GetFocus, not Click.
You need to provide exact exception information, including the line of code. Look, you already asked so many questions and keep getting a lot of criticism related to the quality of your question. Can you finally learn the lessons and provide comprehensive information?
—SA
[no name] 4-Nov-14 10:40am    
If by "offensive" you meant that I'm making spyware or such, this is the code for the form which is a login screen. Somehow I can't see anything wrong with this:
Public Class Form1
Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
With TextBox1
If .Text = "Username" Then
.Text = Nothing
.ForeColor = Color.Black
End If
End With
End Sub
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
With TextBox1
If .Text.Replace(" ", Nothing) = Nothing Then
.Text = "Username"
.ForeColor = Color.Gray
End If
End With
End Sub
Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
With TextBox2
If .Text = "Password" Then
.Text = Nothing
.ForeColor = Color.Black
.UseSystemPasswordChar = True
End If
End With
End Sub
Private Sub TextBox2_LostFocus(sender As Object, e As EventArgs) Handles TextBox2.LostFocus
With TextBox2
If .Text.Replace(" ", Nothing) = Nothing Then
.Text = "Password"
.ForeColor = Color.Gray
.UseSystemPasswordChar = False
End If
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox2.UseSystemPasswordChar = True
End Sub
End Class
Sergey Alexandrovich Kryukov 4-Nov-14 13:06pm    
Gosh, no, "offensive code" simply means the piece of code which is responsible for exception throwing, has some kind of a bug, nothing else. Sorry if I confused you with this expression.
—SA
[no name] 4-Nov-14 13:06pm    
I showed the line of code causing the exception but I've solved it now
Sergey Alexandrovich Kryukov 4-Nov-14 14:23pm    
That's great.
—SA

1 solution

I got it working just by looking at a line and feeling all suspicious about it. I changed the line

If .Text = "Password" Then

to

If .UseSystemPasswordChar = False Then

I have no idea why it works but it does!
 
Share this answer
 

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