OVH Cloud OVH Cloud

[Ctrl]Zone de texte Enrichi pour coloriage syntaxe

5 réponses
Avatar
404 found
Bonjouir,
je cherche un contrôle qui me permettra de colorier un texte. Je m'explique :

dans mon application, mon utilisateur peut écrire une syntaxe de requête
(Select ...) je veux quand il fini d'écrire Select par exemple, celle ci
devient bleu, ensuite les noms de ses champs en noir et puis le "From" en
bleu ... pour plus de lisibilité pour mon utilisateur !! tout comme le
générateur de requête SQL !!

Est ce possible ?
comment ?
des idées ??

Merci.

5 réponses

Avatar
Zoury
> Est ce possible ?
comment ?
des idées ??



En voici une simple, mais il peut y avoir plusieurs façon de procéder :
'***
Public Class Form1
Inherits System.Windows.Forms.Form

Private m_motsCles As Hashtable

#Region " Code généré par le Concepteur Windows Form "

Public Sub New()
MyBase.New()

'Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()

'Ajoutez une initialisation quelconque après l'appel
InitializeComponent()

End Sub

'La méthode substituée Dispose du formulaire pour nettoyer la liste des
composants.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Requis par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer

'REMARQUE : la procédure suivante est requise par le Concepteur Windows
Form
'Elle peut être modifiée en utilisant le Concepteur Windows Form.
'Ne la modifiez pas en utilisant l'éditeur de code.
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(8, 8)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(416, 256)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "RichTextBox1 test Test RichTextBox"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(352, 272)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(432, 302)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' pour les besoins de l'exemple, j'ai employé une Hashtable
hard-codée,
' il est évidemment préférable de conserver la liste de mots en
dehors du programme (table de pilotage, .config, autre fichiers..)
' afin de permettre la modification de celle-ci sans devoir modifié
le code.
m_motsCles = New Hashtable(3)
m_motsCles.Add("SELECT", Color.Blue)
m_motsCles.Add("FROM", Color.Red)
m_motsCles.Add("WHERE", Color.Green)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

RichTextBox1.SuspendLayout()
For Each entry As DictionaryEntry In m_motsCles
ChangeWordColorRecusively(RichTextBox1, entry.Key, entry.Value,
0)
Next
RichTextBox1.ResumeLayout()

End Sub

Private Sub ChangeWordColorRecusively(ByVal rtb As RichTextBox, ByVal
keyword As String, ByVal keywordColor As Color, ByVal startIndex As Int32)

Dim keywordPosition As Int32 = rtb.Find(keyword, startIndex,
RichTextBoxFinds.WholeWord)
If (keywordPosition > -1) Then

rtb.Select(keywordPosition, keyword.Length)
rtb.SelectionColor = keywordColor
ChangeWordColorRecusively(rtb, keyword, keywordColor,
keywordPosition + keyword.Length)

End If

End Sub

End Class
'***

En espérant que ça t'inspires.. :O)

--
Cordialement
Yanick
MVP pour Visual Basic
Avatar
404 found
Excellent !! j'ai testé le programme, ça à l'air pas mal !!

Merci beaucoup !! c'est un bon point de départ !

"Zoury" a écrit :

> Est ce possible ?
> comment ?
> des idées ??

En voici une simple, mais il peut y avoir plusieurs façon de procéder :
'***
Public Class Form1
Inherits System.Windows.Forms.Form

Private m_motsCles As Hashtable

#Region " Code généré par le Concepteur Windows Form "

Public Sub New()
MyBase.New()

'Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()

'Ajoutez une initialisation quelconque après l'appel
InitializeComponent()

End Sub

'La méthode substituée Dispose du formulaire pour nettoyer la liste des
composants.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Requis par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer

'REMARQUE : la procédure suivante est requise par le Concepteur Windows
Form
'Elle peut être modifiée en utilisant le Concepteur Windows Form.
'Ne la modifiez pas en utilisant l'éditeur de code.
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(8, 8)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(416, 256)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "RichTextBox1 test Test RichTextBox"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(352, 272)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(432, 302)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' pour les besoins de l'exemple, j'ai employé une Hashtable
hard-codée,
' il est évidemment préférable de conserver la liste de mots en
dehors du programme (table de pilotage, .config, autre fichiers..)
' afin de permettre la modification de celle-ci sans devoir modifié
le code.
m_motsCles = New Hashtable(3)
m_motsCles.Add("SELECT", Color.Blue)
m_motsCles.Add("FROM", Color.Red)
m_motsCles.Add("WHERE", Color.Green)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

RichTextBox1.SuspendLayout()
For Each entry As DictionaryEntry In m_motsCles
ChangeWordColorRecusively(RichTextBox1, entry.Key, entry.Value,
0)
Next
RichTextBox1.ResumeLayout()

