Solved

Color Code

Posted on 2000-04-03
15
138 Views
Last Modified: 2010-05-02
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
Comment
Question by:tsa2
  • 7
  • 6
  • 2
15 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2681664
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2681798
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
 

Author Comment

by:tsa2
ID: 2681850
Well, can you include a sample project on your site or send me it through E-Mail: tsalaptop@teknoworkz.com
Thanks.
0
 
LVL 1

Expert Comment

by:PatrickVD
ID: 2681920
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
 

Author Comment

by:tsa2
ID: 2681924
Well, can you include a sample project on your site or send me it through E-Mail: tsalaptop@teknoworkz.com
Thanks.
0
 

Author Comment

by:tsa2
ID: 2681954
Excuse the ignorance, but is there
any easier sample that I can use?
0
 

Author Comment

by:tsa2
ID: 2682007
Excuse the ignorance, but is there
any easier sample that I can use?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:PatrickVD
ID: 2682182
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2682464
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2682535
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
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
ID: 2688800
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2688804
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
 

Author Comment

by:tsa2
ID: 2689833
Thanks for your help guys.
I appreciate it very much.
I hope I can get this to work

Regards,
tsa2
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2690369
you havent tried it out yet? And I have to ask why you gave a B
0
 

Author Comment

by:tsa2
ID: 2691846
I did try it out. Not quite the answer I was expecting, but nontheless, it was good. Thanks for your help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now