为什么点击Button不能高亮文本框中的文本?

阿良chjlcn 2006-10-23 09:58:37
为什么点击Button不能高亮文本框中的文本?
以下内容为程序代码:
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "012345";
textBox2.Select(1, 2); // 这里为什么不能高亮textBox2中的12(蓝色背景,白色字)
}

...全文
195 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tenner 2006-10-23
  • 打赏
  • 举报
回复
textBox2.HideSelection = false;

加上这句就行了
anuo06 2006-10-23
  • 打赏
  • 举报
回复
textBox2.Text = "012345";
textBox2.Select(1, 2);
textBox2.Focus();
myminimouse 2006-10-23
  • 打赏
  • 举报
回复
帮顶~
阿良chjlcn 2006-10-23
  • 打赏
  • 举报
回复
应该是TreeView的NodeMouseClick,不过我试过AfterSelect也不行
阿良chjlcn 2006-10-23
  • 打赏
  • 举报
回复
@Knight94(愚翁)
先textBox2.Focux()后可以,

但在TreeView的AfterSelect后为什么不同,也是同样的代码啊?

public class MyTag
{
public int Index;
public int Length;

public MyTag(int index, int length)
{
Index = index;
Length = length;
}
}
}
// 在单击TreeView的node的文本时为什么不能TextBox中的文本(蓝色背景,白色字的效果),
// 而在单击node的+或-号时却可以选择TextBox中的文本?
private void MatchTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
MyTag tag = (MyTag) e.Node.Tag;
MessageBox.Show(string.Format("index = {0};\r\n\r\nlength = {1};",
tag.Index.ToString(),
tag.Length.ToString()));
SampleStringTextBox.Focus();
SampleStringTextBox.Select(tag.Index, tag.Length);
}

copico 2006-10-23
  • 打赏
  • 举报
回复
本来是不显示的,但你选择的值已经存起来了

你可以用
private void button2_Click(object sender, System.EventArgs e)
{
textBox1.Text = "012345";
textBox1.Select(0,2);

}

private void button3_Click(object sender, System.EventArgs e)
{
textBox1.Text = textBox1.SelectedText;
}
来显示值
Knight94 2006-10-23
  • 打赏
  • 举报
回复
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "012345";
textBox2.Focus();
textBox2.Select(1, 2); // 这里为什么不能高亮textBox2中的12(蓝色背景,白色字)
}

110,825

社区成员

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

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

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