Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi how can I set selectionStart for text in some line in a multiline textbox?
Posted

If i undertsand the question correctly the following should work

C#
int lineNum = 3;
textBox1.SelectionStart = textBox1.GetFirstCharIndexFromLine(lineNum);



GetFirstCharIndexFromLine gets the character index of the first character on the specified line, setting the selection start to this will mean your selection starts from the beginning of that line.

EDIT:

just to add a bit more, in your comment in solution 2 you say you want to position the selection start at an arbitrary point along the line not just at the beginning, to do that simply add the number of characters you want to indent from the beginning of the line:

C#
int lineNum = 3;
int characterNum = 5;
textBox1.SelectionStart = textBox1.GetFirstCharIndexFromLine(lineNum) + characterNum;
 
Share this answer
 
v3
Comments
RaisKazi 1-Sep-11 6:56am    
Perfect Answer.
In addition to GParkings solution have a look at given article which demonstrating the GetFirstCharIndexOfCurrentLine() function.
Get current Caret Line and Column in a multiline Windows Forms TextBox[^]
 
Share this answer
 
Comments
RaisKazi 1-Sep-11 6:58am    
Perfect.
RaviRanjanKr 1-Sep-11 7:31am    
Thanks :)
Yes - but it works in terms of character position, so you will have to work out the line based on that, including the length of all the line above where you want the caret to go.
 
Share this answer
 
Could you please give me an example? and also the a way to place the caret where I want, not necessarily at first or end of a line
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900