Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [VB.NET 2012] codice syntax highlighter

    Ho trovato questo codice per il syntax highlighter.. potete aiutarmi a capirlo e come aggiungere altre parole ma con colori diversi?

    http://www.tmyaccount.altervista.org/file.txt

    Grazie in anticipo per l'aiuto :3

    P.s è troppo lungo, non sono riuscito a inserirlo :3

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    La tua è una richiesta troppo generica ... che vuoi dire "aiutarti a capirlo"?

    In un forum tecnico si fanno domande precise ... e comunque il codice non mi sembra impossibile da inserire nel post ...

    codice:
    Public Class SyntaxRTB
    
        Inherits System.Windows.Forms.RichTextBox
    
        Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
           (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    
        Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer
    
        Private _SyntaxHighlight_CaseSensitive As Boolean = False
    
        Private Words As New DataTable
    
        'Contains Windows Messages for the SendMessage API call
        Private Enum EditMessages
            LineIndex = 187
            LineFromChar = 201
            GetFirstVisibleLine = 206
            CharFromPos = 215
            PosFromChar = 1062
        End Enum
    
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            ColorVisibleLines()
        End Sub
    
        Public Sub ColorRtb()
            Dim FirstVisibleChar As Integer
            Dim i As Integer = 0
    
            While i < Me.Lines.Length
                FirstVisibleChar = GetCharFromLineIndex(i)
                ColorLineNumber(i, FirstVisibleChar)
                i += 1
            End While
        End Sub
    
        Public Sub ColorVisibleLines()
            Dim FirstLine As Integer = FirstVisibleLine()
            Dim LastLine As Integer = LastVisibleLine()
            Dim FirstVisibleChar As Integer
    
            If (FirstLine = 0) And (LastLine = 0) Then
                'If there is no text it will error, so exit the sub
                Exit Sub
            Else
                While FirstLine < LastLine
                    FirstVisibleChar = GetCharFromLineIndex(FirstLine)
                    ColorLineNumber(FirstLine, FirstVisibleChar)
                    FirstLine += 1
                End While
            End If
    
        End Sub
    
        Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
            Dim i As Integer = 0
            Dim Instance As Integer
            Dim LeadingChar, TrailingChar As String
            Dim SelectionAt As Integer = Me.SelectionStart
            Dim MyRow As DataRow
            Dim Line() As String, MyI As Integer, MyStr As String
    
            ' Lock the update
            LockWindowUpdate(Me.Handle.ToInt32)
    
            MyI = lStart
    
            If CaseSensitive Then
                Line = Split(Me.Lines(LineIndex).ToString, " ")
            Else
                Line = Split(Me.Lines(LineIndex).ToLower, " ")
            End If
    
            For Each MyStr In Line
                Me.SelectionStart = MyI
                Me.SelectionLength = MyStr.Length
    
                If Words.Rows.Contains(MyStr) Then
                    MyRow = Words.Rows.Find(MyStr)
                    If (Not CaseSensitive) Or (CaseSensitive And MyRow("Word") = MyStr) Then
                        Me.SelectionColor = Color.FromName(MyRow("Color"))
                    End If
                Else
                    Me.SelectionColor = Color.Black
                End If
    
                MyI += MyStr.Length + 1
            Next
    
            ' Restore the selectionstart
            Me.SelectionStart = SelectionAt
            Me.SelectionLength = 0
            Me.SelectionColor = Color.Black
    
            ' Unlock the update
            LockWindowUpdate(0)
        End Sub
    
        Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
            Return SendMessage(Me.Handle, EditMessages.LineIndex, LineIndex, 0)
        End Function
    
        Public Function FirstVisibleLine() As Integer
            Return SendMessage(Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
        End Function
    
        Public Function LastVisibleLine() As Integer
            Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)
    
            If LastLine > Me.Lines.Length Or LastLine = 0 Then
                LastLine = Me.Lines.Length
            End If
    
            Return LastLine
        End Function
    
        Public Sub New()
            Dim MyRow As DataRow
            Dim arrKeyWords() As String, strKW As String
    
            Me.AcceptsTab = True
    
            ''Load all the keywords and the colors to make them 
            Words.Columns.Add("Word")
            Words.PrimaryKey = New DataColumn() {Words.Columns(0)}
            Words.Columns.Add("Color")
    
            arrKeyWords = New String() {"select", "insert", "delete", _
               "truncate", "from", "where", "into", "inner", "update", _
               "outer", "on", "is", "declare", "set", "use", "values", "as", _
               "order", "by", "drop", "view", "go", "trigger", "cube", _
               "binary", "varbinary", "image", "char", "varchar", "text", _
               "datetime", "smalldatetime", "decimal", "numeric", "float", _
               "real", "bigint", "int", "smallint", "tinyint", "money", _
               "smallmoney", "bit", "cursor", "timestamp", "uniqueidentifier", _
               "sql_variant", "table", "nchar", "nvarchar", "ntext", "left", _
               "right", "like", "and", "all", "in", "null", "join", "not", "or"}
    
            For Each strKW In arrKeyWords
                MyRow = Words.NewRow()
                MyRow("Word") = strKW
                MyRow("Color") = Color.LightCoral.Name
                Words.Rows.Add(MyRow)
            Next
    
        End Sub
    
        Public Property CaseSensitive() As Boolean
            Get
                Return _SyntaxHighlight_CaseSensitive
            End Get
            Set(ByVal Value As Boolean)
                _SyntaxHighlight_CaseSensitive = Value
            End Set
        End Property
    
    End Class
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Voglio capire in che modo riesce a colorare alcune parole in una richtextbox e come aggiungere altri insiemi di parole con colori diversi :3

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Le parole chiave e i colori associati sono preparati in

    Public Sub New()


    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Heh, come faccio ad aggiungerne un'altra?

  6. #6
    Utente di HTML.it
    Registrato dal
    Mar 2014
    residenza
    Vicenza
    Messaggi
    318
    Mi sembra abbastanza banale la cosa.

    Ti crei un altro array di stringhe (come arrKeyWords) con le parole che vuoi tu, poi rifai il ciclo simile a For Each strKW In arrKeyWords mettendo le tue parole e specificando il colore che vuoi

    Guardati la DataTable chiamata Words, le parole e i colori relativi sono dentro lì, per cui puoi specificare per ogni parola un colore diverso.

    Sergio
    Ultima modifica di SirJo; 23-03-2014 a 13:54

  7. #7
    Come non detto, risolto, grazie mille :*
    Ultima modifica di Trommo; 23-03-2014 a 14:04

  8. #8
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Speravo che la mia indicazione ti bastasse ad arrivare da solo ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.