RichTextBox : 1 seul mot précis en couleur ? possible ?

Résolu
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 - 5 mars 2007 à 17:56
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 - 14 mars 2007 à 11:12
Bonjour,

Est-ce que quelqu'un serait s'il est possible, en Visual Basic .NET 2005 (donc FrameWork 2), de colorer un mot précis dans un contrôle RichTextBox ...

Si non, comment contourner, quoi d'autre utilisé ?

Merci à tous!

++

11 réponses

cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
5 mars 2007 à 18:05
Oui c'est possible, Propriété .SelectionColor

---- Sevyc64  (alias Casy) ---- # LE PARTAGE EST NOTRE FORCE #
3
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
5 mars 2007 à 18:48
.SelectionColor modifie la couleur du texte sélectionné.

Reste à toi de sélectionner ce texte, tu as effectivement plusieurs pistes possibles. Regarde aussi surtout la methode .Select de la classe RichTextBox

---- Sevyc64  (alias Casy) ---- # LE PARTAGE EST NOTRE FORCE #
3
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
5 mars 2007 à 21:49
tiens regarde ce code, je l'ai un peu corriger

Dim keyword As String() = {"echo", "shuffle", "php"}

Private Sub RichTextBox1_TextChanged(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles
RichTextBox1.TextChanged
        Dim line As String = ""

        'Sauvegarde de la
couleur et de la position actuelle
        Dim defColor As Color = RichTextBox1.SelectionColor
        Dim defPos As Integer = RichTextBox1.SelectionStart

        Try
                With RichTextBox1
                        For i As Integer = 0 To .Lines.GetUpperBound(0)
                                line = .Lines.GetValue(i).ToString
                                If String.IsNullOrEmpty(line) Then
Exit Try
                                line = line.ToLowerInvariant
                                For j As Integer = 0 To keyword.GetUpperBound(0)
                                        If line.Contains(keyword(j))
Then
                                          .SelectionStart = line.IndexOf(keyword(j)) +
.GetFirstCharIndexFromLine(i)
                                         
.SelectionLength =
keyword(j).Length
                                          .SelectionColor =
Color.Blue
                                        End If
                                Next
                        Next
                End With

        Catch ex As Exception
                Exit Try

        Finally
                '
Systématiquement, on restaure la position du curseur et la couleur par
défaut
                RichTextBox1.SelectionStart
= defPos
                RichTextBox1.SelectionLength = 0
                RichTextBox1.SelectionColor = defColor
        End Try

End Sub

---- Sevyc64  (alias Casy) ---- # LE PARTAGE EST NOTRE FORCE #
3
warham Messages postés 23 Date d'inscription lundi 6 février 2006 Statut Membre Dernière intervention 17 novembre 2007
5 mars 2007 à 18:03
hmm, faire un peu comme notepad en C++ c'est ca?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 2
5 mars 2007 à 18:12
ah oui tu as raison casy j'avais mal lu SelectionColor : mais je vois pas très bien comment l'utiliser :

dans le handles textchanged :

RichTextBox1.SelectionColor.Blue et après ??
(dsl si c'est une question "bete" mais je vois pas ...)

merci encore
0
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 2
5 mars 2007 à 18:24
c'est bon je viens de comprendre :

if mot.contains("echo") then
.selectioncolor = color.blue
end if

c'est bien comme ça ?
0
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 2
5 mars 2007 à 18:31
y'a bien quelque chose de possible avec .lines et .selectioncolor mais je trouve pas (j'ai parlé trop vite !) ...
0
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 2
5 mars 2007 à 18:56
ah merci!!! tu es un boss :P

oui select + une boucle For pour les lignes ...
merci bien
0
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 2
5 mars 2007 à 19:20
Voici ce que j'ai écris mais c'est encore plus compliqué que je le pensais :-(
si tu pouvais m'aider ... c'est pourtant simple (dans la tete en tout cas) mais pour codé ça ... wow!



Dim



keyword

As



Object
() = {

"echo"
,

"shuffle"
,

"php"
}


Private



Sub
RichTextBox1_TextChanged(

ByVal
sender

As
System.Object,

ByVal
e

As
System.EventArgs)

Handles
RichTextBox1.TextChanged


Try






With
RichTextBox1


Dim
line

As



String
=

""






For
i

As



Integer
= 0

To
.Lines.GetUpperBound(0)line = .Lines.GetValue(i).ToString


If



String
.IsNullOrEmpty(line)

Then



Exit



Try

line = line.ToLowerInvariant







For
j

As



Integer
= 0

To
keyword.GetUpperBound(0)


If
line.Contains(keyword.GetValue(j).ToString)

Then






Dim
debut

As



Integer
= line.IndexOf(keyword.GetValue(j).ToString)


Dim
taille

As



Integer
= keyword.GetValue(j).ToString.Length.Select(debut, taille)

.SelectionColor = Color.Blue


End



If






Next






Next






End



With






Catch
ex

As
Exception


Exit



Try






End



Try






End



Sub
0
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 2
5 mars 2007 à 19:22
oups petite erreur avec le if String.IsNullOrEmpty ça quitte tout le Try-Catch, mais enfin je pense que tu comprendras le code quand même
0
allthew3 Messages postés 551 Date d'inscription samedi 8 janvier 2005 Statut Membre Dernière intervention 12 avril 2008 2
14 mars 2007 à 11:12
ah oui merci ! j'aurais jamais trouvé tout seul !

merci encore
++
0
Rejoignez-nous