【发布时间】:2014-06-11 08:31:12
【问题描述】:
我正在使用 c# winforms (.NET 4,0),我想创建一个“智能”密码文本框类(或 UserControl),它会在一段时间内显示输入的字符,然后屏蔽该字符。我看了这篇文章:Create a textbox with "smart" password char 并且该解决方案效果很好,但在 Form 类中完成。我希望所有功能都在一个类或用户控件中,以便可以简单地将其拖放到表单上。
我的班级使用上面提到的解决方案:
using System;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class SmartTextBox : TextBox
{
public SmartTextBox()
{
InitializeComponent();
}
#region Component Designer generated code
private void InitializeComponent()
{
//
// SmartTextBox
//
this.TextChanged += new EventHandler(SmartTextBox_TextChanged);
}
#endregion
System.Threading.Timer timer = null;
void SmartTextBox_TextChanged(object sender, EventArgs e)
{
base.OnTextChanged(e);
if (timer == null)
{
timer = new System.Threading.Timer(new TimerCallback(Do), null, 1000, 1000);
}
SmartTextBox tb = this as SmartTextBox;
int num = tb.Text.Length;
if (num > 1)
{
StringBuilder s = new StringBuilder(tb.Text);
s[num - 2] = '*';
tb.Text = s.ToString();
tb.SelectionStart = num;
//Debug.WriteLine("TextChanged: " + tb.Text);
}
}
public void Do(object state)
{
if (this.InvokeRequired)
{
int num = this.Text.Length;
if (num > 0)
{
StringBuilder s = new StringBuilder(this.Text);
s[num - 1] = '*';
this.Invoke(new Action(() => // <----Error on this line
{
this.Text = s.ToString();
this.SelectionStart = this.Text.Length;
timer.Dispose();
timer = null;
}));
}
}
}
}
但是当我尝试编译时,出现以下错误: 错误 CS0305:使用泛型类型“System.Action”需要 1 个类型参数
我不确定如何解决此错误,我们将不胜感激。
【问题讨论】:
-
你确定你的目标是 .NET 4 吗?见这里:stackoverflow.com/questions/8263626/…
-
好吧,我现在不觉得自己像个白痴。很好,我将它设置为 .NET4 并进行了一些其他更改,它可以按预期编译和工作。感谢您的帮助。
-
@JonB 你能把它作为答案,以便我们可以从开放列表中删除这个问题吗?
-
@NickUdell 让我们将其标记为骗子。
标签: c# winforms textbox passwords