110,570
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 用于记录密码
/// </summary>
private StringBuilder StrPassword = new StringBuilder();
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (password.Text.Length > 0)
{
if (e.KeyValue == 46)
{
if (password.SelectionStart >= password.Text.Length)
{
return;
}
else
{
isDel = true;
if (password.SelectionStart != 0)
{
mouseStart = password.SelectionStart;
}
else
{
mouseStart = 0;
}
int leg = password.SelectionLength == 0 ? 1 : password.SelectionLength;
try
{
password.Text = password.Text.Remove(mouseStart, leg - 1);
StrPassword.Remove(mouseStart, leg);
}
catch
{
}
password.SelectionStart = mouseStart;
}
}
else
{
if ((int)e.KeyValue == 22 || e.KeyValue == 24 || e.KeyValue == 3 && e.KeyValue == 8 && e.KeyValue == 13)
{ }
else if (password.SelectionLength > 0)
{
isResetPWD = true;
resetPWD = password.SelectionLength;
mouseStart = password.SelectionStart;
StrPassword.Remove(mouseStart, resetPWD);
}
isDel = false;
}
}
}
bool isResetPWD = false;//选择部分密码时输入密码
bool isDel = false;
int mouseStart = 0;
int resetPWD = 0; //选中部分密码时选中的长度
bool cando = true;
bool isback = false; //是否按的是后退
private void password_KeyPress(object sender, KeyPressEventArgs e)
{
if (password.SelectionStart == password.Text.Length)
{
cando = true;
}
if ((int)e.KeyChar == 22 || e.KeyChar == 24 || e.KeyChar == 3 && e.KeyChar == 8 && e.KeyChar == 13)
{
e.Handled = true;
cando = false;
}
if (e.KeyChar == 8)
{
if (password.SelectionStart == 0 && password.SelectionLength == 0)
{
return;
}
else
{
isback = true;
mouseStart = password.SelectionStart;
try
{
if (password.SelectionLength == 0)
{
password.Text = password.Text.Remove(mouseStart - 1, 1);
StrPassword.Remove(mouseStart - 1, 1);
password.SelectionStart = mouseStart - 1;
}
else
{
password.Text = password.Text.Remove(mouseStart, password.SelectionLength);
StrPassword.Remove(mouseStart, password.SelectionLength);
password.SelectionStart = mouseStart;
}
}
catch
{
}
}
}
else
{
isback = false;
}
}
private void password_KeyUp(object sender, KeyEventArgs e)
{
if (password.Text == "")
{
StrPassword.Length = 0;
}
}
private void password_TextChanged(object sender, EventArgs e)
{
if (!cando)
{
return;
}
if (password.Text.Trim().Length > 0 && !isback && !isDel)
{
isback = false;
isDel = false;
if (isResetPWD)
{
string ss = password.Text.Substring(mouseStart, 1);
if (ss != "*")
{
string endstr = StrPassword.ToString().Insert(mouseStart, ss);
StrPassword = new StringBuilder(endstr);
}
if (password.Text.Length > 1)
{
string sstart = password.Text.Substring(0, 1);
if (sstart != "*")
{
StrPassword.Append(ss);
password.Text = password.Text.Trim().Replace(sstart, "*");
}
}
isResetPWD = false;
}
string s = password.Text.Substring(password.Text.Length - 1, 1);
if (s != "*")
StrPassword.Append(s);
if (password.Text.Length > 1)
{
string sstart = password.Text.Substring(0, 1);
if (sstart != "*")
{
StrPassword.Append(s);
password.Text = password.Text.Trim().Replace(sstart, "*");
}
}
password.Text = password.Text.Trim().Replace(s, "*");
password.SelectionStart = password.Text.Length;
}
}
StringBuilder sb = new StringBuilder();
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (textBox1.Text.Trim().Length > 0)
{
string s=textBox1.Text.Substring(textBox1.Text.Length-1,1);
sb.Append(s);
textBox1.Text=textBox1.Text.Trim().Replace(s,"*");
textBox1.SelectionStart = textBox1.Text.Length;
}
}
public class MyTextBox : TextBox
{
private Timer _Timer;
public MyTextBox()
{
this.KeyUp += TextBoxKeyUp;
_Timer = new Timer();
_Timer.Tick += Tick;
_Timer.Interval = 200;
}
private void TextBoxKeyUp(object sender, EventArgs e)
{
_Timer.Start();
}
private void Tick(object sender, EventArgs e)
{
_Timer.Stop();
Text = new String('*', Text.Length);
this.SelectionStart = Text.Length;
}
}
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
if (textBox3.Tag == null)
{
textBox3.Tag = e.KeyCode.ToString();
}
else
{
string str = "";
str = str.PadLeft(textBox3.Tag.ToString().Length, '*') + e.KeyCode.ToString();
textBox3.Text = str;
textBox3.Tag = textBox3.Tag.ToString() + e.KeyCode.ToString();
}
}