Solved

Color Code

Posted on 2000-04-03
15
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

751 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