日期:2014-05-18 浏览次数:20816 次
private TextBox tb; //定义一个动态的textbox
public delegate void onkeydowndelegate(object sender,KeyEventArgs e);
public event onkeydowndelegate keydown; //动态控件的keydown事件
private void Form1_Load(object sender, System.EventArgs e) //load的时候创建该控件并隐藏
{
tb = new TextBox();
tb.Visible = false;
}
public void onkeydown(object sender, KeyEventArgs e)// 动态控件按回车空格就复制给textbox1
{
if (e.KeyValue == 13 )
{
textBox1.Text =textBox1.Text + tb.Text;
textBox1.Invalidate(true);
tb.Text = "";
tb.Visible = false;
textBox1.SelectionStart = textBox1.Text.Length;
}
}
//textbox1里面按下去(我的第一句是判断是否模拟输入法用的,可以去掉不管)
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode.ToString().ToLower() == "processkey" && checkBox1.Checked)
{
//下面开始动态显示tb位置
tb.Width = 22;
tb.Height = 21;
tb.BackColor = Color.Yellow; //Color.Gainsboro;
tb.MaxLength = 1;
tb.Top = 10;
tb.Left = 10;
tb.BorderStyle = BorderStyle.None;
tb.KeyDown += new KeyEventHandler(onkeydown);
tb.Parent = this;
tb.BringToFront();
tb.Visible = true;
tb.Focus();
tb.Text = "";
}
//上面这段代码执行并按'a'后,显示了tb,并且确实把'a'传到tb中(不过没显示出来,怎么办?再按个b才能显示全部)
//另外,按'a'后,textbox1中是不要显示的,改怎么处理(注: 用length - (length-1)的方法不要)
}
}