IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Touver un mot


Sujet :

C#

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2012
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2012
    Messages : 20
    Points : 14
    Points
    14
    Par défaut Touver un mot
    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 !

  2. #2
    Membre expert
    Avatar de GuruuMeditation
    Homme Profil pro
    .Net Architect
    Inscrit en
    Octobre 2010
    Messages
    1 705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Belgique

    Informations professionnelles :
    Activité : .Net Architect
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2010
    Messages : 1 705
    Points : 3 570
    Points
    3 570
    Par défaut
    RichTextBox n'est pas un contrôle facile à utiliser, j'ai déjà sué pas mal dessus par le passé !

    Essaye un truc comme :
    Code C# : 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
     
    var position = rtb.Document.ContentStart;
     
                while (position != null)
                {
                    if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                    {
                        var textRun = position.GetTextInRun(LogicalDirection.Forward);
     
                        var indexInRun = textRun.IndexOf(wordToSearch);
                        if (indexInRun >= 0)
                        {
                            var start = position.GetPositionAtOffset(indexInRun);
                            var end = start.GetPositionAtOffset(wordToSearch.Length);
     
                            rtb.Selection.Select(start,end);
     
                            rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, rtb.SelectionBrush);
                            rtb.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                            rtb.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
                            rtb.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Yellow));
     
                        }
                    }
     
                    position = position.GetNextContextPosition(LogicalDirection.Forward);
                }
    la variable rtb étant ton RichTextBox

Discussions similaires

  1. Extraction de mots clés
    Par Olive1808 dans le forum Algorithmes et structures de données
    Réponses: 6
    Dernier message: 01/02/2016, 20h49
  2. Réponses: 2
    Dernier message: 16/07/2013, 01h54
  3. Réccupération du mot de passe d'une connexion
    Par zinaif dans le forum Bases de données
    Réponses: 1
    Dernier message: 02/12/2002, 18h32
  4. Au sujet des mots de passe
    Par FranT dans le forum Langage
    Réponses: 6
    Dernier message: 17/09/2002, 22h16
  5. [langage] Reconnaissance de mots
    Par Flo. dans le forum Langage
    Réponses: 2
    Dernier message: 10/08/2002, 23h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo