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 :

selection des mots similaires en textbox


Sujet :

C#

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2011
    Messages : 59
    Points : 33
    Points
    33
    Par défaut selection des mots similaires en textbox
    bonjour tout le monde
    j'ai essayer de trouver une solution de pouvoir récuperer une mot ou phrase selectionner d'un text en textbox c# et de pouvoir coloré tous les mots similaires dans textbox.
    voila mon code:

    int index = 0;
    var word = richTextBox1.SelectedText.ToString();
    //word!=null
    if (richTextBox1.SelectedText!=String.Empty)
    {
    while (index < richTextBox1.Text.LastIndexOf(word))
    {
    richTextBox1.Find(word, index, richTextBox1.TextLength, RichTextBoxFinds.None);
    richTextBox1.SelectionBackColor = Color.Yellow;
    index = richTextBox1.Text.IndexOf(word, index) + 1;
    richTextBox1.Focus();
    }
    }

    je pense que le programme ne récupère pas le mot selectionné ?

    merci pour votre suggestion

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2011
    Messages : 59
    Points : 33
    Points
    33
    Par défaut
    bonjour tout le monde
    j'ai essayer de trouver une solution de pouvoir récuperer une mot ou phrase selectionner d'un text en textbox c# et de pouvoir coloré tous les mots similaires dans textbox.
    voila 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
     
    int index = 0;
    var word = richTextBox1.SelectedText.ToString();
    //word!=null
    if (richTextBox1.SelectedText!=String.Empty)
    {
    while (index < richTextBox1.Text.LastIndexOf(word))
    {
    richTextBox1.Find(word, index, richTextBox1.TextLength, RichTextBoxFinds.None);
    richTextBox1.SelectionBackColor = Color.Yellow;
    index = richTextBox1.Text.IndexOf(word, index) + 1;
    richTextBox1.Focus();
    }
    }
    je pense que le programme ne récupère pas le mot selectionné ?

    merci pour votre suggestion

  3. #3
    olf
    olf est déconnecté
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2004
    Messages
    26
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 26
    Points : 32
    Points
    32
    Par défaut
    Bonjour,

    richTextBox1.SelectedText étant un String, je pense qu'il est inutile de lui appliquer la méthode ToString().

    Sinon, je ne vois pas pourquoi le programme ne récupère pas le mot selectionné.

    Comment votre extrait de code est-il appelé/déclenché ?

    Cordialement,

    Florian.

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2011
    Messages : 59
    Points : 33
    Points
    33
    Par défaut
    Citation Envoyé par olf Voir le message
    Bonjour,

    richTextBox1.SelectedText étant un String, je pense qu'il est inutile de lui appliquer la méthode ToString().

    Sinon, je ne vois pas pourquoi le programme ne récupère pas le mot selectionné.

    Comment votre extrait de code est-il appelé/déclenché ?

    Cordialement,

    Florian.
    j'ai pas compris votre question ?

  5. #5
    Inactif  

    Homme Profil pro
    développeur Vala
    Inscrit en
    Février 2011
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : développeur Vala
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2011
    Messages : 478
    Points : 3 700
    Points
    3 700
    Par défaut
    Je suis de bonne humeur . Je t'ai bricolé un nouveau contrôle, basé sur un RichTextBox :
    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
    	public class SearchTextBox : RichTextBox
    	{
    		public SearchTextBox ()
    		{
     
    		}
     
    		public void HighlightText(string word, Color color){  
                         int s_start = this.SelectionStart, startIndex = 0, index;
     
                         while((index = this.Text.IndexOf(word, startIndex)) != -1)
                         {
                            this.Select(index, word.Length);
                            this.SelectionColor = color;
                            startIndex = index + word.Length;
                         }
     
                         this.SelectionStart = s_start;
                         this.SelectionLength = 0;
                         this.SelectionColor = Color.Black;
        	        }
    	}
    Je l'ai appelé SearchTextBox, appelle le comme tu veux

  6. #6
    olf
    olf est déconnecté
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2004
    Messages
    26
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 26
    Points : 32
    Points
    32
    Par défaut
    Citation:
    Envoyé par olf
    Bonjour,

    richTextBox1.SelectedText étant un String, je pense qu'il est inutile de lui appliquer la méthode ToString().

    Sinon, je ne vois pas pourquoi le programme ne récupère pas le mot selectionné.

    Comment votre extrait de code est-il appelé/déclenché ?

    Cordialement,

    Florian.

    j'ai pas compris votre question ?
    J'essaie de reformuler ma question :

    À quel moment le programme va-t-il récupérer le texte sélectionné dans la RichTextBox et surligner en jaune tous les mots similaires ? Est-ce au démarrage de l'application ? Est-ce suite à une action de l'utilisateur (clic sur un bouton par exemple) ?

    L'objectif de cette question est de comprendre si possible pourquoi votre code ne fonctionne pas de votre côté (la récupération du texte sélectionné fonctionne sans problème pour moi).

    Cordialement,

    Florian.

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2011
    Messages : 59
    Points : 33
    Points
    33
    Par défaut
    Citation Envoyé par rotrevrep Voir le message
    Je suis de bonne humeur . Je t'ai bricolé un nouveau contrôle, basé sur un RichTextBox :
    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
    	public class SearchTextBox : RichTextBox
    	{
    		public SearchTextBox ()
    		{
     
    		}
     
    		public void HighlightText(string word, Color color){  
                         int s_start = this.SelectionStart, startIndex = 0, index;
     
                         while((index = this.Text.IndexOf(word, startIndex)) != -1)
                         {
                            this.Select(index, word.Length);
                            this.SelectionColor = color;
                            startIndex = index + word.Length;
                         }
     
                         this.SelectionStart = s_start;
                         this.SelectionLength = 0;
                         this.SelectionColor = Color.Black;
        	        }
    	}
    Je l'ai appelé SearchTextBox, appelle le comme tu veux
    Merci rotrevrep mais ça marche pas lors de selection d'une mot !!
    est ce que tu as un exemple dans textbox ??

  8. #8
    Inactif  

    Homme Profil pro
    développeur Vala
    Inscrit en
    Février 2011
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : développeur Vala
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2011
    Messages : 478
    Points : 3 700
    Points
    3 700
    Par défaut
    Je t'ai bien proposé est bien une RichTextBox, comme tu l'as montré plus haut. Si tu veux faire une recherche dans une TextBox, suffit d'indiquer dans la fonction que le mot recherché est TextBox.Text. Et si tu veux carrément tu peux même rechercher directement dans la RichTextBox :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    SearchTextBox.SelectionChanged+= delegate(object sender, EventArgs e) {
    var view = sender as SearchTextBox;
        view.HighlightText(view.SelectedText,Color.Green);
    			};

  9. #9
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2011
    Messages : 59
    Points : 33
    Points
    33
    Par défaut
    Citation Envoyé par rotrevrep Voir le message
    Je t'ai bien proposé est bien une RichTextBox, comme tu l'as montré plus haut. Si tu veux faire une recherche dans une TextBox, suffit d'indiquer dans la fonction que le mot recherché est TextBox.Text. Et si tu veux carrément tu peux même rechercher directement dans la RichTextBox :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    SearchTextBox.SelectionChanged+= delegate(object sender, EventArgs e) {
    var view = sender as SearchTextBox;
        view.HighlightText(view.SelectedText,Color.Green);
    			};
    Merci pour les deux codes rotrevrep mais il ne fonctionnent pas !!
    c quoi le problèmes !!

  10. #10
    Inactif  

    Homme Profil pro
    développeur Vala
    Inscrit en
    Février 2011
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : développeur Vala
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2011
    Messages : 478
    Points : 3 700
    Points
    3 700
    Par défaut
    Avec l'aide de tomlev qui a corrigé ma fonction, je te propose un petit programme tout fait pour que tu puisses voir le fonctionnement.

    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    namespace test
    {
    	class MainClass
    	{
    		public static void Main (string[] args)
    		{
    			Win window = new Win();
    			window.Show();
    			Application.Run();
    		}
    	}
     
    	public class Win : Form
    	{
    		RichTextBox box;
    		TextBox textbox;
     
    		public Win ()
    		{
    			box = new RichTextBox();
    			this.Controls.Add(box);
    			textbox = new TextBox();
    			textbox.KeyUp+= delegate(object sender, KeyEventArgs e) {
    				box.HighlightText(textbox.Text,Color.Magenta);
    			};
    			this.Controls.Add(textbox);
    			textbox.Location = new Point(150,150);
    			this.Closed+= delegate {
    				Application.Exit();
    			};
    		}
    	}
     
    	public static class RichTextBoxExtensions
        {
            public static void HighlightText(this RichTextBox richTextBox, string word, Color color)
    		{
    			int startIndex = 0, index;
    			richTextBox.Select(0,richTextBox.Text.Length);
    			richTextBox.SelectionColor = richTextBox.ForeColor;
    			if(word.Length==0)return;
    			while((index = richTextBox.Text.IndexOf(word, startIndex)) != -1)
                {
                    richTextBox.Select(index, word.Length);
                    richTextBox.SelectionColor = color;
                    startIndex = index + word.Length;
                }
            }
        }
    }
    Mais que ça ne t'empêche pas d'apprendre les extensions et les héritages en C# .Net

  11. #11
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2011
    Messages : 59
    Points : 33
    Points
    33
    Par défaut
    Citation Envoyé par rotrevrep Voir le message
    Avec l'aide de tomlev qui a corrigé ma fonction, je te propose un petit programme tout fait pour que tu puisses voir le fonctionnement.

    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    namespace test
    {
    	class MainClass
    	{
    		public static void Main (string[] args)
    		{
    			Win window = new Win();
    			window.Show();
    			Application.Run();
    		}
    	}
     
    	public class Win : Form
    	{
    		RichTextBox box;
    		TextBox textbox;
     
    		public Win ()
    		{
    			box = new RichTextBox();
    			this.Controls.Add(box);
    			textbox = new TextBox();
    			textbox.KeyUp+= delegate(object sender, KeyEventArgs e) {
    				box.HighlightText(textbox.Text,Color.Magenta);
    			};
    			this.Controls.Add(textbox);
    			textbox.Location = new Point(150,150);
    			this.Closed+= delegate {
    				Application.Exit();
    			};
    		}
    	}
     
    	public static class RichTextBoxExtensions
        {
            public static void HighlightText(this RichTextBox richTextBox, string word, Color color)
    		{
    			int startIndex = 0, index;
    			richTextBox.Select(0,richTextBox.Text.Length);
    			richTextBox.SelectionColor = richTextBox.ForeColor;
    			if(word.Length==0)return;
    			while((index = richTextBox.Text.IndexOf(word, startIndex)) != -1)
                {
                    richTextBox.Select(index, word.Length);
                    richTextBox.SelectionColor = color;
                    startIndex = index + word.Length;
                }
            }
        }
    }
    Mais que ça ne t'empêche pas d'apprendre les extensions et les héritages en C# .Net

    est ce qu'il ya une solution pareil pour les textbox ???????????

Discussions similaires

  1. Double click et selection des mots
    Par ennadi dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 05/06/2013, 16h58
  2. [XL-2007] Rechercher des mots dans un tableau et selection
    Par Olivier6464 dans le forum Macros et VBA Excel
    Réponses: 17
    Dernier message: 02/05/2013, 16h57
  3. [Débutant] Ouvrir un fichier word et remplacer des mots pas le contenu des textbox
    Par nabileon dans le forum VB.NET
    Réponses: 12
    Dernier message: 29/03/2013, 15h52
  4. selection des mots similaires en textbox
    Par morched89 dans le forum C#
    Réponses: 7
    Dernier message: 23/11/2012, 09h19
  5. Réponses: 3
    Dernier message: 14/09/2010, 12h45

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