Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Color Code

I am developing an app that color codes NC Language. I am using an RTF box to do it. How do I color code text while the user is typing, and when a file is opened?

I want to code all 'M's as vbBlue
Anything between parentheseis to be green, including the parentheseis.

Please help!
0
tsa2
Asked:
tsa2
  • 7
  • 6
  • 2
1 Solution
 
AzraSoundCommented:
in the keydown event you might try writing code that says if key = 'm' Or 'M' then selcolor = vbblue, else its whatever color the rest of your text is. same idea for parenthesis except use green
0
 
AzraSoundCommented:
as for the opening the file, you would need to implement the find method in a loop, do until find returns a -1 (which means it didnt find a match)  for each instance it finds the item change the color of the selected text, i'm sorry i am not offering code but i am not at home and i dont want to send the code unless i am sure it is fully functional
0
 
tsa2Author Commented:
Well, can you include a sample project on your site or send me it through E-Mail: tsalaptop@teknoworkz.com
Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PatrickVDCommented:
Hi tsa2,

You might want to have a look at this sample project. It's available on  :
http://www.freevbcode.com/ShowCode.Asp?ID=991

It is HTML Syntax Colorizer and a basic HTML editor which uses the 'syntax coloring when typing' functionality. So this might get you a giant step in the right direction.
It might take you some time to figure it all out, because it contains more than just coloring tags...

Hope this helps,

Patrick.
0
 
tsa2Author Commented:
Well, can you include a sample project on your site or send me it through E-Mail: tsalaptop@teknoworkz.com
Thanks.
0
 
tsa2Author Commented:
Excuse the ignorance, but is there
any easier sample that I can use?
0
 
tsa2Author Commented:
Excuse the ignorance, but is there
any easier sample that I can use?
0
 
PatrickVDCommented:
Hi tsa2,

Well, colouring on the fly is not an easy matter, but I did find some other links to similar code samples... May be those are easier to catch up with...
http://www.vbweb.co.uk/code/downloads/colortag.zip
http://www.vbweb.co.uk/code/downloads/htmlcolour.zip

Hope this helps.

Patrick.
0
 
AzraSoundCommented:
Private Sub rtf1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyM Then
        rtf1.SelColor = vbBlue
    Else
        rtf1.SelColor = vbBlack
    End If
End Sub


that will handle coloring on the fly
0
 
AzraSoundCommented:
sorry forgot about the green:

Dim stayGreen As Boolean


Private Sub Form_Load()
    stayGreen = False
End Sub

Private Sub rtf1_KeyDown(KeyCode As Integer, Shift As Integer)
    If stayGreen = True Then
        If KeyCode = vbKey0 And Shift = 1 Then
            stayGreen = False
        Else
            Exit Sub
        End If
    ElseIf KeyCode = vbKeyM Then
        rtf1.SelColor = vbBlue
    ElseIf KeyCode = vbKey9 And Shift = 1 Then
        rtf1.SelColor = vbGreen
        stayGreen = True
    Else
        rtf1.SelColor = vbBlack
    End If
End Sub

0
 
AzraSoundCommented:
As for when the file opens, there is no quick way other than recursively searching the text for the text in question and replacing it, this is the code in its entirety:

Dim stayGreen As Boolean
Dim i As Integer
Dim pos As Long
Dim one As Long
Dim two As Long



Private Sub cmdLoad_Click()
    rtf1.LoadFile "C:/Windows/Desktop/test.rtf"
    For pos = 0 To Len(rtf1.Text)
        rtf1.SelStart = pos
        rtf1.SelLength = 1
        If rtf1.SelText = "m" Then
            rtf1.SelColor = vbBlue
        Else
            rtf1.SelColor = vbBlack
        End If
        rtf1.SelLength = 0
    Next
    two = 1
    one = 1
    Do
    one = InStr(two, rtf1.Text, "(")
    If one = 0 Then Exit Sub
    two = InStr(one, rtf1.Text, ")")
    If two = 0 Then Exit Sub
    rtf1.SelStart = one - 1
    rtf1.SelLength = two - one + 1
    rtf1.SelColor = vbGreen
    rtf1.SelStart = two
    rtf1.SelColor = vbBlack
    Loop Until one = 0 Or two = 0
End Sub

Private Sub cmdSave_Click()
    rtf1.SaveFile "C:/Windows/Desktop/test.rtf"
End Sub

Private Sub Form_Load()
    stayGreen = False
End Sub

Private Sub rtf1_KeyDown(KeyCode As Integer, Shift As Integer)
    If stayGreen = True Then
        If KeyCode = vbKey0 And Shift = 1 Then
            stayGreen = False
        Else
            Exit Sub
        End If
    ElseIf KeyCode = vbKeyM Then
        'rtf1.SelColor = vbBlue
    ElseIf KeyCode = vbKey9 And Shift = 1 Then
        'rtf1.SelColor = vbGreen
        stayGreen = True
    Else
        rtf1.SelColor = vbBlack
    End If
End Sub


obviously you choose a different filename and path


Cheers!

0
 
AzraSoundCommented:
the commented out lines in the rtf1_keydown event shouldnt be commented out, just remove the apostrophes. i did that so i could test my loading and saving of the file without the coloring on the fly.
0
 
tsa2Author Commented:
Thanks for your help guys.
I appreciate it very much.
I hope I can get this to work

Regards,
tsa2
0
 
AzraSoundCommented:
you havent tried it out yet? And I have to ask why you gave a B
0
 
tsa2Author Commented:
I did try it out. Not quite the answer I was expecting, but nontheless, it was good. Thanks for your help.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now