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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18050714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:08:36  来源:igfitidea点击:

How can I select all the text within a Windows Forms textbox?

c#winformstextboxselectall

提问by B. Clay Shannon

I want to select all the text that is with in text box.

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

I've tried this using the code below:

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

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

Source: I got this code from here http://msdn.microsoft.com/en-us/library/vstudio/hk09zy8f(v=vs.100).aspxbut for some reason it doesn't seem to work.

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

采纳答案by Ehsan

You can use the built in method for this purpose.

为此,您可以使用内置方法。

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

回答by Conrad Lotz

You can also try the following which might solve you problem:

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

textBoxResults.SelectAll();

This works well with multi-lined textbox.

这适用于多行文本框。

回答by Ajay

This method enables you to select all text within the control.

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

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();
}

Check this link for more info. http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectall.aspx

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