我正在开发一个涉及图书馆管理系统的项目,我使用一个名为" PlaceholderTextBox"的定制类。充当带有占位符文本的TextBox:
internal class PlaceholderTextBox : TextBox
{
private bool isPlaceHolder = true;
private string _PlaceholderText;
private bool isPassword;
public string PlacehoderText
{
get { return _PlaceholderText; }
set { _PlaceholderText = value; setPlaceholder(); }
}
public bool IsPassword
{
get { return isPassword; }
set { isPassword = value; }
}
private void setPlaceholder()
{
if (string.IsNullOrEmpty(this.Text))
{
this.Text = PlacehoderText;
this.ForeColor = System.Drawing.Color.Gray;
isPlaceHolder = true;
if (isPassword) this.UseSystemPasswordChar = false;
}
}
private void removePlaceHolder()
{
if (isPlaceHolder)
{
if (isPassword) this.UseSystemPasswordChar = true;
this.Text = "";
this.ForeColor = System.Drawing.SystemColors.WindowText;
isPlaceHolder = false;
}
}
public PlaceholderTextBox()
{
GotFocus += removePlaceHolder;
LostFocus += setPlaceholder;
}
private void setPlaceholder(object sender, EventArgs e)
{
setPlaceholder();
}
private void removePlaceHolder(object sender, EventArgs e)
{
removePlaceHolder();
}
}
如您所见,此类是TextBox类的扩展版本,它可以处理占位符文本,以及密码项目符号,以备我需要时使用。
问题出现在void setPlaceholder()函数上,在线" if(isPassword)this.UseSystemPasswordChar = false;"这让我觉得" Win32Exception"哪些状态"创建窗口句柄时出错"每当我尝试按下文本框时,占位符文本应替换为应该带项目符号的实际密码文本。
关于程序为什么会抛出这个异常的任何想法,以及我如何解决这个问题的想法?
非常感谢。