Solved

Color Code

Posted on 2000-04-03
15
172 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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