如何选择Windows窗体文本框中的所有文本?

我想选择文本框中的所有文本。

我使用下面的代码试过这个:

textBoxResults.SelectionStart = 0; textBoxResults.SelectionLength = textBoxResults.Text.Length; 

来源:我从这里得到这个代码http://msdn.microsoft.com/en-us/library/vstudio/hk09zy8f(v=vs.100).aspx但由于某种原因它似乎不起作用。

您可以使用内置方法来实现此目的。

 textBoxResults.SelectAll(); textBoxResults.Focus(); //you need to call this to show selection if it doesn't has focus 

您还可以尝试以下可能解决您的问题:

 textBoxResults.SelectAll(); 

这适用于多行文本框。

使用此方法可以选择控件中的所有文本。

 public void CopyAllMyText() { // Determine if any text is selected in the TextBox control. if(textBox1.SelectionLength == 0) // Select all text in the text box. textBox1.SelectAll(); // Copy the contents of the control to the Clipboard. textBox1.Copy(); } 

请查看此链接以获取更多信息。 http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectall.aspx