创建密码箱控件 [英] Creating a Password Box Control

查看:50
本文介绍了创建密码箱控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义控件,其功能类似于文本框,但不显示文本,而是显示星号。它仍然跟踪输入的文本只是不显示它。首先,这是我的班级:



I am attempting to make a custom control that functions like a textbox except that instead of displaying the text, it displays star characters. It still keeps track of the text entered just doesn't display it. First off, here is my class:

public partial class PasswordBox : TextBox
	{
		private string _password;

		public string Password { get { return _password; } }

		public PasswordBox ()
		{
			InitializeComponent();
			_password = "";
		}

		protected override void OnTextChanged (EventArgs e)
		{
			base.Text = new string('*', _password.Length);
			this.Select(this.Text.Length, 0);
			base.OnTextChanged(e);
		}

		protected override void OnPreviewKeyDown (PreviewKeyDownEventArgs e)
		{
			if( e.KeyCode == Keys.Back )
			{
				int start = this.SelectionStart;
				int length = this.SelectionLength;
				if( length > 0 )
				{
					_password = _password.Remove(start, length);
				}
				else if( _password.Length > 0 )
				{
					_password = _password.Substring(0, start - 1) + _password.Substring(start);
				}
			}
			else if( e.KeyCode != Keys.Enter )
			{
				if( e.Shift.False() )
				{
					_password += ((char)e.KeyValue).ToString().ToLower()[0];
				}
				else
				{
					MatchCollection matches = RegexPatterns.GetMatches(((char)e.KeyValue).ToString(),"[A-Za-z0-9!@#$%^&*()_+=\\:;\"'<,>\\[\\]{}.?/-]");
					if( matches.Count > 0 )
					{
						_password += matches[0].Value;
					}
				}
			}
			base.OnPreviewKeyDown(e);
		}
	}





为了解释其中的一部分,密码存储在一个也可以由属性访问的全局变量中。当文本改变时,将显示由星组成的字符串。与密码变量字符串相同的长度。



我也在处理使用shift键与常规按键分开,因为我不知道如何获得小写值,因为在获取KeyValue的字符值时,按下shift键时总是会有额外的字符,因此RegEx测试。



我遇到的问题是退格功能。如果您没有选择任何内容并按下退格键,它可以正常工作但是当我有一个选择并按退格键时,应该从密码字段中删除的空间将填充无关的字符,如百分号,双引号,和单引号。我必须具备此功能,以便您需要并获得知识渊博的帮助。



To explain some of it, the password is stored in a global variable that can also accessed by a property. When the text changes, a string consisting of stars will be displayed of the same length as the password variable string.

I am also handling the use of the shift key separately from a regular key press because I didn't know how else to get the lowercase value and because when getting the character value of the KeyValue, it would always have extra characters when the shift key was pressed, hence the RegEx test.

The issue I am having is with the backspace functionality. It works just fine if you have nothing selected and hit the backspace key but when I have a selection and press the backspace key the space that was supposed to be removed from the password field gets filled with extraneous characters such as percent signs, double quotes, and single quotes. I must have this functionality so your knowledgeable help is needed and appreciated.

推荐答案

%^& *()_ + = \\:; \ <,> \\ [\\] {} / - )。?;
if (matches.Count > 0
{
_password + =匹配[ 0 ]。
}
}
}
base .OnPreviewKeyDown(e);
}
}
%^&*()_+=\\:;\"'<,>\\[\\]{}.?/-]"); if( matches.Count > 0 ) { _password += matches[0].Value; } } } base.OnPreviewKeyDown(e); } }





为了解释其中一些,密码存储在一个全局变量中,也可以由酒店访问。当文本改变时,由星号组成的字符串将显示与密码变量字符串相同的长度。



我也分别处理使用shift键来自常规按键,因为我不知道如何获得小写值,因为当获取KeyValue的字符值时,按下shift键时总是会有额外的字符,因此RegEx测试。



我遇到的问题是退格功能。如果您没有选择任何内容并按下退格键,它可以正常工作但是当我有一个选择并按退格键时,应该从密码字段中删除的空间将填充无关的字符,如百分号,双引号,和单引号。我必须拥有这个功能,所以需要你的知识渊博的帮助。



To explain some of it, the password is stored in a global variable that can also accessed by a property. When the text changes, a string consisting of stars will be displayed of the same length as the password variable string.

I am also handling the use of the shift key separately from a regular key press because I didn't know how else to get the lowercase value and because when getting the character value of the KeyValue, it would always have extra characters when the shift key was pressed, hence the RegEx test.

The issue I am having is with the backspace functionality. It works just fine if you have nothing selected and hit the backspace key but when I have a selection and press the backspace key the space that was supposed to be removed from the password field gets filled with extraneous characters such as percent signs, double quotes, and single quotes. I must have this functionality so your knowledgeable help is needed and appreciated.


非常简短的答案:



Really short answer:

passwordtextbox.PasswordChar = '*'





还需要做什么?你使用过文本框的密码功能吗?



What else does it need to do? have you used the password features of the textbox?


这篇关于创建密码箱控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