我在一个应用程序中工作,它将测量打字的速度。
我想选择那个时候正在写的单词。在form事件中有以下代码片段:
string line;
FileStream fs = new FileStream("text.txt", FileMode.Open, FileAccess.Read);
StreamReader fr = new StreamReader(fs);
while ((line = fr.ReadLine()) != null)
{
textBox1.AppendText(line);
}
string texto = textBox1.Text;
string[] split = texto.Split(new char[] { ' ' });
textBox1.SelectionStart = 0;
textBox1.SelectionLength = split[0].Length;这是正确的,但当我将焦点更改为另一个文本框开始键入时,所选的单词将消失。
有什么办法可以避免这种情况发生吗?
发布于 2015-04-26 01:02:59
若要防止文本框在失去焦点时丢失其选定内容,您可以这样做(创建窗体时)。
textBox1.HideSelection = false;请参阅 Property
获取或设置一个值,该值指示文本框控件中的选定文本在控件失去焦点时是否保持高亮显示。
https://stackoverflow.com/questions/29872490
复制相似问题