?
Solved

Different character color in Rich Text Box

Posted on 2007-12-05
5
Medium Priority
?
735 Views
Last Modified: 2012-08-14
Hi everybody,

I am new here ...I have some problems to give color to exact words in RTB...I searched the forum I get some answer about this issue but I couldnt find and understand these...My problem is :

I have some files that their extensions are  *.eia   that can be opened with Notepad.
 I can load these files from thier location into RTB in my project.
This files like this:

N1 ..............
N2 G1 X......  Y........ Z......
N3       X...... Y........ Z......
N4 G1 X.....Y......Z.....
N5 G2 X......Y.....Z..... f3000
G0
N6......
.
after loading I can see these lines in my RTB..

This is the problem:

I want to see just "G1" s   as Green   and also
I want to see just "G2" s   as Yellow   in my RTB when I click a command buton. How can I do these??
..I know some topics about this issue in this forum but I couldnt adapt them to my project..Because I couldnt understand the logic about these...Sellenght SelColor??..I couldnt do...
0
Comment
Question by:hakanogu
[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
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
tward earned 200 total points
ID: 20413911
Try this in the Click event of the button:

  Dim Counter As Long
  Dim StringPos As Long
 
  For Counter = 1 To Len(Me.RichTextBox1.Text)
     
     StringPos = InStr(Counter, Me.RichTextBox1.Text, " G1 ", vbTextCompare)
     
     If StringPos > 0 Then
       
       Me.RichTextBox1.SelStart = StringPos
       Me.RichTextBox1.SelLength = 2
       Me.RichTextBox1.SelColor = vbGreen
       
       Counter = StringPos + 4
       
     Else
       
       Exit For
       
     End If
     
  Next Counter
 
  For Counter = 1 To Len(Me.RichTextBox1.Text)
     
     StringPos = InStr(Counter, Me.RichTextBox1.Text, " G2 ", vbTextCompare)
     
     If StringPos > 0 Then
       
       Me.RichTextBox1.SelStart = StringPos
       Me.RichTextBox1.SelLength = 2
       Me.RichTextBox1.SelColor = vbYellow
       
       Counter = StringPos + 4
       
     Else
       
       Exit For
       
     End If
     
  Next Counter
0
 

Author Comment

by:hakanogu
ID: 20414117
thank you it seems work really.. But it doesnt work in my project..0n my project it gives error line "Me.RichTextBox1.SelColor = vbGreen"     it couldnt find " SelColor" properties.  

I tried the code above in other an empty project then I checked the "RichtextBox1. Selcolor"  yes I can see its properties. But in my project I am adding a new RTB, 1 couldnt see SelColor. 1 can see everyting "SelLenght, SelStart,SelSelect vb.." but no SelColor..What sholud I do?
0
 

Author Comment

by:hakanogu
ID: 20415283
I solved the problem thank you...
0
 

Author Comment

by:hakanogu
ID: 20443333
Hi again,
The codes works fine but I have a little bit problem...If our text have 400-500 line there is no problem, It just takes to change the color approx. 5-6 seconds..But If Our text have 80000-85000 lines, 1t takes approx. 1-1.5 minutes..Is there any other way to make faster to decrase this time..??

And the other question: I am writing "S3000" instead of "G2" in order to search that word, but the same codes doesnt work proper..What should I change to run the code in order to change the "S3000"s color.. I think 0t is relevant with  "Me.RichTextBox1.SelLength = 2" I changed its value" 5",  but I couldnt solve again..Could you help me?
0
 
LVL 7

Expert Comment

by:tward
ID: 20444106
Try this and see if it is faster:

Private Const StringsToFind = " G1 , G2 , S3000 "
Private Const ColorsToUse = "65280,65535,255"  ' vbGreen, vbYellow, vbRed '
  Dim Counter As Long
  Dim LastStart As Long
  Dim Strings() As String
  Dim TheColors() As String
 
  Strings = Split(StringsToFind, ",", -1, vbTextCompare)
  TheColors = Split(ColorsToUse, ",", -1)
   
  For Counter = 0 To UBound(Strings)
   
    LastStart = 0
 
    Do While LastStart <> -1 And LastStart < Len(Me.RichTextBox1.Text)
   
      LastStart = Me.RichTextBox1.Find(Strings(Counter), LastStart)
   
      If LastStart <> -1 Then
       
        Me.RichTextBox1.SelColor = Val(TheColors(Counter))
        LastStart = LastStart + Len(Strings(Counter))
       
      End If
   
    Loop
   
  Next Counter
 
End Sub
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

770 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