End Sub

Private Sub ChangeWordColorRecusively(ByVal rtb As RichTextBox, ByVal
keyword As String, ByVal keywordColor As Color, ByVal startIndex As Int32)

Dim keywordPosition As Int32 = rtb.Find(keyword, startIndex,
RichTextBoxFinds.WholeWord)
If (keywordPosition > -1) Then

rtb.Select(keywordPosition, keyword.Length)
rtb.SelectionColor = keywordColor
ChangeWordColorRecusively(rtb, keyword, keywordColor,
keywordPosition + keyword.Length)

End If

End Sub

End Class
'***

En espérant que ça t'inspires.. :O)

--
Cordialement
Yanick
MVP pour Visual Basic





Avatar
404 found
juste une dernière question : avec le bouton ça marche nickel !! k'ai fait un
évenement sur keypress et je teste sur l'espace (pourchanger la couleur)

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
Select Case e.KeyChar
Case " "
RichTextBox1.SuspendLayout()
For Each entry As DictionaryEntry In m_motsCles
ChangeWordColorRecusively(RichTextBox1, entry.Key, entry.Value, 0)
Next
RichTextBox1.ResumeLayout()
End Select

mais il n'arrive pas à sortir de la récursivité dans
ChangeWordColorRecusively !!

bref, si vous voyez rapidement le problème, sinon j'y passerais le temps
nécessaire !!

En tout cas merci !!

"Zoury" a écrit :

> Est ce possible ?
> comment ?
> des idées ??

En voici une simple, mais il peut y avoir plusieurs façon de procéder :
'***
Public Class Form1
Inherits System.Windows.Forms.Form

Private m_motsCles As Hashtable

#Region " Code généré par le Concepteur Windows Form "

Public Sub New()
MyBase.New()

'Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()

'Ajoutez une initialisation quelconque après l'appel
InitializeComponent()

End Sub

'La méthode substituée Dispose du formulaire pour nettoyer la liste des
composants.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Requis par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer

'REMARQUE : la procédure suivante est requise par le Concepteur Windows
Form
'Elle peut être modifiée en utilisant le Concepteur Windows Form.
'Ne la modifiez pas en utilisant l'éditeur de code.
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(8, 8)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(416, 256)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "RichTextBox1 test Test RichTextBox"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(352, 272)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(432, 302)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' pour les besoins de l'exemple, j'ai employé une Hashtable
hard-codée,
' il est évidemment préférable de conserver la liste de mots en
dehors du programme (table de pilotage, .config, autre fichiers..)
' afin de permettre la modification de celle-ci sans devoir modifié
le code.
m_motsCles = New Hashtable(3)
m_motsCles.Add("SELECT", Color.Blue)
m_motsCles.Add("FROM", Color.Red)
m_motsCles.Add("WHERE", Color.Green)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

RichTextBox1.SuspendLayout()
For Each entry As DictionaryEntry In m_motsCles
ChangeWordColorRecusively(RichTextBox1, entry.Key, entry.Value,
0)
Next
RichTextBox1.ResumeLayout()

End Sub

Private Sub ChangeWordColorRecusively(ByVal rtb As RichTextBox, ByVal
keyword As String, ByVal keywordColor As Color, ByVal startIndex As Int32)

Dim keywordPosition As Int32 = rtb.Find(keyword, startIndex,
RichTextBoxFinds.WholeWord)
If (keywordPosition > -1) Then

rtb.Select(keywordPosition, keyword.Length)
rtb.SelectionColor = keywordColor
ChangeWordColorRecusively(rtb, keyword, keywordColor,
keywordPosition + keyword.Length)

End If

End Sub

End Class
'***

En espérant que ça t'inspires.. :O)

--
Cordialement
Yanick
MVP pour Visual Basic





Avatar
AlexC
La version de Zoury est bien interressante, personnellement j'avais utilisé
l'event texte_changed pour modifier en temps réel les couleurs de texte.
Mais c'est une usine à gaz ...
J'ai testé le code de Zouru qui chez moi, plante pour explosion de la pile
^^
en fait il faut modifier son code comme ceci :

Private Sub ChangeWordColorRecusively(ByVal rtb As RichTextBox, ByVal
keyword As String, ByVal keywordColor As Color, ByVal startIndex As Int32)

Dim keywordPosition As Int32 = rtb.Find(keyword, startIndex,
RichTextBoxFinds.WholeWord)

If (keywordPosition > -1) Then

rtb.Select(keywordPosition, keyword.Length)

rtb.SelectionColor = keywordColor

If keywordPosition + keyword.Length < RichTextBox1.TextLength Then

