C# textbox光标定位与字符插入

C# textbox光标定位与字符插入

(2013-04-17 14:19:26)
  分类:cs程序

我们经常使用textbox作为文字录入界面,下面时实现该功能代码

插入:(光标处插入)

                   string s = txtCode.Text;
                   idx = txtCode.SelectionStart;
                   s=s.Insert(idx, "T")

                   txtCode.Text = s;
                   txtCode.SelectionStart = idx+1;
                   txtCode.Focus();

回退:(光标处退格)

                       string s=txtCode.Text;
                       idx = txtCode.SelectionStart;
                       if (idx > 0)
                       {
                           txtCode.Text = s.Substring(0, idx - 1) + s.Substring(idx);
                           txtCode.SelectionStart = idx - 1;
                       }
                       else
                       {
                           txtCode.SelectionStart = 0;
           

猜你喜欢

转载自blog.csdn.net/u013139930/article/details/52513960