Csharp/CSharp Tutorial/GUI Windows Forms/Password Field

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Password field

<source lang="csharp">using System; using System.ruponentModel; using System.Windows.Forms; public class TextForm : System.Windows.Forms.Form {

   private System.ruponentModel.Container components = null;
 private System.Windows.Forms.Button btnPasswordDecoderRing;
 private System.Windows.Forms.Label label3;
 private System.Windows.Forms.TextBox passwordBox;
 private System.Windows.Forms.Label label2;
 private System.Windows.Forms.TextBox capsOnlyBox;
 private System.Windows.Forms.Button btnGetMultiLineText;
 private System.Windows.Forms.Label label1;
 private System.Windows.Forms.TextBox multiLineBox;
   public TextForm()
   {
       InitializeComponent();
   CenterToScreen();
   }
 protected override void Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
 private void InitializeComponent()
 {
   this.capsOnlyBox = new System.Windows.Forms.TextBox();
   this.multiLineBox = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.passwordBox = new System.Windows.Forms.TextBox();
   this.btnGetMultiLineText = new System.Windows.Forms.Button();
   this.btnPasswordDecoderRing = new System.Windows.Forms.Button();
   this.label3 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   // 
   // capsOnlyBox
   // 
   this.capsOnlyBox.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
   this.capsOnlyBox.Location = new System.Drawing.Point(14, 176);
   this.capsOnlyBox.Name = "capsOnlyBox";
   this.capsOnlyBox.Size = new System.Drawing.Size(120, 20);
   this.capsOnlyBox.TabIndex = 3;
   this.capsOnlyBox.Text = "";
   // 
   // multiLineBox
   // 
   this.multiLineBox.AcceptsReturn = true;
   this.multiLineBox.AcceptsTab = true;
   this.multiLineBox.Location = new System.Drawing.Point(152, 8);
   this.multiLineBox.Multiline = true;
   this.multiLineBox.Name = "multiLineBox";
   this.multiLineBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.multiLineBox.Size = new System.Drawing.Size(240, 104);
   this.multiLineBox.TabIndex = 0;
   this.multiLineBox.Text = "Type some stuff here (and hit the return and tab keys...)";
   // 
   // label1
   // 
   this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
   this.label1.Location = new System.Drawing.Point(8, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(136, 56);
   this.label1.TabIndex = 1;
   this.label1.Text = "A multiline textbox that accepts tabs and return keystrokes.";
   // 
   // label2
   // 
   this.label2.Font = new System.Drawing.Font("Comic Sans MS", 12F);
   this.label2.Location = new System.Drawing.Point(14, 144);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(106, 24);
   this.label2.TabIndex = 4;
   this.label2.Text = "Caps only!!";
   // 
   // passwordBox
   // 
   this.passwordBox.Location = new System.Drawing.Point(160, 176);
   this.passwordBox.Name = "passwordBox";
   this.passwordBox.PasswordChar = "$";
   this.passwordBox.Size = new System.Drawing.Size(232, 20);
   this.passwordBox.TabIndex = 5;
   this.passwordBox.Text = "COM seems so old fashion";
   // 
   // btnGetMultiLineText
   // 
   this.btnGetMultiLineText.Location = new System.Drawing.Point(13, 72);
   this.btnGetMultiLineText.Name = "btnGetMultiLineText";
   this.btnGetMultiLineText.Size = new System.Drawing.Size(120, 32);
   this.btnGetMultiLineText.TabIndex = 2;
   this.btnGetMultiLineText.Text = "Get Text";
   this.btnGetMultiLineText.Click += new System.EventHandler(this.btnGetMultiLineText_Click);
   // 
   // btnPasswordDecoderRing
   // 
   this.btnPasswordDecoderRing.Location = new System.Drawing.Point(280, 144);
   this.btnPasswordDecoderRing.Name = "btnPasswordDecoderRing";
   this.btnPasswordDecoderRing.Size = new System.Drawing.Size(112, 24);
   this.btnPasswordDecoderRing.TabIndex = 7;
   this.btnPasswordDecoderRing.Text = "Decode Password";
   this.btnPasswordDecoderRing.Click += new System.EventHandler(this.btnPasswordDecoderRing_Click);
   // 
   // label3
   // 
   this.label3.Font = new System.Drawing.Font("Comic Sans MS", 12F);
   this.label3.Location = new System.Drawing.Point(152, 144);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(120, 24);
   this.label3.TabIndex = 6;
   this.label3.Text = "Password Box";
   // 
   // TextForm
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(408, 221);
   this.Controls.Add(this.btnPasswordDecoderRing);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.passwordBox);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.capsOnlyBox);
   this.Controls.Add(this.btnGetMultiLineText);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.multiLineBox);
   this.Name = "TextForm";
   this.Text = "TextBox Types";
   this.ResumeLayout(false);
 }
 protected void btnPasswordDecoderRing_Click (object sender, System.EventArgs e)
 {
   MessageBox.Show(passwordBox.Text, "Your password is:");
 }
 protected void btnGetMultiLineText_Click (object sender, System.EventArgs e)
 {
   MessageBox.Show(multiLineBox.Text, "Here is your text");
 }
   public static void Main(string[] args) 
   {
       Application.Run(new TextForm());
   }

}</source>

