Hrm... lol i wanna make it so that all the END's in a textbox turn red? any ideas how i could do that?
Printable View
Hrm... lol i wanna make it so that all the END's in a textbox turn red? any ideas how i could do that?
For starters, Textboxes can only have one forecolor. If you want to do that you may need a Rich Text Box.
i have a richtextbox i got a color code thing by umm someone on the forum but its not working im tyring to change the word END to red but it don't work :(
VB Code:
Private Sub HighlightText(sKeyword As String, iColour As Long) Dim nStart As Integer, sPrevChar As String, 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 = iColour 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 "END", vbRed End Sub
This works...
kk lol but it goes lower case :\ i need it to stay upper
Ucase(string)
still puts End instead of END :\
thats impossible. try this then.
Option Explicit
Private Sub Form_Load()
Text1.Text = Replace(Text1.Text, "end", UCase("end"))
End Sub
you may want to check this link out for some tips on color coding:
color coding tips
How about
VB Code:
RichTextBox1.SelStart = 1 ' Check First time do while RichTextBox1.SelStart > 0 ' do until not found anymore If RichTextBox1.SelStart <> 1 then RichTextbox1.SelStart + 3 ' Start at end of the last instance endif RichTextBox1.Find "END" ' search for "END" if RichTextBox1.SelStart > 0 then ' if found RichTextBox1.SelColor = iColor ' color it endif loop
EDIT: Changed to prevent endless loop.
The problem is the line that says StrConv(sKeyword, vbProperCase). ProperCase will change the first letter of the word to upper. Change the linetoVB Code:
.SelText = StrConv(sKeyword, vbProperCase)VB Code:
.SelText = sKeyword ' StrConv(sKeyword, vbProperCase)
Based on dglienna's suggesed code (which won't work... sorry, dude)However... this way you are not checking the previous and next character. Maybe you want to add this.VB Code:
Dim lStart As Long Do RichTextBox1.Find sKeyword, lStart ' search for "END" If RichTextBox1.SelStart < lStart Then 'We found all the ocurrences!! Exit Do End If If RichTextBox1.SelStart > 0 Then ' if found RichTextBox1.SelColor = iColour ' color it RichTextBox1.SelText = sKeyword End If lStart = RichTextBox1.SelStart + Len(sKeyword) + 1 Loop While RichTextBox1.SelStart > 0 ' do until not found anymore
I use this in my program. Whatever I search for is highlighted.
VB Code:
.SelStart = 0 .Find Trim(Line2) .SelAlignment = rtfCenter
and this
VB Code:
.Find ("[47]") .SelBold = 1 .SelText = Space(4) & "ESTIMATE ONLY"
so you could certainly use .Find, then .SelColor, then .SelText = "END",
but I don't think the replacement is even necessary, as shown in the first example. Whatever I .Find is selected. Whatever is selected is then Highlighted.
The .SelText = "END" part is to change the text to uppercase (in case it was not in upper letters).Quote:
Originally Posted by dglienna
so why wouldn't the other code work, then? are you saying that it might have been 'end' in the text? then I agree. he didn't state that. as far as searching for "END" and changing the color, that would probably work, as other formatting operations work. I highlight variables that way using BOLD.
The code you posted first does not work because it never exit the loop. It keeps looking and finding the first occurence (which is the only word that's highlighted) and never exists the loop. You have to break the code and exit manually or just STOP the execution.Quote:
Originally Posted by dglienna
Oh. I see now. I added that for multiple instances. You would have to stop resetting the start to .SelStart + 3 until SelStart = 0 (when it wasn't found)
The RichTextBox1.SelStart = 0 line was missing.Quote:
Originally Posted by dglienna
VB Code:
Dim lStart As Long RichTextBox1.SelStart = 0 '<--- Actually... this is the only line that was missing '(if no ocurrence is found) Do ' RichTextBox1.SelStart = 0 ' Cannot set to 0. Otherwise, always the first word would be found RichTextBox1.Find sKeyword, lStart ' search for "END" If RichTextBox1.SelStart < lStart Then 'We found all the ocurrences!! Exit Do End If If RichTextBox1.SelStart > 0 Then ' if found RichTextBox1.SelColor = iColour ' color it RichTextBox1.SelText = sKeyword End If lStart = RichTextBox1.SelStart + Len(sKeyword) + 1 Loop While RichTextBox1.SelStart > 0 ' do until not found anymore
OK.... lol so i got it so it stays in upper case but now i wanna make it so that if i have an If statement the whole thing turns blue like in this programming lang an if would look like(Wos scripting)
If T23, @hastoken23
(action if u don't got)
@hastoken23
(action if u got)
i wanna make it so that the If turns blue as well as the @hastoken23
aha ha :P i got it i just took out the
.SelStart = 0
from
richtextbox1_change()
thank you all ur the best
EDIT ok... ill give you an example of what it does now it puts
If T23, @hast23
in blue
but then i put an
END scene
and it turns END into red and scene is still in blue :\
EDIT again lol well now i took out
.SelLength = Len(.Text)
as well as
.SelStart = 0
and it seems to not do what it did before but now after i type a command
IF t23, @hast23 (in blue)
the action that i put will be in blue also :\
anyone know why?
Can you post your code to test it?Quote:
Originally Posted by Tempestknight
Sure
VB Code:
Option Explicit Private Sub HighlightText(sKeyword As String, iColour As Long) Dim nStart As Integer, sPrevChar As String, sNextChar As String nStart = InStr(1, UCase(RichTextBox1.Text), sKeyword, vbTextCompare) 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) Or sNextChar = ".") Then With RichTextBox1 .SelStart = nStart - 1 .SelLength = Len(sKeyword) .SelColor = iColour .SelText = sKeyword .SelStart = Len(RichTextBox1.Text) .SelColor = iColour End With End If nStart = InStr(nStart + Len(sKeyword), UCase(RichTextBox1.Text), sKeyword, vbTextCompare) Loop End Sub Private Sub RichTextBox1_Change() With RichTextBox1 .SelStart = 0 .SelColor = vbBlack .SelStart = Len(.Text) End With ' Add more custom words here HighlightText "END", vbRed HighlightText "GOTO", vbYellow HighlightText "IF", vbBlue End Sub
thats what i got i want it to go back to black after i type say GOTO or IF or END...
It does a little flicker... but can't think of another solution without messing around too much with the code.Quote:
Originally Posted by Tempestknight
VB Code:
Option Explicit Private Sub UnHighlightText(iColour As Long) Dim iPos As Long With RichTextBox1 iPos = .SelStart .SelStart = 0 .SelLength = Len(.Text) .SelColor = iColour .SelStart = iPos - 1 End With End Sub Private Sub HighlightText(sKeyword As String, iColour As Long) Dim nStart As Integer, sPrevChar As String, sNextChar As String Dim iPos As Long nStart = InStr(1, UCase(RichTextBox1.Text), sKeyword, vbTextCompare) 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) Or sNextChar = ".") Then With RichTextBox1 iPos = .SelStart .SelStart = nStart - 1 .SelLength = Len(sKeyword) .SelColor = iColour .SelText = sKeyword .SelStart = Len(RichTextBox1.Text) .SelColor = iColour .SelStart = iPos End With End If nStart = InStr(nStart + Len(sKeyword), UCase(RichTextBox1.Text), sKeyword, vbTextCompare) Loop End Sub Private Sub RichTextBox1_Change() With RichTextBox1 .SelStart = 0 .SelColor = vbBlack .SelStart = Len(.Text) End With ' Add more custom words here UnHighlightText vbBlack HighlightText "END", vbRed HighlightText "GOTO", vbYellow HighlightText "IF", vbBlue End Sub
if u test that u will see it ****s up
ok that still doesn't do what i want it to... it just keeps going same color like last time
I don't understand what you need then. Can you posted the sample text you want COLORED, please??Quote:
Originally Posted by Tempestknight
ok lol... say if i have
IF T23, @hast23
ok i want that whole line blue but then if i i have
IF T23, @hast23
1: hey
i want IF T23, @hast23 to be blue and the 1: hey to be black
So you want this:
IF T23, @hast23
1: hey
I think I deserve a couple of votes for all this help ;)
VB Code:
Option Explicit Private Sub UnHighlightText(iColour As Long) Dim iPos As Long With RichTextBox1 iPos = .SelStart .SelStart = 0 .SelLength = Len(.Text) .SelColor = iColour If iPos - 1 >= 0 Then .SelStart = iPos - 1 End If End With End Sub Private Sub HighlightText(sKeyword As String, iColour As Long) Dim nStart As Integer, sPrevChar As String, sNextChar As String Dim iPos As Long Dim sLine As String nStart = InStr(1, UCase(RichTextBox1.Text), sKeyword, vbTextCompare) 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) Or sNextChar = ".") Then With RichTextBox1 iPos = .SelStart .SelStart = nStart - 1 sLine = Mid$(.Text, .SelStart + 1) If InStr(sLine, vbCr) Then sLine = Left$(sLine, InStr(sLine, vbCr) - 1) End If .SelLength = Len(sLine) .SelColor = iColour ' .SelText = sKeyword ' .SelStart = Len(RichTextBox1.Text) ' .SelColor = iColour .SelStart = iPos End With End If nStart = InStr(nStart + Len(sKeyword), UCase(RichTextBox1.Text), sKeyword, vbTextCompare) Loop End Sub Private Sub RichTextBox1_Change() With RichTextBox1 .SelStart = 0 .SelColor = vbBlack .SelStart = Len(.Text) End With ' Add more custom words here UnHighlightText vbBlack HighlightText "END", vbRed HighlightText "GOTO", vbYellow HighlightText "IF", vbBlue End Sub
OK lol that all works thank you. but now when u go and say u have
END
and u wanna change it to
END SCENE
u have it and u try to edit it but it brings you to next line. like u have
END
and add scene to it youll end up with
END S
CENE
because it brings you to next line know why? lol and yes i will vote for you :P lol i already gave u one yesterday
HTH. I've helped you enough. You have the coloring thing working. All you need now is mess with the code to get it working like you want.... and I don't see you're grateful with the provided help.Quote:
Originally Posted by Tempestknight
VB Code:
Option Explicit Private Sub UnHighlightText(iColour As Long) Dim iPos As Long With RichTextBox1 iPos = .SelStart .SelStart = 0 .SelLength = Len(.Text) .SelColor = iColour If iPos - 1 >= 0 Then .SelStart = iPos ' - 1 End If End With End Sub Private Sub HighlightText(sKeyword As String, iColour As Long) Dim nStart As Integer, sPrevChar As String, sNextChar As String Dim iPos As Long Dim sLine As String nStart = InStr(1, UCase(RichTextBox1.Text), sKeyword, vbTextCompare) 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) Or sNextChar = ".") Then With RichTextBox1 iPos = .SelStart .SelStart = nStart - 1 sLine = Mid$(.Text, .SelStart + 1) If InStr(sLine, vbCr) Then sLine = Left$(sLine, InStr(sLine, vbCr) - 1) End If .SelLength = Len(sLine) .SelColor = iColour ' .SelText = sKeyword ' .SelStart = Len(RichTextBox1.Text) ' .SelColor = iColour .SelStart = iPos End With End If nStart = InStr(nStart + Len(sKeyword), UCase(RichTextBox1.Text), sKeyword, vbTextCompare) Loop End Sub Private Sub RichTextBox1_Change() ' Add more custom words here UnHighlightText vbBlack HighlightText "END", vbRed HighlightText "GOTO", vbYellow HighlightText "IF", vbBlue End Sub
lol i am very grateful lol.Its kinda hard to express feeling on a forum :\ lol u know. I always fiddle around with the code provided before asking questions. As well as search before i ask.... lol but thank you :D
If you read my signature you'll know what I mean.Quote:
Originally Posted by Tempestknight
Lol ill remember that :D but i try to give you a point it says
You must spread some Reputation around before giving it to Mc Brain again.
so as soon as i get a chance ill give you a point :D
Sorry about that. ;)Quote:
Originally Posted by Tempestknight
I feel realy bad asking this sinse u helped me so much but can anyone direct me to a link with all the built in VB colors?
I don't understand what you need.Quote:
Originally Posted by Tempestknight
lol like vbYellow vbRed vbBlue i dunno them all is there a website(i tired looking) where there is a list of em?
Here's MSDN's text...Quote:
Originally Posted by Tempestknight
Quote:
Originally Posted by MSDN
Thanks :D
You might also want to have a look at thr RGB function
Quote:
Originally Posted by MSDN