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

Change color of specific value in a richtexbox

Posted on 2013-06-27
3
168 Views
Last Modified: 2013-06-27
I am attempting to use the following code to change the color of all the 1s in my field.
The field looks like this.
0010 1110 0000 1010 0001 1111 1000 0000 0110

The whole field ends up red instead of just the 1s.

Private Sub HighLightWords(ByVal Bitmap As String, ByVal myColor As Color)
        ''http://www.vbforums.com/showthread.php?125371-Change-Text-Color-and-Font-Character-by-Character
        Dim strWord As String
        Dim lPos As Long
 
        strWord = "1"
        If Bitmap = "Bitmap1" Then
            lPos = InStr(1, Me.txtBinary1.Text, strWord, vbTextCompare)
 
            If lPos > 0 Then
                With txtBinary1
                    .SelectionStart = lPos - 1
                    .SelectionLength = Len(strWord)
                    .SelectionColor = myColor
                    .SelectionStart = Len(Me.txtBinary1.Text)
                End With
            End If
        Else
            lPos = InStr(1, Me.txtBinary2.Text, strWord, vbTextCompare)
            If lPos > 0 Then
                With txtBinary2
                    .SelectionStart = lPos - 1
                    .SelectionLength = Len(strWord)
                    .SelectionColor = myColor
                    .SelectionStart = Len(Me.txtBinary2.Text)
                End With
            End If
        End If
End sub
0
Comment
Question by:WINN2012
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39281118
this code will color all the 1s in red:
Dim i = 0
        For Each c In Me.txtBinary1.Text
            If c = "1"c Then
                With RichTextBox1
                    .SelectionStart = i
                    .SelectionLength = 1
                    .SelectionColor = Drawing.Color.Red
                End With
            End If
            i = i + 1
        Next

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39281137
here's your function:

    Private Sub HighLightWords(ByVal Bitmap As String, ByVal myColor As Color)
        Dim i = 0
        Dim ctrl As RichTextBox

        If Bitmap = "Bitmap1" Then
            ctrl = Me.txtBinary1.Text

        Else
            ctrl = Me.txtBinary2.Text
        End If

        For Each c In ctrl.Text
            If c = "1"c Then
                With ctrl
                    .SelectionStart = i
                    .SelectionLength = 1
                    .SelectionColor = myColor
                End With
            End If
            i = i + 1
        Next

    End Sub

Open in new window

0
 

Author Closing Comment

by:WINN2012
ID: 39281923
Worked like a champ..tyvm
I think I couldnt get it to work because I was not incrementing the counter. So it just started over again.

I do have a hiccup though, the first time i run the code it works and all 1s are red as desired, if I make a change on my form which triggers the code again, the entire line is red again.
I'm sure I'll figure it out.

MW
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net Report Printing Issue 3 52
Obtain cell value using column ID in DataGrid using VB.NET 2 24
Very Large data in MYSQL 7 73
Need syntax help building hyperlink 2 20
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

789 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