ChangeWordColorRecusively(rtb, keyword, keywordColor, keywordPosition +
keyword.Length)

End If

End If

End Sub



"404 found" a écrit dans le message de
news:
juste une dernière question : avec le bouton ça marche nickel !! k'ai fait
un
évenement sur keypress et je teste sur l'espace (pourchanger la couleur)

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
Select Case e.KeyChar
Case " "
RichTextBox1.SuspendLayout()
For Each entry As DictionaryEntry In m_motsCles
ChangeWordColorRecusively(RichTextBox1, entry.Key, entry.Value,
0)
Next
RichTextBox1.ResumeLayout()
End Select

mais il n'arrive pas à sortir de la récursivité dans
ChangeWordColorRecusively !!

bref, si vous voyez rapidement le problème, sinon j'y passerais le temps
nécessaire !!

En tout cas merci !!

"Zoury" a écrit :

> Est ce possible ?
> comment ?
> des idées ??

En voici une simple, mais il peut y avoir plusieurs façon de procéder :
'***
Public Class Form1
Inherits System.Windows.Forms.Form

Private m_motsCles As Hashtable

#Region " Code généré par le Concepteur Windows Form "

Public Sub New()
MyBase.New()

'Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()

'Ajoutez une initialisation quelconque après l'appel
InitializeComponent()

End Sub

'La méthode substituée Dispose du formulaire pour nettoyer la liste
des
composants.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Requis par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer

'REMARQUE : la procédure suivante est requise par le Concepteur
Windows
Form
'Elle peut être modifiée en utilisant le Concepteur Windows Form.
'Ne la modifiez pas en utilisant l'éditeur de code.
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(8, 8)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(416, 256)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "RichTextBox1 test Test RichTextBox"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(352, 272)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(432, 302)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' pour les besoins de l'exemple, j'ai employé une Hashtable
hard-codée,
' il est évidemment préférable de conserver la liste de mots en
dehors du programme (table de pilotage, .config, autre fichiers..)
' afin de permettre la modification de celle-ci sans devoir
modifié
le code.
m_motsCles = New Hashtable(3)
m_motsCles.Add("SELECT", Color.Blue)
m_motsCles.Add("FROM", Color.Red)
m_motsCles.Add("WHERE", Color.Green)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

RichTextBox1.SuspendLayout()
For Each entry As DictionaryEntry In m_motsCles
ChangeWordColorRecusively(RichTextBox1, entry.Key,
entry.Value,
0)
Next
RichTextBox1.ResumeLayout()

End Sub

Private Sub ChangeWordColorRecusively(ByVal rtb As RichTextBox, ByVal
keyword As String, ByVal keywordColor As Color, ByVal startIndex As
Int32)

Dim keywordPosition As Int32 = rtb.Find(keyword, startIndex,
RichTextBoxFinds.WholeWord)
If (keywordPosition > -1) Then

rtb.Select(keywordPosition, keyword.Length)
rtb.SelectionColor = keywordColor
ChangeWordColorRecusively(rtb, keyword, keywordColor,
keywordPosition + keyword.Length)

End If

End Sub

End Class
'***

En espérant que ça t'inspires.. :O)

--
Cordialement
Yanick
MVP pour Visual Basic







Avatar
Zoury
> personnellement j'avais utilisé l'event texte_changed pour modifier en
temps réel les couleurs de texte.



Pour de courte chaine, ça pourrait faire le travail... Mais plus ton texte
sera long plus ça laguera... Il faudrait opter pour une autre approche.


Mais c'est une usine à gaz ...
J'ai testé le code de Zouru qui chez moi, plante pour explosion de la pile
^^



Yep ! J'ai vraiment mal testé. :O)

Tant qu'à réécrire la méthode, je la ferais comme ceci :
'***
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

For Each entry As DictionaryEntry In m_motsCles
ChangeWordColor(RichTextBox1, entry.Key, entry.Value)
Next

End Sub

Private Sub ChangeWordColor(ByVal rtb As RichTextBox, ByVal keyword As
String, ByVal keywordColor As Color)

Dim keywordPosition As Int32 = rtb.Find(keyword, 0,
RichTextBoxFinds.WholeWord)
Do While ((keywordPosition > -1) AndAlso _
(keywordPosition + keyword.Length < rtb.Text.Length))

rtb.Select(keywordPosition, keyword.Length)
rtb.SelectionColor = keywordColor
keywordPosition = rtb.Find(keyword, _
keywordPosition + keyword.Length, _
RichTextBoxFinds.WholeWord)

Loop

End Sub
'***

Afin d'éviter la récusion inutile. Les performances n'en seront que
meilleures.


--
Cordialement
Yanick
MVP pour Visual Basic