今天在开发的时候记录一些小的功能点,TextBox 不像Qt里面的那些API,没有直接的插入API需要手动的去定位一下。也很简单。
一、 光标处插入
流程就是获取 输入框的字符串,然后再获取光标位置,再对获取到的字符串插入操作,最后把插入完成的字符串再回设置到 TextBox 里面就好啦。
string s = txtCode.Text;
string needToInsert = "Hello";
idx = txtCode.SelectionStart;
s=s.Insert(idx, needToInsert );
txtCode.Text = s;
txtCode.SelectionStart = idx + needToInsert.Length;
//聚焦光标
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;
}
//聚焦光标
txtCode.Focus();