You know in vb if you type ' it turns it green, our you put if and it capitlaizes it and turns it blue, or if their is a problem with the statment it turns it red. Does anyone know how to do that, and get it to do it so fast?
Printable View
You know in vb if you type ' it turns it green, our you put if and it capitlaizes it and turns it blue, or if their is a problem with the statment it turns it red. Does anyone know how to do that, and get it to do it so fast?
Either use this code from Megatron:
Or from MartinLiss:Code:Option Explicit
Const vbDarkBlue = &H800000
Private Sub HighlightText(sKeyword As String, iColour As Long)
Dim nStart As Integer
Dim sPrevChar As String
Dim sNextChar As String
nStart = InStr(1, LCase(RichTextBox1.Text), sKeyword)
Do While nStart <> 0
If nStart > 1 Then
sPrevChar = Mid$(RichTextBox1.Text, nStart - 1, 1)
Else
sPrevChar = " "
End If
If Len(RichTextBox1.Text) >= nStart + Len(sKeyword) Then
sNextChar = Mid$(RichTextBox1.Text, nStart + Len(sKeyword), 1)
Else
sNextChar = " "
End If
If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
sPrevChar = Chr(10) Or sPrevChar = Chr(9)) And _
(sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
sNextChar = Chr(10) Or sNextChar = Chr(9)) Then
With RichTextBox1
.SelStart = nStart - 1
.SelLength = Len(sKeyword)
.SelColor = iColour
.SelText = StrConv(sKeyword, vbProperCase)
.SelStart = Len(RichTextBox1.Text)
.SelColor = vbBlack
End With
End If
nStart = InStr(nStart + Len(sKeyword), LCase(RichTextBox1.Text), sKeyword)
Loop
End Sub
Private Sub Form_Resize()
RichTextBox1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub RichTextBox1_Change()
With RichTextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SelColor = vbBlack
.SelStart = Len(.Text)
End With
' Add more custom words here
HighlightText "if", vbDarkBlue
HighlightText "then", vbDarkBlue
HighlightText "else", vbDarkBlue
HighlightText "for", vbDarkBlue
HighlightText "next", vbDarkBlue
HighlightText "do", vbDarkBlue
HighlightText "while", vbDarkBlue
HighlightText "until", vbDarkBlue
HighlightText "loop", vbDarkBlue
End Sub
Or from Serge:Code:Private Sub RichTextBox1_Change()
Static lngStart As Long
Dim lngFound As Long
Dim intLen As Integer
Const LOOK_FOR = "PRIVATE"
intLen = Len(LOOK_FOR)
If lngStart = 0 Then
lngStart = 1
End If
lngFound = InStr(lngStart, UCase(RichTextBox1.Text), LOOK_FOR)
If lngFound > 0 Then
RichTextBox1.SelStart = lngFound - 1
RichTextBox1.SelLength = lngFound + intLen
RichTextBox1.SelColor = vbBlue
RichTextBox1.SelStart = lngFound + intLen + 1
RichTextBox1.SelColor = vbBlack
lngStart = lngFound + intLen
End If
End Sub
Or....hehe, just kidding. They all work very well. Choose the one you desire :rolleyes:.Code:Public Sub ColorWords(pRich As RichTextBox, pWord As String, pColor As OLE_COLOR)
Dim iPos As Integer
With pRich
iPos = InStr(iPos + 1, .Text, pWord, vbTextCompare)
Do While iPos <> 0
.SelStart = iPos - 1
.SelLength = Len(pWord)
.SelColor = pColor
iPos = InStr(iPos + 1, .Text, pWord, vbTextCompare)
Loop
End With
End Sub
Private Sub Command1_Click()
ColorWords RichTextBox1, "Private", vbBlue
End Sub
On the subject of formated text .
does anyone know how to append formated text from an RTB to formated text in another RTB with a vbCrLf between them ?
[code]
Dim oldRTF, newrtf As String
oldRTF = rtbChat.SelText
newrtf = oldRTF & vbCrLf & strRTF$
rtbChat.SelRTF = newrtf
'scroll rtbChat down
rtbChat.SelStart = Len(rtbChat.Text)
rtbChat.SelLength = 0
[code]
that above doesn't seem to work ? I've been messing with this for awhile . If I'm Stupid please let me know .
I believe you can use one of the VB IDE's DLL's to do that, but I can't find the code anywhere :(