如何实现点击文本框后,自动选中所有文字

tmp_gogoto 2011-07-29 11:45:06
如何实现点击文本框后,自动选中所有文字

我的代码为何无效呢?
应该如何实现,我的目的是,只要点一下文本框,就选中文本框中的所有文字


//文本框获取焦点时
private void txtsxkh_Enter(object sender, EventArgs e)
{
txtsxkh.SelectionStart = 0;
txtsxkh.SelectionLength = txtsxkh.Text.Length;
txtsxkh.SelectAll();
}
...全文
1001 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
言多必失 2011-07-29
  • 打赏
  • 举报
回复
private void txtsxkh_MouseDown(object sender, MouseEventArgs e)
{
txtsxkh.SelectionStart = 0;
txtsxkh.SelectionLength = txtsxkh.Text.Length;
txtsxkh.SelectAll();
}

测试通过
sunny906 2011-07-29
  • 打赏
  • 举报
回复
用文本框的MouseDown事件:

private void txtsxkh_MouseDown(object sender, MouseEventArgs e)
{
txtsxkh.SelectionStart = 0;
txtsxkh.SelectionLength = txtsxkh.Text.Length;
txtsxkh.SelectAll();
}
shighui 2011-07-29
  • 打赏
  • 举报
回复
将这些代码写到mouseUp的事件里就可以了。
fxie8908 2011-07-29
  • 打赏
  • 举报
回复

private void txtsxkh_Click(object sender, EventArgs e)
{
txtsxkh.SelectAll();
}
流浪的青春 2011-07-29
  • 打赏
  • 举报
回复
private bool setAll = false;
private void textBox2_Click(object sender, EventArgs e)
{
if (!setAll)
{
this.textBox2.SelectAll();
setAll = true;
}
}

private void textBox2_Leave(object sender, EventArgs e)
{
setAll = false;
}

第一次点击的时候全选,再次点击的时候解除全选,这个时候可以局部选择。
tmp_gogoto 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shiguohui000 的回复:]

将这些代码写到mouseUp的事件里就可以了。
[/Quote]
这个也不行的,只能选中全部了,无法再选中部分字符了。
tmp_gogoto 2011-07-29
  • 打赏
  • 举报
回复
mousedown不行的。
选中的只是开头到光标位置的

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