How to select a specific line under the mouse cursor in C # -wpf?

I am working on a project that is in C #. My question is: how can I determine the line that is under the mouse cursor and that is between "(" and ")"?

For example: I want to highlight "yahoo" in the text box with this content when the mouse over it: google (1) yahoo (2) apple (3) microsoft (4) ...

+4
source share
3 answers

Edit

The following code will select the word directly below the mouse

Xaml

<TextBox MouseMove="TextBox_MouseMove" Text="Google(1) yahoo(2) apple(3) microsoft(4)"/> 

Code for

 private void TextBox_MouseMove(object sender, MouseEventArgs e) { TextBox textBox = sender as TextBox; Point mousePoint = Mouse.GetPosition(textBox); int charPosition = textBox.GetCharacterIndexFromPoint(mousePoint, true); if (charPosition > 0) { textBox.Focus(); int index = 0; int i = 0; string[] strings = textBox.Text.Split(' '); while (index + strings[i].Length < charPosition && i < strings.Length) { index += strings[i++].Length + 1; } textBox.Select(index, strings[i].Length); } } 
+3
source

A very interesting problem, I don’t know the answer right away, there are similar questions, not a solution out of the box, but if you develop it and give it inspiration, a solution may be possible:

Get display text from TextBlock

+1
source

You can split the text into several runs of TextBlock ... like on a website (using spans ) ... then you can use MouseOver events in style.

Example:

 <TextBox> <TextBox.Text> <TextBlock Text="google" /> <TextBlock Text="(1) " /> <TextBlock Text="yahoo" /> <TextBlock Text="(2) " /> <TextBlock Text="apple" /> <TextBlock Text="(3) " /> <TextBlock Text="microsoft" /> <TextBlock Text="(4) " /> </TextBox.Text> </TextBox> 

Then add a style for the IsMouseOver text blocks you want. (You can do all this in code).

+1
source

Source: https://habr.com/ru/post/1369549/


All Articles