简易密码输入自定义控件示例 含自定义事件 可在调用窗口事件视图中看到事件名
2012-08-28 17:28
507 查看
public partial class MyCheckBox : UserControl
{
string text=string.Empty;
bool bAutoSrcoll=true;
Size sSize;
public MyCheckBox()
{
InitializeComponent();
textBox1.TextChanged +=new EventHandler(Right_Click);
}
[Description("文本框内输入的内容")]
public string MyText
{
get { return text; }
set
{
textBox1.Text = value;
text = value;
}
}
[Description("光标跟随编辑位置")]
public bool AutoSrcoll
{
get { return bAutoSrcoll; }
set
{
bAutoSrcoll = value;
}
}
// [Description("文本更改")]
public delegate void RTextChangedEventHandler(object sender, RTextChangedEventArgs e);//事件所需的委托
//当颜色改变时触发事件
public event RTextChangedEventHandler RTextChanged;//定义一个ColorChanged事件
protected virtual void OnTextChanged(RTextChangedEventArgs e)
{//事件触发方法
if (RTextChanged != null)
{//判断事件是否为空
RTextChanged(this, e);//触发事件
}
}
public class RTextChangedEventArgs : EventArgs
{
public string str;
public RTextChangedEventArgs()
{
str = "right";
}
}
private void Right_Click(object sender, EventArgs e)
{
RTextChanged(this, new RTextChangedEventArgs());
}
private void SetSign(int Position)
{
string temp = string.Empty;
for (int i = 0; i < textBox1.Text.Length; i++)
{
temp += @"*";
}
textBox1.Text = temp;
if (bAutoSrcoll == true)
{
textBox1.Select(Position + 1, 0);
}
else
{
textBox1.Select(textBox1.Text.Length, 0);
}
textBox1.ScrollToCaret();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (MyText.Length == textBox1.Text.Length) //显示回显
{
string tmp = textBox1.Text.Replace(@"*", "");
if (tmp.Trim().Length != 0)
{
SetSign(textBox1.Text.Length);
}
return;
}
//输入空,不需回显
if (string.IsNullOrEmpty(textBox1.Text) == true)
{
text = string.Empty;
return;
}
string input = textBox1.Text.Replace(@"*", "");
if (input == string.Empty)//输入*号,不需回显
{
text = text + @"*";
return;
}
else
{
int inputset = textBox1.Text.LastIndexOf(input);
if (inputset == text.Length)//追加
{
text += input;
}
else//插入
text = text.Insert(inputset, input);
SetSign(inputset);
}
}
}
{
string text=string.Empty;
bool bAutoSrcoll=true;
Size sSize;
public MyCheckBox()
{
InitializeComponent();
textBox1.TextChanged +=new EventHandler(Right_Click);
}
[Description("文本框内输入的内容")]
public string MyText
{
get { return text; }
set
{
textBox1.Text = value;
text = value;
}
}
[Description("光标跟随编辑位置")]
public bool AutoSrcoll
{
get { return bAutoSrcoll; }
set
{
bAutoSrcoll = value;
}
}
// [Description("文本更改")]
public delegate void RTextChangedEventHandler(object sender, RTextChangedEventArgs e);//事件所需的委托
//当颜色改变时触发事件
public event RTextChangedEventHandler RTextChanged;//定义一个ColorChanged事件
protected virtual void OnTextChanged(RTextChangedEventArgs e)
{//事件触发方法
if (RTextChanged != null)
{//判断事件是否为空
RTextChanged(this, e);//触发事件
}
}
public class RTextChangedEventArgs : EventArgs
{
public string str;
public RTextChangedEventArgs()
{
str = "right";
}
}
private void Right_Click(object sender, EventArgs e)
{
RTextChanged(this, new RTextChangedEventArgs());
}
private void SetSign(int Position)
{
string temp = string.Empty;
for (int i = 0; i < textBox1.Text.Length; i++)
{
temp += @"*";
}
textBox1.Text = temp;
if (bAutoSrcoll == true)
{
textBox1.Select(Position + 1, 0);
}
else
{
textBox1.Select(textBox1.Text.Length, 0);
}
textBox1.ScrollToCaret();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (MyText.Length == textBox1.Text.Length) //显示回显
{
string tmp = textBox1.Text.Replace(@"*", "");
if (tmp.Trim().Length != 0)
{
SetSign(textBox1.Text.Length);
}
return;
}
//输入空,不需回显
if (string.IsNullOrEmpty(textBox1.Text) == true)
{
text = string.Empty;
return;
}
string input = textBox1.Text.Replace(@"*", "");
if (input == string.Empty)//输入*号,不需回显
{
text = text + @"*";
return;
}
else
{
int inputset = textBox1.Text.LastIndexOf(input);
if (inputset == text.Length)//追加
{
text += input;
}
else//插入
text = text.Insert(inputset, input);
SetSign(inputset);
}
}
}
相关文章推荐
- jQuery的控件及事件(输入控件及回车事件)使用示例
- 控件:UIControl 我在UITextFiled输入事件调用到
- Android自定义类似支付宝密码输入的控件
- iOS无法使用自定义密码输入控件(自带键盘)
- 控件视图Android 自定义视图容器控件拦截ontouch事件传递给其子控件
- js实现屏蔽默认快捷键调用自定义事件示例
- Android自定义类似支付宝密码输入的控件
- 水印控件windows phone中,制作一个自定义的密码输入框控件,含图片,有水印,星号显示
- C# winform中自定义用户控件 然后在页面中调用用户控件的事件
- 自定义view,仿微信、支付宝密码输入控件的源码实现
- android自定义密码输入键盘控件
- 同一个窗口的控件之间调用事件,就这么简单
- js实现屏蔽默认快捷键调用自定义事件示例
- Android 自定义视图容器控件拦截ontouch事件传递给其子控件
- MFC模态对话框和非模态对话框及副窗口调用主窗口控件 自定义消息发送
- 自定义视图窗口控件
- 自定义View之自定义支付宝密码输入控件
- ExtJS中表单验证使用自定义vtype(两次输入密码重复)示例
- VC++单文档分割窗口、在视图中添加控件,并响应该控件的事件