none
coloreado de codigo en visual basic 2010 ayuda muy lento el coloreado RRS feed

  • Pregunta

  • hola buenas noches aqui les dejo mi duda con este codigo.

    bueno estoy haciendo un editor de codigo porcierto lla lo e echo pero el problema esta en la sintaxis del codigo que escribo por ejp hola algunas 50 vese y el programa se buel be requete lento aqui les dejo el codigo que e echo hasta el momento.

     

    Option Explicit On
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Data
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Text.RegularExpressions
    Imports System.IO
    Public Class Form1
        Dim Colores(2) As Color
        Public Sub New()
            InitializeComponent()
            'Label1.Font = New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size + 1.019F)
        End Sub
     
        Private Sub updateNumberLabel()
            'we get index of first visible char and number of first visible line
            Dim pos As New Point(0, 0)
            Dim firstIndex As Integer = RichTextBox1.GetCharIndexFromPosition(pos)
            Dim firstLine As Integer = RichTextBox1.GetLineFromCharIndex(firstIndex)
            'now we get index of last visible char and number of last visible line
            pos.X = ClientRectangle.Width
            pos.Y = ClientRectangle.Height
            Dim lastIndex As Integer = RichTextBox1.GetCharIndexFromPosition(pos)
            Dim lastLine As Integer = RichTextBox1.GetLineFromCharIndex(lastIndex)
            'this is point position of last visible char, we'll use its Y value for calculating numberLabel size
            pos = RichTextBox1.GetPositionFromCharIndex(lastIndex)
            'finally, renumber label 
            Label1.Text = ""
            For i As Integer = firstLine To lastLine + 1
                Label1.Text += i + 1 & "" & vbLf & ""
            Next
        End Sub
        Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
            updateNumberLabel()
            Label1.Refresh()
            ' RichTextBox1.Refresh()
            RichTextBox1.Enabled = False
            Dim Pos As Long
            Dim reg As Regex
            Dim partes As MatchCollection
            Dim selStart As Integer = RichTextBox1.SelectionStart
            Dim selLength As Integer = RichTextBox1.SelectionLength
            With RichTextBox1
                Pos = .SelectionStart
                CambiaColores(RichTextBox1)
                .SelectionStart = Pos
                .Focus()
     
                ' Buscamos todas la palabras reservadas
                reg = New Regex("(while true do |char |for |if |end |then |else |System|system)")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.Blue
     
                Next
                ' Buscamos todas las cadenas
                reg = New Regex("[""][^""]*[""]")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.Gray
     
                Next
                ' Buscamos todas la palabras reservadas
                reg = New Regex("(1|2|3|4|5|6|7|8|9|0)")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.Orange
     
                Next
                ' Buscamos todas las cadenas
                reg = New Regex("[(]|[)]")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.LightBlue
     
                Next
                ' Buscamos todas la palabras reservadas
                reg = New Regex("(screen)")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.Purple
     
                Next
                ' Buscamos todas la palabras reservadas
                reg = New Regex("(print|Print)")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.CornflowerBlue
     
                Next
                ' Buscamos todas la palabras reservadas
                reg = New Regex("(new|New)")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.CornflowerBlue
     
                Next
                ' Buscamos todas la palabras reservadas
                reg = New Regex("(=)")
                partes = reg.Matches(RichTextBox1.Text)
                For i As Integer = 0 To partes.Count - 1
                    RichTextBox1.SelectionStart = partes(i).Index
                    RichTextBox1.SelectionLength = partes(i).Length
                    RichTextBox1.SelectionColor = Color.Red
     
                Next
                RichTextBox1.SelectionStart = selStart
                RichTextBox1.SelectionLength = selLength
                RichTextBox1.Enabled = True
                RichTextBox1.Focus()
            End With
        End Sub
        Sub CambiaColores(ByVal RichTextBox1 As RichTextBox)
     
            ' RichTextBox1.Enabled = False
            ' Guardamos la selecciona actual para regresar
            ' todo a su sitio cuando terminemos
            Dim selStart As Integer = RichTextBox1.SelectionStart
            Dim selLength As Integer = RichTextBox1.SelectionLength
     
            Dim F As Long
            Dim Contador As Long
            With RichTextBox1
                .Enabled = False
                For F = 1 To Len(.Text)
                    If Mid$(.Text, F, 2) = "--" Then
                        Contador = Contador + 1
                        If Contador > 2 Then Contador = 2
                        .SelectionStart = F - 1
                        .SelectionLength = 1
                        .SelectionColor = Colores(Contador)
                    ElseIf Mid$(.Text, F, 1) = vbLf Then
                        .SelectionStart = F - 1
                        .SelectionLength = 1
                        .SelectionColor = Colores(Contador)
                        Contador = Contador - 1
                        If Contador < 0 Then Contador = 0
                    Else
                        .SelectionStart = F - 1
                        .SelectionLength = 1
                        .SelectionColor = Colores(Contador)
                    End If
                Next F
            End With
     
        End Sub
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            updateNumberLabel()
            Colores(0) = Color.Black
            Colores(1) = Color.Green
            Me.Show()
        End Sub
     
        Private Sub CortarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CortarToolStripMenuItem.Click
            RichTextBox1.Cut()
        End Sub
     
        Private Sub CopiarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopiarToolStripMenuItem.Click
            RichTextBox1.Copy()
        End Sub
     
        Private Sub PegarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PegarToolStripMenuItem.Click
            RichTextBox1.Paste()
        End Sub
     
        Private Sub RichTextBox1_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.FontChanged
            updateNumberLabel()
            RichTextBox1_VScroll(Nothing, Nothing)
        End Sub
        Private Sub RichTextBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.Resize
            RichTextBox1_VScroll(Nothing, Nothing)
        End Sub
     
        Private Sub RichTextBox1_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll
            'move location of numberLabel for amount of pixels caused by scrollbar
            Dim d As Integer = RichTextBox1.GetPositionFromCharIndex(0).Y Mod (RichTextBox1.Font.Height + 1)
            'Label1.Location = New Point(0, d)
            updateNumberLabel()
        End Sub
    End Class
    

    • Cambiado Enrique M. Montejo domingo, 13 de mayo de 2012 17:01 controles de Windows Forms (De:Lenguaje VB.NET)
    lunes, 19 de diciembre de 2011 1:10

Todas las respuestas

  • se vuelve lento porque estas parseando la sintaxis en cada cambio que se realiza en el RichTextBox

    si dejarias que la persona escribiera el texto y al quitar el foco se aplciaria el parse serguro seria mas rapido

     

    o tal vez si harias el parse en segundo plano, quizas usando thread (o sea hilos)

    saludos


    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina
    lunes, 19 de diciembre de 2011 1:47
  • bueno como tedigo el problema es que soy un nobato en visual basic e echo esto basandome en un codigo viejo que tenia echo en c# poreso si me pudieras facilitar un ejemplo seria mas facil uso visual studio 2010 por fabor lo nesecito.

    gracias por contestar tan rapido un saludo y gracias.

    lunes, 19 de diciembre de 2011 16:35
  • como llo hago lo que tu me dices

     

    martes, 20 de diciembre de 2011 12:52