Partage
  • Partager sur Facebook
  • Partager sur Twitter

Parser un texte d'une RichTextBox ...

Puis le colorer !

    28 mars 2011 à 18:34:49

    Bonjour !

    Je suis en train de créer un éditeur de texte en VB.NET, et je voudrais que le programme colorie certains mots, comme "Nombre" ou "Phrase". Voici mon code :

    Imports System
    Imports System.IO
    Imports System.Text
    
    Public Class Form1
        ' Get a handle to an application window.
        Declare Auto Function FindWindow Lib "USER32.DLL" ( _
            ByVal lpClassName As String, _
            ByVal lpWindowName As String) As IntPtr
    
        ' Activate an application window.
        Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
            (ByVal hWnd As IntPtr) As Boolean
    
        Public fichier As FileStream
        Public change(3) As Integer
    
        Sub SearchWord(ByVal word As String, ByVal newFont As FontStyle, ByVal colour As System.Drawing.Color)
    
            Dim result As Integer = 0
    
            result = Input1.Find(word, result, RichTextBoxFinds.NoHighlight)
    
            Do While (result > 0)
                Input1.Select(result, Len(word))
    
                Input1.SelectionFont = New Font(Input1.SelectionFont, newFont)
                Input1.SelectionColor = colour
                Input1.Select(result + Len(word), Input1.TextLength - (result + (Len(word) - 1)))
                Input1.SelectionFont = New Font(Input1.SelectionFont, FontStyle.Regular)
                Input1.SelectionColor = Color.Black
                result = Input1.Find(word, result + 1, RichTextBoxFinds.NoHighlight)
            Loop
    
            Input1.Select(Input1.TextLength, 0)
    
        End Sub
    
        Sub textOpenFile()
            If (openFile.ShowDialog() = Windows.Forms.DialogResult.OK) Then
    
                fichier = openFile.OpenFile()
                fichier.Close()
                Input1.Text = File.ReadAllText(fichier.Name)
    
                Me.Text = fichier.Name
            End If
        End Sub
    
        Function textSaveAsFile() As Integer
            If (saveFile.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                fichier = saveFile.OpenFile()
                fichier.Close()
                File.WriteAllText(fichier.Name, Input1.Text)
                Me.Text = fichier.Name
                Return 0
            Else
                Return 1
            End If
        End Function
    
        Sub textSaveFile()
            File.WriteAllText(fichier.Name, Input1.Text)
            Me.Text = fichier.Name
        End Sub
    
        Private Sub Input1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Input1.TextChanged
    
            SearchWord("::", FontStyle.Bold, Color.Blue)
            SearchWord("phrase", FontStyle.Bold, Color.Red)
            SearchWord("nombre", FontStyle.Bold, Color.Blue)
    
            If (Strings.Left(Me.Text, 1) <> "*") Then
                Me.Text = "*" + Me.Text
            End If
    
        End Sub
    
        Private Sub Nouveau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Nouveau.Click
            Dim result As Microsoft.VisualBasic.MsgBoxResult
    
            If (Strings.Left(Me.Text, 1) = "*") Then
                result = MsgBox("Il y a un fichier non enregistré, voulez-vous continuer ?", 51, "Fichier non enregistré")
                If (result = MsgBoxResult.Yes) Then
                    Input1.Text = ""
                    Me.Text = "prOOEdit - Sans titre"
                ElseIf (result = MsgBoxResult.No) Then
                    textSaveAsFile()
                End If
            Else
                Input1.Text = ""
                Me.Text = "prOOEdit - Sans titre"
            End If
    
            change(0) = 0
            change(1) = 0
            change(2) = 0
        End Sub
    
        Private Sub Ouvrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ouvrir.Click
    
            textOpenFile()
    
        End Sub
    
        Private Sub Enregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enregistrer.Click
    
            If (Me.Text = "prOOEdit - Sans titre" Or Me.Text = "*prOOEdit - Sans titre") Then
                textSaveAsFile()
            Else
                textSaveFile()
            End If
    
        End Sub
    
        Private Sub onForm1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            Dim result As MsgBoxResult
    
            If (Strings.Left(Me.Text, 1) = "*") Then
                result = MsgBox("Abandonner les modifications de " & Me.Text.Remove(0, 1) & " ?", 51, "Fichier non enregistré")
                If (result = MsgBoxResult.No) Then
                    textSaveAsFile()
                ElseIf (result = MsgBoxResult.Cancel) Then
                    e.Cancel = True
                End If
            End If
        End Sub
    
        Private Sub Exécuter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exécuter.Click
    
            Dim appli As IntPtr
    
            If (Me.Text = "*prOOEdit - Sans titre" Or Me.Text = "prOOEdit - Sans titre") Then
    
                If (Not textSaveAsFile()) Then
                    Shell("E:\prOOven\prOOven.exe " & Me.Text)
                End If
    
            ElseIf (Strings.Left(Me.Text, 1) = "*") Then
                textSaveFile()
                Shell("E:\prOOven\prOOven.exe " & Me.Text)
            Else
                Shell("E:\prOOven\prOOven.exe " & Me.Text)
            End If
    
            appli = FindWindow(0&, "prOOven.exe")
            SetForegroundWindow(appli)
    
        End Sub
    
        Private Sub EnregistrerSous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSous.Click
            textSaveAsFile()
        End Sub
    End Class
    


    Quand j'exécute, tout marche bien sauf le parsing : quand j'écris un mot-clé, il le colorie mais il y a une chance sur 3 qu'il colorie le 2e puis 1 sur 100 pour le 3e !

    C'est vraiment bizarre !
    • Partager sur Facebook
    • Partager sur Twitter
      28 mars 2011 à 23:45:36

      Je fais une proposition très aléatoire, est-ce parce que ta fonction SearchWord(mot), ne n'exécute que sur un seul mot ?
      Si oui, et bien assure-toi de le faire pour chacun (si tu ne vois pas comment, j'imaginerai une petite solution)
      • Partager sur Facebook
      • Partager sur Twitter
        29 mars 2011 à 7:43:25

        Non, d'une part parce qu'on ne cherche qu'un mot à la fois, et de deux plusieurs mots peuvent se colorier, et plusieurs fois le même.
        • Partager sur Facebook
        • Partager sur Twitter

        Parser un texte d'une RichTextBox ...

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown