winform中有多个TextBox,当光标停留在某个TextBox时,点击按钮,则会在此TextBox中插入字符,该怎么实现呢,谢谢!

解决方案 »

  1.   

    TextBox  enter事件用变量记下当前的TextBox
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

    补充一下楼上的
    适用与在textbox本身有文本需要在光标处插入字符
    TextBox  enter事件用变量记下当前的TextBox ,然后TextBox.Focus();SendKeys.Send("字符");
      

  3.   

    我用个笨办法解决的不知道是不是你说的那个意思public partial class Form1 : Form
        {
            string textBoxName;
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void textBox1_Click(object sender, EventArgs e)
            {
                textBoxName = textBox1.Name;
            }        private void textBox2_Click(object sender, EventArgs e)
            {
                textBoxName = textBox2.Name;
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (textBoxName == this.textBox1.Name)
                {
                    this.textBox1.Text = "要显示的字符";
                    this.textBox2.Text = "";
                }
                else if (textBoxName == this.textBox2.Name)
                {
                    this.textBox1.Text = "";
                    this.textBox2.Text = "要显示的字符";
                }
            }
        }
    先设置一个全局变量,存储获得焦点文本框的Name,点击按钮的时候判断是哪个文本框获得焦点了
      

  4.   

    也可以这样获得控件名称哈
    public partial class Form1 : Form
        {
            string textBoxName;
            public Form1()
            {
                InitializeComponent();
                this.textBox1.Click += new System.EventHandler(this.Control_Click);
                this.textBox2.Click += new System.EventHandler(this.Control_Click);
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void Control_Click(object sender, System.EventArgs e)
            {
                Control control = (Control)sender;
                textBoxName = control.Name;
            }           private void button1_Click(object sender, EventArgs e)
            {
                if (textBoxName == this.textBox1.Name)
                {
                    this.textBox1.Text = "show";
                    this.textBox2.Text = "";
                }
                else if (textBoxName == this.textBox2.Name)
                {
                    this.textBox1.Text = "";
                    this.textBox2.Text = "show";
                }
            }
        }
    原理一样
      

  5.   

          Text1.Text = Text1.Text.Insert(Text1.SelectionStart, "abcd")
      

  6.   

            private void textBox1_Enter(object sender, EventArgs e)
            {
                textBox1.Text = "数据";
            }