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);
}
}
}
本文介绍了一个自定义文本框组件,包含属性设置、事件处理及文本改变时的响应机制。
439

被折叠的 条评论
为什么被折叠?



