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

3

25

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).aspx but for some reason it doesn't seem to work - meaning, no text gets selected.

Kantian answered 5/8, 2013 at 4:44 Comment(9)
(...)but it doesn't work.(...), what do you get in selectedText if you write var selectedText = textBoxResults.SelectedText;? Are you modifying the text property after the code above?Sociability
No, this is after programmatically populating the textbox. I just want to quickly highlight everything so I don't have to scroll through a gazillion lines of text.Kantian
How much text is in there?Sociability
A lot; too much to quickly drag through.Kantian
What doesn't work? Is the text only partially selected? Or not selected at all? You have to provide more details about your problem.Sociability
Nothing at all happens.Kantian
And the same exact comment could be made regarding this question of mine: #18034516Kantian
It was answered below by Ehsan Ullah, so he understood it, and it's working now - in addition to SelectAll(), I also had to call Focus() after that. IOW, it was selected (theoretically, at least), but how would I know that. Seems like a strange implementation to me, but, as I said, the question has been answered/solved.Kantian
I had the same issue until I set the HideSelection property for textbox to false....Lederman
G
68

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
Geraldgeralda answered 5/8, 2013 at 4:48 Comment(9)
That would seem sensible, but it doesn't work, either. I added textBoxResults.SelectAll();Kantian
i have tested it, it works eperfectly fine. Can you show how have you implemented it. it must be something else that is causing the issue.Geraldgeralda
It's very simple: I programmatically populate the textbox, then I have a button with that code in it. I tried what I posted, which didn't work, and I tried the SelectAll(), which also doesn't work.Kantian
got your problem, call textBoxResults.Focus() after selectall callGeraldgeralda
@ClayShannon is your issue resolved? have you tried the updated code?Geraldgeralda
Or set the HideSelection property to false.Neomaneomah
Okay, with the added .Focus() it works, thanks. Strange that the official way to do it from msdn led me down the garden path.Kantian
I always upvote every answer to one of my questions as soon as I see it - before I even read it.Kantian
I typed the following code in the "Enter" event of a Textbox. When i click into the Textbox, text is not selected: Textbox1.SelectAll(); Textbox1.Focus();Dug
C
3

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

textBoxResults.SelectAll();

This works well with multi-lined textbox.

Compellation answered 5/8, 2013 at 4:53 Comment(0)
P
2

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

Punjabi answered 5/8, 2013 at 5:1 Comment(2)
Why would I need to check for selectionlength? I want to select all with nothing selected prior to that.Kantian
You can remove if condition.Punjabi

© 2022 - 2024 — McMap. All rights reserved.