bonjour a tous,

le petite de mon code est de demander a l'utilisateur un mot et je lui retrouve dans le text.
et si il redemande c mot je cherche le suivant dans le text.
J'ai essaye d'ecrire du code pour ca mais j'ai un proble d'index.
il y a un decalage

voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
public void SelectText(string wordToSearch, int timer)
        {
            TextRange tr = new TextRange(txtBoxContent.Document.ContentStart, txtBoxContent.Document.ContentEnd);
            int startPosition = tr.Text.IndexOf(wordToSearch);
            if (startPosition == -1)
            {
                MessageBox.Show("The text has not been found");
            }
            else 
            {
                int lines = GetLinesCount(tr.Text, startPosition);
 
                TextPointer startRtb = txtBoxContent.Document.ContentStart;
                TextPointer start = startRtb.GetPositionAtOffset(startPosition + lines);
                TextPointer end = start.GetPositionAtOffset(wordToSearch.Length);
                if (timer == 0)
                {
                    txtBoxContent.Selection.Select(start, end);
                }
                for (int i = 0; i < timer; i++)
                {
                    startRtb = end;
                    tr.Select(startRtb, txtBoxContent.Document.ContentEnd);
                    startPosition = tr.Text.IndexOf(wordToSearch);
                    if (startPosition == -1)
                    {
                        MessageBox.Show("The text has not been found");
                        return;
                    }
                    start = startRtb.GetPositionAtOffset(startPosition + lines);
                    end = start.GetPositionAtOffset(wordToSearch.Length);
                    txtBoxContent.Selection.Select(start, end);
                }
 
                txtBoxContent.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, txtBoxContent.SelectionBrush);
                txtBoxContent.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                txtBoxContent.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
                txtBoxContent.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Yellow));
            }
        }
Merci de votre aide !