4

I'm working on a project which is in the C# language. My question is: How can I detect the string which is under the Mouse Cursor and is between the "(" and ")"?

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

CAbbott
  • 8,078
  • 4
  • 31
  • 38

3 Answers3

3

Edit

The following code will select the word directly under the Mouse

Xaml

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

Code behind

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);
    }
}
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
1

Very interesting problem, I don't know the answer right away, there are similar questions around, not a solution out of the box but if you work it out and let it inspire you, a solution could be possible:

Get Displayed Text from TextBlock

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

You can split up the text into multiple TextBlock runs... just like you would in a web site (using spans)... then you can tap into the MouseOver events in the styleing.

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 styling for the IsMouseOver of the textblocks that you want. (You can do this all in code as well).

Timothy Khouri
  • 31,315
  • 21
  • 88
  • 128
  • Not exactly light on implementation, but a viable solution just the same. Combining this with some kind of factory which takes in the text to display and spits out a control collection would be ideal. – Nathan Taylor Aug 29 '11 at 19:55
  • `TextBox.Text` is a string property and doesn't accept a `StackPanel` – Fredrik Hedblad Aug 29 '11 at 19:56
  • Property 'Text' does not support values of type 'StackPanel'. – Siyamak Shahpasand Aug 29 '11 at 20:01
  • Thanks, I forgot you don't need a stackpanel (and one is not appropriate there)... I just wrote this by hand remembering similar things I've done in the past. – Timothy Khouri Aug 30 '11 at 00:47
  • @Timothy Khouri: `TextBlock` isn't of type string either. You won't be able to use any kind of UIElement inside the `Text` property of `TextBox` – Fredrik Hedblad Aug 30 '11 at 05:35
  • @Meleak ... you don't need a UIElement inside there, and I don't have any UIElements inside of my example - you lost me. I think the requirement was simply to "highlight" (or 'style'), which you can do perfectly. – Timothy Khouri Aug 31 '11 at 02:58
  • @Timothy Khouri: Come on now, sure you do. You add not 1, but 8 `TextBlocks` to the `Text` property of a `TextBox`. And yes, `TextBlock` derives from `UIElement`. Have you tried to paste it into a Xaml editor? You'll get the following errors: **Property 'Text' does not support values of type 'TextBlock'** and **error MC3089: The object 'String' already has a child and cannot add 'TextBlock'. 'String' can accept only one child.** – Fredrik Hedblad Aug 31 '11 at 14:16