Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected line in a Text Box?

I have text box with text wrapping enabled in my Windows Phone 7 app, how do I get the line count at the character selected by the user? For example, if a text box it looked like this:

test
text
bo|x
is
here

, with "|" representing the selected character, the line count would be 3. I need to do this at any point in time, most specifically when the text is changed. I could count the number of newlines in a text box without text wrapping, but this is clearly a different scenario.

like image 270
msbg Avatar asked Nov 23 '12 02:11

msbg


People also ask

What do you do if need to enter multiple lines in a text box control?

Click the Display tab. To enable multiple lines of text to be typed in the text box, select the Multi-line check box, and then optionally do one of the following: To prevent users from being able to insert paragraph breaks in the text box by pressing ENTER, clear the Paragraph breaks check box.

How do I select a textbox?

To select the text box, you need to click the border of the text box, and the insertion point disappears. If you press Tab or Ctrl+Tab while the insertion point is visible in the text box, then you only modify the text in the text box; you don't select the next object.


2 Answers

I figured this out myself:

Rect rec = textbox.GetRectFromCharacterIndex(textbox.SelectionStart);
double rectop = rec.Top;
double lineheight = text.LineHeight;
int result = (int)(rectop / lineheight + 1);

result = the selected line.

like image 196
msbg Avatar answered Oct 12 '22 00:10

msbg


only a stupid way:

you can handle these event:ManipulationStarted ManipulationCompleted

From the callback args you can find the point(x,y) user point, maybe it is the coordinate of selected place.

and you know the height of everyline from the prop —— lineHeight

and do a division

like image 22
MengMeng Avatar answered Oct 12 '22 00:10

MengMeng