Password Field in TextBox Control: Windows XP vs Vista

darkagn 2 Tallied Votes 360 Views Share

Hi all,

Well this is my 1000th post here at DaniWeb and I thought "What better way to celebrate than by posting my first ever code snippet?"

This problem occurred in an application that I am working on. I have a password field in my form that was specified as Multiline, and it correctly worked on my PC, but my friend's PC was showing it as text. It turns out that Windows XP expects password fields to be single line only, but Vista (and I assume Windows 7) doesn't care if it is a single or multi line text box. In the code snippet you see three text boxes being added to a form. On XP, only the first field will correctly display a * character in place of the text, whereas in Vista all three will.

I guess a password field would be unusual to accept multiline text, but I overlooked this property when attempting to solve the issue. I hope this sheds some light for someone out there...

kvprajapati commented: Congrats! you are awesome. +11
public partial class MyForm : Form
{
  private TextBox passwordField1;
  private TextBox passwordField2;
  private TextBox passwordField3;

  public MyForm()
  {
    InitializeComponent();

    passwordField1 = new TextBox();
    passwordField2 = new TextBox();
    passwordField3 = new TextBox();
    
    passwordField1.PasswordChar = '*';
    passwordField2.UseSystemPassword = true;
    passwordField3.PasswordChar = '*';

    passwordField1.Multiline = false;
    passwordField2.Multiline = true;
    passwordField3.Multiline = true;

    passwordField1.Top = 10;
    passwordField2.Top = 50;
    passwordField3.Top = 100;

    passwordField1.Left = 10;
    passwordField2.Left = 50;
    passwordField3.Left = 100;

    Controls.Add(passwordField1);
    Controls.Add(passwordField2);
    Controls.Add(passwordField3);
  }
}
ashishkumar008 2 Light Poster

hello,
i run this code in vs2005 in window 7
then,
second text box simply show text when multiline is true.
i don't know why? but if u know then please tell me.
what is the exact reason.

darkagn 315 Veteran Poster Featured Poster

If that is the case then the issue is specific to Vista. The PasswordChar property should only work (according to the MSDN documentation) if the textbox's Multiline property is false. However Vista allows it to be true! I assumed in my previous post that Windows 7 would follow the same as Vista, but obviously not...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.