首先,感谢您的回答!共9个答案。谢谢。
坏消息:所有答案都有一些古怪之处,或者说不是很正确(或根本没有)。我已在您的每个帖子中添加了评论。
好消息:我已经找到一种使之工作的方法。该解决方案非常简单,并且似乎可以在所有情况下使用(向下移动,选择文本,以制表符为焦点等)
bool alreadyFocused;
...
textBox1.GotFocus += textBox1_GotFocus;
textBox1.MouseUp += textBox1_MouseUp;
textBox1.Leave += textBox1_Leave;
...
void textBox1_Leave(object sender, EventArgs e)
{
alreadyFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
// Select all text only if the mouse isn't down.
// This makes tabbing to the textbox give focus.
if (MouseButtons == MouseButtons.None)
{
this.textBox1.SelectAll();
alreadyFocused = true;
}
}
void textBox1_MouseUp(object sender, MouseEventArgs e)
{
// Web browsers like Google Chrome select the text on mouse up.
// They only do it if the textbox isn't already focused,
// and if the user hasn't selected all text.
if (!alreadyFocused && this.textBox1.SelectionLength == 0)
{
alreadyFocused = true;
this.textBox1.SelectAll();
}
}
据我所知,这会使文本框的行为与Web浏览器的地址栏完全相同。
希望这有助于下一个尝试解决这个看似简单的问题的人。
再次感谢大家提供的所有答案,这些答案帮助我迈向了正确的道路。