Password fields match

<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; partial class Form1 : Form {

   public Form1()
   {
       InitializeComponent();
   }
   private void button1_Click(object sender, EventArgs e)
   {
       if ( UserIsValid( password1.Text, password2.Text ))
       {
           MessageBox.Show("Congratulations - they match!");
       }
       else
       {
           MessageBox.Show("They don"t match - try again.");
       }
   }
   private bool UserIsValid(string firstPassword, string secondPassword)
   {
       return (firstPassword == secondPassword);
   }

} partial class Form1 {

   private void InitializeComponent()
   {
       this.label1 = new System.Windows.Forms.Label();
       this.label2 = new System.Windows.Forms.Label();
       this.password1 = new System.Windows.Forms.TextBox();
       this.password2 = new System.Windows.Forms.TextBox();
       this.button1 = new System.Windows.Forms.Button();
       this.SuspendLayout();
       this.label1.AutoSize = true;
       this.label1.Location = new System.Drawing.Point(13, 10);
       this.label1.Name = "label1";
       this.label1.Size = new System.Drawing.Size(83, 14);
       this.label1.TabIndex = 0;
       this.label1.Text = "Enter password";
       this.label2.AutoSize = true;
       this.label2.Location = new System.Drawing.Point(25, 37);
       this.label2.Name = "label2";
       this.label2.Size = new System.Drawing.Size(71, 14);
       this.label2.TabIndex = 1;
       this.label2.Text = "Enter it again";
       this.password1.Location = new System.Drawing.Point(103, 7);
       this.password1.Name = "password1";
       this.password1.Size = new System.Drawing.Size(182, 20);
       this.password1.TabIndex = 2;
       this.password2.Location = new System.Drawing.Point(103, 34);
       this.password2.Name = "password2";
       this.password2.Size = new System.Drawing.Size(182, 20);
       this.password2.TabIndex = 3;
       this.button1.Location = new System.Drawing.Point(210, 63);
       this.button1.Name = "button1";
       this.button1.TabIndex = 4;
       this.button1.Text = "OK";
       this.button1.Click += new System.EventHandler(this.button1_Click);
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(302, 98);
       this.Controls.Add(this.button1);
       this.Controls.Add(this.password2);
       this.Controls.Add(this.password1);
       this.Controls.Add(this.label2);
       this.Controls.Add(this.label1);
       this.Name = "Form1";
       this.Text = "Form1";
       this.ResumeLayout(false);
       this.PerformLayout();
   }
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Label label2;
   private System.Windows.Forms.TextBox password1;
   private System.Windows.Forms.TextBox password2;
   private System.Windows.Forms.Button button1;

} public class PasswordFieldCompare {

   [STAThread]
   static void Main()
   {
       Application.Run(new Form1());
   }

}</source>