关于C#自定义控件

rcy5211314 2010-08-20 02:19:10
今天使用系统的修改密码时,发现window7的修改密码文本框里边的提示文本,自己没什么事也做了一个,
其它没有什么,只是在为passwordChar附上值的时候,就不行了,大给给想想办法。

我知啊窗体的那控件的TextChanged写上以下在,是可以实现的
private void suggestiveTextBox1_TextChanged(object sender, EventArgs e)
{
if (suggestiveTextBox1.Text==suggestiveTextBox1.SuggestiveText)
{
suggestiveTextBox1.PasswordChar = new char();
}
else
{
suggestiveTextBox1.PasswordChar = '*';
}
}

但我觉得这样不好,请大家帮忙想想怎么在设计控件时就实现这个功能,只要是不知道用户是否给passwordchar赋值没有???

下边是全部代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SuggestiveTextBox
{
[ToolboxBitmap(typeof(TextBox))]
public partial class SuggestiveTextBox : TextBox
{
public SuggestiveTextBox()
{
InitializeComponent();
this.Text = SuggestiveText;
InputColor = Color.Black;
SugestiveColor = Color.DimGray;
this.TextChanged += new EventHandler(SuggestiveTextBox_TextChanged);
this.MouseLeave += new EventHandler(SuggestiveTextBox_MouseLeave);
this.Click += new EventHandler(SuggestiveTextBox_Click);
this.KeyDown += new KeyEventHandler(SuggestiveTextBox_KeyDown);
this.Enter += new EventHandler(SuggestiveTextBox_Enter);

// password = PasswordChar;
}
//private char password;
//判断是否为用户的操作 如鼠标单击,或键盘按下
private bool isUserInfo = false;
/// <summary>
/// 获取光标事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SuggestiveTextBox_Enter(object sender, EventArgs e)
{
if (this.Text == "")//如果没有值则显示提示文本
{
this.Text = SuggestiveText;
this.ForeColor = SugestiveColor;//前景色为提示文本颜色
this.Select(0, 0);//不选中
}

}
/// <summary>
/// 按下键盘时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SuggestiveTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (this.Text == SuggestiveText)//当为提示文本时 设置前景色为输入文本颜色 并清空
{
isUserInfo = true;//标记为自己输入
this.ForeColor = InputColor;
this.Text = "";

}
else//只设前景色为输入文本
{
this.ForeColor = InputColor;
isUserInfo = false;
}
}
/// <summary>
/// 鼠标单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SuggestiveTextBox_Click(object sender, EventArgs e)
{
if (this.Text == SuggestiveText)//单击是清空文本
{
isUserInfo = true;
this.Select(0, 0);
this.Text = "";
}
else
{
isUserInfo = false;
}
}
/// <summary>
/// 鼠标离开时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SuggestiveTextBox_MouseLeave(object sender, EventArgs e)
{
if (this.Text == "")//如果为空则显示提示文本
{

this.Text = SuggestiveText;
this.ForeColor = SugestiveColor;
this.Focus();
}
}

/// <summary>
/// 值改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SuggestiveTextBox_TextChanged(object sender, EventArgs e)
{
if (this.Text == "" && isUserInfo == false)//当为空时并且有用户删除导致时
{
this.Text = SuggestiveText;//提示文本
this.ForeColor = SugestiveColor;//提示文本颜色
}
if (this.Text ==SuggestiveText)//当为提示文本时
{
this.ForeColor = SugestiveColor;
this.Select(0, 0);//设置不被选中
//this.PasswordChar = new char();
}
else//为其它时 设置前景色为 输入文本颜色
{
this.ForeColor = InputColor;
// this.PasswordChar = '*';
}
}
//属性

private string sugestiveText;
[Description("提示文本")]
public string SuggestiveText
{
get { return sugestiveText; }
set { sugestiveText = value;
this.Text = sugestiveText;
}
}

private Color sugestiveColor;
[Description("提示文本颜色")]
public Color SugestiveColor
{
get { return sugestiveColor; }
set { sugestiveColor = value; }
}

private Color inputColor;
[Description("输入文本颜色")]
public Color InputColor
{
get { return inputColor; }
set { inputColor = value; }
}

}
}
...全文
205 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rcy5211314 2010-08-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 starts_2000 的回复:]
参考
[/Quote]

很感谢你的这个参考,但我不喜欢这画上去的感觉,我就喜欢我这种 估计有些落后了吧
rcy5211314 2010-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 anqanq000 的回复:]
每次显示提示文本之前都先判断一下passwordChar属性不可以么。如果设置就将其清除,并记录设置的passwardChar值,在下次text属性改变时(按下键盘事件或其他)再将passwardChar属性还原。

还有,从你的代码来看,如果用户输入的文本和你的提示文本相同的话,输入时无效的。
[/Quote]

这个是肯定的不过我觉得这点不怎么重要,只要能实现知道用户使用密码就行

rcy5211314 2010-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 anqanq000 的回复:]
每次显示提示文本之前都先判断一下passwordChar属性不可以么。如果设置就将其清除,并记录设置的passwardChar值,在下次text属性改变时(按下键盘事件或其他)再将passwardChar属性还原。

还有,从你的代码来看,如果用户输入的文本和你的提示文本相同的话,输入时无效的。
[/Quote]

这个是肯定的不过我觉得这点不怎么重要,只要能实现知道用户使用密码就行

allen3010 2010-08-20
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分
rinoya111 2010-08-20
  • 打赏
  • 举报
回复
学习.
starts_2000 2010-08-20
  • 打赏
  • 举报
回复
mevender 2010-08-20
  • 打赏
  • 举报
回复
每次显示提示文本之前都先判断一下passwordChar属性不可以么。如果设置就将其清除,并记录设置的passwardChar值,在下次text属性改变时(按下键盘事件或其他)再将passwardChar属性还原。

还有,从你的代码来看,如果用户输入的文本和你的提示文本相同的话,输入时无效的。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