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

假如想象 提交于 2019-12-03 16:30:17

问题


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.


回答1:


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



回答2:


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

textBoxResults.SelectAll();

This works well with multi-lined textbox.




回答3:


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



来源:https://stackoverflow.com/questions/18050714/how-can-i-select-all-the-text-within-a-windows-forms-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!