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

时间:2013-08-05 04:44:30

标签: c# winforms textbox selectall

我想在文本框中选择所有文字。

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

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

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

3 个答案:

答案 0 :(得分:49)

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

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

答案 1 :(得分:2)

您还可以尝试以下方法来解决您的问题:

textBoxResults.SelectAll();

这适用于多行文本框。

答案 2 :(得分:1)

此方法可让您选择控件中的所有文本。

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