11. april 2004 - 15:50Der er
13 kommentarer og 1 løsning
Syntax farvning i richtextbox
Er der nogen der har en god idé til hvordan jeg kan lave syntax farvning i en richtextbox, altså sådan at det kommer til at ligne VBA-editoren. Jeg forestiller mig noget med at man har en referencefil med VB-ord og så evaluerer man det indtastede op i mod denne fil, men hvordan skal det gøres?
Dim search& Public Function ColorWord(RTF As RichTextBox, Swords As String, Scolor As ColorConstants, _ SFontsize As Integer, Sbold As Boolean, Sitalic As Boolean, Sbullet As Boolean) search = 1 Do Until search = 0 search = InStr(search, RTF.Text, Swords, vbTextCompare) If search > 0 Then With RTF .SelStart = search - 1 .SelLength = Len(Swords) .SelColor = Scolor .SelFontSize = SFontsize .SelBold = Sbold .SelItalic = Sitalic .SelBullet = Sbullet End With search = search + Len(Swords) End If Loop With RTF .SelStart = Len(RTF.Text) .SelColor = vbBlack .SelFontSize = 8 .SelBold = False .SelItalic = False .SelBullet = False End With End Function
'********CALL THE FUNCTION FROM YOUR FORM LIKE THIS********
>>the_master Den første er da noget i den rigtige retning, meeen der er stadig en del problemer, f.eks. tager den også og farver DELE af et ord hvis det matcher et ord i ref.-filen.
Og det skal jo også være sådan at ord omsluttet af "-tegn ikke farves ligesom at kommentarer skal behandles særskilt (farves grønne).
Private Sub Command1_Click() HighlightWords RichTextBox1, "text", vbRed End Sub
Private Function HighlightWords(rtb As RichTextBox, _ sFindString As String, _ lColor As Long) _ As Integer
Dim lFoundPos As Long 'Position of first character 'of match Dim lFindLength As Long 'Length of string to find Dim lOriginalSelStart As Long Dim lOriginalSelLength As Long Dim iMatchCount As Integer 'Number of matches
'Save the insertion points current location and length lOriginalSelStart = rtb.SelStart lOriginalSelLength = rtb.SelLength
'Cache the length of the string to find lFindLength = Len(sFindString)
'Attempt to find the first match lFoundPos = rtb.Find(sFindString, 0, , rtfNoHighlight) While lFoundPos > 0 iMatchCount = iMatchCount + 1
rtb.SelStart = lFoundPos 'The SelLength property is set to 0 as 'soon as you change SelStart rtb.SelLength = lFindLength rtb.SelColor = lColor
'Attempt to find the next match lFoundPos = rtb.Find(sFindString, _ lFoundPos + lFindLength, , rtfNoHighlight) Wend
'Restore the insertion point to its original 'location and length rtb.SelStart = lOriginalSelStart rtb.SelLength = lOriginalSelLength
'Return the number of matches HighlightWords = iMatchCount
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.