Moi!!!
Olis tässä semmonen ongelma, etä teen koodille väritystä ja se ei toimi. Koodi:
Function colors(ByVal box As RichTextBox)
Dim text As String = box.Text
box.Clear()
Dim merkki As Char = ""
Dim mCounter As Long = 1
Dim lainauschr As Boolean = False
Do
merkki = Mid(text, mCounter, 1)
Select Case merkki
Case """"
If lainauschr = True Then
box.SelectionColor = Color.Black
lainauschr = False
Else
box.SelectionColor = Color.Red
lainauschr = True
End If
End Select
box.Text = box.Text & merkki
mCounter += 1
Application.DoEvents()
Loop Until mCounter = Len(text) + 1
End FunctionMikä mättää??
Pitääkö texti jotenkin "Printata" sinne boxiin??
Et missään vaiheessa määritä valintaa siellä richtextboxissa. Jos käyttäjä onnistuu tuon pyörimisen aikana valitsemaan jonkun pätkän tekstistä, niin sitten tuo skriptisi vaihtelee sen kyseisen pätkän väriä, kunnes lopulta väriksi jää se väri mikä viimeisen merkin perusteella olisi.
Luultavasti seuraava toimisi paremmin (ei kokeiltu)
Sub colors(ByVal box As RichTextBox)
Dim i As Integer, lainauschr As Boolean
For i = 0 To box.Text.Length - 1
box.SelectionStart = i
box.SelectionLength = 1
Select Case box.SelectedText
Case """"
If lainauschr = True Then
box.SelectionColor = Color.Black
lainauschr = False
Else
box.SelectionColor = Color.Red
lainauschr = True
End If
End Select
Application.DoEvents()
Next
End SubJoo täytyy kopioida se.
Teen hommaa omalla koneellani ja tietysti elisan/saunalahden verkot ovat kaatuneet joten tulin toiselle koneelle katsomaan tänne.
Joo toimii kyllä(raahasin sen tossa nopeesti omalle koneelle muistitikulla)
mutta täytyy lisätä vielä yksi muuttuja. Se osoittaisi että on tullut toinen lainausmerkki ja... jotain tonne päin
Mä osasin tehä sen!
Sub colors(ByVal box As RichTextBox)
Dim i As Integer, lainauschr As Boolean, redtext As Boolean = False
For i = 0 To box.Text.Length - 1
box.SelectionStart = i
box.SelectionLength = 1
If lainauschr = True Then
box.SelectionColor = Color.Red
End If
If redtext = True Then
box.Select(i - 1, 1)
box.SelectionColor = Color.Red
redtext = False
GoTo appdoevents
End If
Select Case box.SelectedText
Case """"
If lainauschr = True Then
box.SelectionColor = Color.Black
lainauschr = False
redtext = True
Else
box.SelectionColor = Color.Red
lainauschr = True
End If
End Select
appdoevents:
Application.DoEvents()
Next
End SubMättää taas! Noi keywordit...
Module ModuleColoring
Sub colorsred(ByVal box As RichTextBox)
Dim i As Integer, lainauschr As Boolean, redtext As Boolean = False
For i = 0 To box.Text.Length
box.SelectionStart = i
box.SelectionLength = 1
If lainauschr = True Then
box.SelectionColor = Color.Red
End If
If redtext = True Then
box.Select(i - 1, 1)
box.SelectionColor = Color.Red
redtext = False
GoTo appdoevents
End If
Select Case box.SelectedText
Case """"
If lainauschr = True Then
box.SelectionColor = Color.Black
lainauschr = False
redtext = True
Else
box.SelectionColor = Color.Red
lainauschr = True
End If
End Select
appdoevents:
Application.DoEvents()
Next
End Sub
Sub colorscomment(ByVal box As RichTextBox)
Dim i As Integer, lainauschr As Boolean, redtext As Boolean = False
For i = 0 To box.Text.Length
box.SelectionStart = i
box.SelectionLength = 1
If lainauschr = True Then
box.SelectionColor = Color.Orange
End If
If redtext = True Then
box.Select(i - 1, 1)
box.SelectionColor = Color.Orange
redtext = False
GoTo appdoevents
End If
Select Case box.SelectedText
Case "'"
If lainauschr = True Then
box.SelectionColor = Color.Black
lainauschr = False
redtext = True
Else
box.SelectionColor = Color.Orange
lainauschr = True
End If
End Select
appdoevents:
Application.DoEvents()
Next
End Sub
Sub colDefine(ByVal box As RichTextBox)
Dim i As Integer, lainauschr As Boolean, redtext As Boolean = False
For i = 0 To box.Text.Length
box.SelectionStart = i
box.SelectionLength = 1
If lainauschr = True Then
box.SelectionColor = Color.LightGray
End If
If redtext = True Then
box.Select(i - 1, 1)
box.SelectionColor = Color.Aqua
redtext = False
GoTo appdoevents
End If
Select Case box.SelectedText
Case "#"
If lainauschr = True Then
box.SelectionColor = Color.Black
lainauschr = False
redtext = True
Else
box.SelectionColor = Color.Aqua
lainauschr = True
End If
End Select
appdoevents:
Application.DoEvents()
Next
End Sub
Sub colnumbers(ByVal box As RichTextBox)
Dim i As Integer = 1
Dim merkki As Char = ""
Do
merkki = Mid(box.Text, i, 1)
box.Select(i - 1, 1)
Select Case Asc(merkki)
Case 48 To 57
'numero
box.SelectionColor = Color.Brown
End Select
i += 1
Loop Until i = box.Text.Length + 1
End Sub
Sub colKeywords(ByVal box As RichTextBox)
On Error GoTo virhe
Dim sanat(14) As String
Dim i As Integer = 0
Dim paikka As Integer = 0
Dim sana As String = ""
Dim merkki As Char = ""
sanat(0) = "print"
sanat(1) = "scan"
sanat(2) = "end"
sanat(3) = "private"
sanat(4) = "public"
sanat(5) = "int"
sanat(6) = "char"
sanat(7) = "long"
sanat(8) = ";"
sanat(9) = "move"
sanat(10) = "if"
sanat(11) = "then"
sanat(12) = "elseif"
sanat(13) = "else"
sanat(14) = "endif"
nextWord: 'seuraava sana
paikka = i 'sanan alku
i += 1
Do
If i = box.Text.Length Then Exit Sub
merkki = Mid(box.Text, i, 1)
If merkki = " " Then Exit Do
sana = sana & merkki
i += 1
Loop
tarkistus: 'tarkistetaan, ettei sana ole avainsana
For n As Integer = 0 To sanat.Length - 1
If sana = sanat(n) Then
'sana on avainsana (keyword)!
'värjätään siitä sininen!
box.SelectionStart = paikka
box.SelectionLength = i
box.SelectionColor = Color.Blue
sana = ""
merkki = ""
GoTo nextWord
End If
Next
sana = ""
merkki = ""
GoTo nextWord
Exit Sub
virhe:
Exit Sub
End Sub
Sub colors(ByVal box As RichTextBox)
Dim curspos As Point
curspos = Cursor.Position
colKeywords(box)
colorsred(box)
colorscomment(box)
colDefine(box)
colnumbers(box)
Cursor.Position = curspos
End Sub
End Moduleyäääääh. Sori, mutta itku meinas päästä tuota lukiessa. No jos nyt otan kantaa vain siihen miksi tuo ei toimi, niin ensinnäkin tuohan toimii jos noi avainsanat mitä sulla on, on kirjoitettu pienellä siellä koodissakin.
Eli ehkäpä muuttaisin vertailun kirjainkokoa huomioimattomaksi rivillä 138:
If sana = sanat(n) Then 'tilalle --> If String.Compare(sana, sanat(n), StringComparison.InvariantCultureIgnoreCase) = 0 Then
Epäilen muuten, ettei värittimesi tule löytämään hirveästi endif avainsanoja.
Ajatele: Melkein kaiken koodin tein siitä esimerkistä jonka sinä teit!
Muuten tuo väritys melkein toimii, mutta se värittää vähän muutakin kuin pelkät avainsanat.
Aihe on jo aika vanha, joten et voi enää vastata siihen.