PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/Forms/Forms/UserCredentials.cs

#
C# | 334 lines | 248 code | 8 blank | 78 comment | 12 complexity | 1aad4834eb994f03c4846b95ad2dcd65 MD5 | raw file
  1. // HSS.Forms.UserCredentials.cs
  2. // ----------------------------------------------------------------------------
  3. // Licensed under the MIT license
  4. // http://www.opensource.org/licenses/mit-license.html
  5. // ----------------------------------------------------------------------------
  6. // HighSpeed-Solutions, LLC
  7. // Copyright (c) 2001-2010
  8. // ----------------------------------------------------------------------------
  9. // File: UserCredentials.cs
  10. // Author: HSS\gbanta
  11. // Created: 08/12/2010
  12. // Modified: 12/23/2010
  13. // ----------------------------------------------------------------------------
  14. namespace HSS.Forms
  15. {
  16. #region Using Directives
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. #endregion
  22. #region UserCredentials
  23. /// <summary>
  24. /// A dialog for collecting user credentials. Username can
  25. /// be excluded and not shown. Includes an optional delegate
  26. /// to validate the supplied credentials.
  27. /// </summary>
  28. public sealed class UserCredentials : Form
  29. {
  30. #region Fields
  31. private System.Windows.Forms.Label lblUserName;
  32. private System.Windows.Forms.TextBox txtUserName;
  33. private System.Windows.Forms.TextBox txtPassword;
  34. private System.Windows.Forms.Label lblPassword;
  35. private System.Windows.Forms.TextBox txtConfirm;
  36. private System.Windows.Forms.Label lblConfirm;
  37. private System.Windows.Forms.Button btnOK;
  38. private System.Windows.Forms.Button btnCancel;
  39. private System.ComponentModel.IContainer components = null;
  40. private int standardHeight = 190;
  41. private int extendedHeight = 250;
  42. private bool isExtended = true;
  43. private Panel pnlCredentials;
  44. private Panel pnlUserName;
  45. private string exceptionDialogTitle = "User Credentials";
  46. #endregion
  47. #region Constructors
  48. /// <summary>
  49. /// Constructor
  50. /// </summary>
  51. public UserCredentials()
  52. {
  53. this.exceptionDialogTitle = Resources.UserCredentials_ExceptionDialogTitle;
  54. this.InitializeComponent();
  55. this.IsPasswordRequired = true;
  56. this.isExtended = true;
  57. this.Height = extendedHeight;
  58. this.lblUserName.Visible = true;
  59. this.txtUserName.Visible = true;
  60. }
  61. /// <summary>
  62. /// Constructor
  63. /// </summary>
  64. /// <param name="includeUserName">true to include username (default); false for password only.</param>
  65. public UserCredentials(bool includeUserName)
  66. {
  67. this.exceptionDialogTitle = Resources.UserCredentials_ExceptionDialogTitle;
  68. this.InitializeComponent();
  69. this.IsPasswordRequired = true;
  70. if (includeUserName)
  71. {
  72. this.Height = extendedHeight;
  73. this.isExtended = true;
  74. this.pnlUserName.Visible = true;
  75. this.lblUserName.Visible = true;
  76. this.txtUserName.Visible = true;
  77. }
  78. else
  79. {
  80. this.Height = standardHeight;
  81. this.isExtended = false;
  82. this.pnlUserName.Visible = false;
  83. this.lblUserName.Visible = false;
  84. this.txtUserName.Visible = false;
  85. }
  86. }
  87. #endregion
  88. #region Properties
  89. /// <summary>
  90. /// Gets or sets the UserName
  91. /// </summary>
  92. public string UserName
  93. {
  94. get { return this.txtUserName.Text; }
  95. set { this.txtUserName.Text = value; }
  96. }
  97. /// <summary>
  98. /// Gets or sets the Password
  99. /// </summary>
  100. public string Password
  101. {
  102. get { return this.txtPassword.Text; }
  103. set { this.txtPassword.Text = value; }
  104. }
  105. /// <summary>
  106. /// Gets or sets whether or not a password is required. Default: true.
  107. /// </summary>
  108. public bool IsPasswordRequired { get; set; }
  109. /// <summary>
  110. /// Gets or sets the optional Validation delegate.
  111. /// </summary>
  112. public Func<UserCredentials, bool> ValidateCredentials
  113. {
  114. get;
  115. set;
  116. }
  117. #endregion
  118. #region Overrides
  119. /// <summary>
  120. /// Clean up any resources being used.
  121. /// </summary>
  122. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  123. protected override void Dispose(bool disposing)
  124. {
  125. if (disposing && (components != null))
  126. {
  127. components.Dispose();
  128. }
  129. base.Dispose(disposing);
  130. }
  131. #endregion
  132. #region Methods
  133. /// <summary>
  134. /// Required method for Designer support - do not modify
  135. /// the contents of this method with the code editor.
  136. /// </summary>
  137. private void InitializeComponent()
  138. {
  139. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UserCredentials));
  140. this.lblUserName = new System.Windows.Forms.Label();
  141. this.txtUserName = new System.Windows.Forms.TextBox();
  142. this.txtPassword = new System.Windows.Forms.TextBox();
  143. this.lblPassword = new System.Windows.Forms.Label();
  144. this.txtConfirm = new System.Windows.Forms.TextBox();
  145. this.lblConfirm = new System.Windows.Forms.Label();
  146. this.btnOK = new System.Windows.Forms.Button();
  147. this.btnCancel = new System.Windows.Forms.Button();
  148. this.pnlCredentials = new System.Windows.Forms.Panel();
  149. this.pnlUserName = new System.Windows.Forms.Panel();
  150. this.pnlCredentials.SuspendLayout();
  151. this.pnlUserName.SuspendLayout();
  152. this.SuspendLayout();
  153. //
  154. // lblUserName
  155. //
  156. this.lblUserName.AutoSize = true;
  157. this.lblUserName.Location = new System.Drawing.Point(12, 9);
  158. this.lblUserName.Name = "lblUserName";
  159. this.lblUserName.Size = new System.Drawing.Size(68, 16);
  160. this.lblUserName.TabIndex = 0;
  161. this.lblUserName.Text = "User Name:";
  162. //
  163. // txtUserName
  164. //
  165. this.txtUserName.Location = new System.Drawing.Point(15, 28);
  166. this.txtUserName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
  167. this.txtUserName.Name = "txtUserName";
  168. this.txtUserName.Size = new System.Drawing.Size(360, 22);
  169. this.txtUserName.TabIndex = 1;
  170. //
  171. // txtPassword
  172. //
  173. this.txtPassword.Location = new System.Drawing.Point(15, 28);
  174. this.txtPassword.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
  175. this.txtPassword.Name = "txtPassword";
  176. this.txtPassword.Size = new System.Drawing.Size(360, 22);
  177. this.txtPassword.TabIndex = 3;
  178. this.txtPassword.UseSystemPasswordChar = true;
  179. //
  180. // lblPassword
  181. //
  182. this.lblPassword.AutoSize = true;
  183. this.lblPassword.Location = new System.Drawing.Point(12, 8);
  184. this.lblPassword.Name = "lblPassword";
  185. this.lblPassword.Size = new System.Drawing.Size(61, 16);
  186. this.lblPassword.TabIndex = 2;
  187. this.lblPassword.Text = "Password:";
  188. //
  189. // txtConfirm
  190. //
  191. this.txtConfirm.Location = new System.Drawing.Point(15, 83);
  192. this.txtConfirm.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
  193. this.txtConfirm.Name = "txtConfirm";
  194. this.txtConfirm.Size = new System.Drawing.Size(360, 22);
  195. this.txtConfirm.TabIndex = 6;
  196. this.txtConfirm.UseSystemPasswordChar = true;
  197. //
  198. // lblConfirm
  199. //
  200. this.lblConfirm.AutoSize = true;
  201. this.lblConfirm.Location = new System.Drawing.Point(12, 63);
  202. this.lblConfirm.Name = "lblConfirm";
  203. this.lblConfirm.Size = new System.Drawing.Size(106, 16);
  204. this.lblConfirm.TabIndex = 5;
  205. this.lblConfirm.Text = "Confirm Password:";
  206. //
  207. // btnOK
  208. //
  209. this.btnOK.Location = new System.Drawing.Point(169, 122);
  210. this.btnOK.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
  211. this.btnOK.Name = "btnOK";
  212. this.btnOK.Size = new System.Drawing.Size(100, 30);
  213. this.btnOK.TabIndex = 7;
  214. this.btnOK.Text = "&OK";
  215. this.btnOK.UseVisualStyleBackColor = true;
  216. this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
  217. //
  218. // btnCancel
  219. //
  220. this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  221. this.btnCancel.Location = new System.Drawing.Point(279, 122);
  222. this.btnCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
  223. this.btnCancel.Name = "btnCancel";
  224. this.btnCancel.Size = new System.Drawing.Size(100, 30);
  225. this.btnCancel.TabIndex = 8;
  226. this.btnCancel.Text = "&Cancel";
  227. this.btnCancel.UseVisualStyleBackColor = true;
  228. this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  229. //
  230. // pnlCredentials
  231. //
  232. this.pnlCredentials.Controls.Add(this.lblPassword);
  233. this.pnlCredentials.Controls.Add(this.txtPassword);
  234. this.pnlCredentials.Controls.Add(this.lblConfirm);
  235. this.pnlCredentials.Controls.Add(this.txtConfirm);
  236. this.pnlCredentials.Controls.Add(this.btnOK);
  237. this.pnlCredentials.Controls.Add(this.btnCancel);
  238. this.pnlCredentials.Dock = System.Windows.Forms.DockStyle.Fill;
  239. this.pnlCredentials.Location = new System.Drawing.Point(0, 53);
  240. this.pnlCredentials.Name = "pnlCredentials";
  241. this.pnlCredentials.Size = new System.Drawing.Size(394, 169);
  242. this.pnlCredentials.TabIndex = 9;
  243. //
  244. // pnlUserName
  245. //
  246. this.pnlUserName.Controls.Add(this.txtUserName);
  247. this.pnlUserName.Controls.Add(this.lblUserName);
  248. this.pnlUserName.Dock = System.Windows.Forms.DockStyle.Top;
  249. this.pnlUserName.Location = new System.Drawing.Point(0, 0);
  250. this.pnlUserName.Name = "pnlUserName";
  251. this.pnlUserName.Size = new System.Drawing.Size(394, 53);
  252. this.pnlUserName.TabIndex = 0;
  253. //
  254. // UserCredentials
  255. //
  256. this.AcceptButton = this.btnOK;
  257. this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 16F);
  258. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  259. this.CancelButton = this.btnCancel;
  260. this.ClientSize = new System.Drawing.Size(394, 222);
  261. this.Controls.Add(this.pnlCredentials);
  262. this.Controls.Add(this.pnlUserName);
  263. this.Font = new System.Drawing.Font("Microsoft YaHei", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  264. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  265. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  266. this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
  267. this.MaximizeBox = false;
  268. this.MinimizeBox = false;
  269. this.Name = "UserCredentials";
  270. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  271. this.Text = "Set Credentials";
  272. this.pnlCredentials.ResumeLayout(false);
  273. this.pnlCredentials.PerformLayout();
  274. this.pnlUserName.ResumeLayout(false);
  275. this.pnlUserName.PerformLayout();
  276. this.ResumeLayout(false);
  277. }
  278. #endregion
  279. #region Event Handlers
  280. private void btnOK_Click(object sender, EventArgs e)
  281. {
  282. if (this.isExtended && string.IsNullOrEmpty(this.txtUserName.Text))
  283. {
  284. MessageBox.Show(this, Resources.UserCredentials_UserNameRequired, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  285. this.txtUserName.Focus();
  286. return;
  287. }
  288. if (this.IsPasswordRequired && string.IsNullOrEmpty(this.txtPassword.Text))
  289. {
  290. MessageBox.Show(this, Resources.UserCredentials_PasswordRequired, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  291. this.txtConfirm.ResetText();
  292. this.txtPassword.ResetText();
  293. this.txtPassword.Focus();
  294. return;
  295. }
  296. if (!string.Equals(this.txtPassword.Text, this.txtConfirm.Text, StringComparison.CurrentCulture))
  297. {
  298. MessageBox.Show(this, Resources.UserCredentials_PasswordDoNotMatch, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  299. this.txtConfirm.ResetText();
  300. this.txtPassword.ResetText();
  301. this.txtPassword.Focus();
  302. return;
  303. }
  304. if (null != this.ValidateCredentials)
  305. {
  306. if (!this.ValidateCredentials(this))
  307. {
  308. MessageBox.Show(this, Resources.UserCredentials_CredentialsNotValid, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  309. this.txtConfirm.ResetText();
  310. this.txtPassword.ResetText();
  311. this.txtPassword.Focus();
  312. return;
  313. }
  314. }
  315. this.DialogResult = DialogResult.OK;
  316. this.Close();
  317. }
  318. private void btnCancel_Click(object sender, EventArgs e)
  319. {
  320. this.DialogResult = DialogResult.Cancel;
  321. this.Close();
  322. }
  323. #endregion
  324. }
  325. #endregion
  326. }