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
Solved

Color Code

Posted on 2000-04-03
15
155 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

839 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