Solved

Richtextbox Selection BackColor

Posted on 2008-06-18
14
848 Views
Last Modified: 2012-08-14
I am using a richtextbox that contains a string of letters that represents a strand of DNA.  For example,  CTTTGGGCCCCCC.   I have set the selection.backcolor to yellow for the three Ts.  I now have to assign a cost to each of the letters in the dna strand.  So i am looping through the string, parsing the text into one letter at a time, and i need to test the color of the selected character.  The first character C should have a selectedbackcolor of white, then the next three Ts should have a selectedbackcolor of yellow, then the rest of the string should be white.  However, when i call the .select the Ts that used to be yellow get set to whatever the selectedbackcolor property is at the time.  I would like them to stay yellow!  Can anyone suggest a better method of getting the backcolor of the selected letter?   Here is sample code:

For i = 0 To Me.RichTextBox1.TextLength - 1  'begin to loop thru string one character at a time
            x = Me.RichTextBox1.Text.Substring(i, 1)
            Me.RichTextBox1.Select(i, 1) 'select the character
            Select Case Me.RichTextBox1.SelectionBackColor'
                    Case Color.LightBlue
                        MsgBox("lb")
                    Case Color.Yellow
                        MsgBox("yellow")
                    Case Color.Orange
                        MsgBox("orange")
                End Select
                  Next
0
Comment
Question by:K_Watkins
  • 8
  • 6
14 Comments
 
LVL 25

Expert Comment

by:SStory
Comment Utility
Are you responding to any events when the RichTextBox (RTB) selection changes?
0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
Here's a backdoor way of doing it, in C#, but could be translated... they claim it is fast:
http://www.codeproject.com/KB/cs/RTFSyntaxColour.aspx

Also, I am wondering, depending upon your final purpose for the document... Is it text that you parse once and color or does it continually change?  If static, then you might even consider CSS/HTML and an embedded webbrowser.  If you color every single char different, then it might be considerably larger in filesize, somethinglike <span class="YelBlu">C</span>  if you had YelBlu defined in the CSS as Yellow on Blue...this would be required for coloring one letter.  However if 5 letters in a row needed that coloring, it would be less expensive in terms of file size.  (just another thought)
0
 

Author Comment

by:K_Watkins
Comment Utility
In the textchanged event i change some navigator button statuses and activate some tabpages, but do nothing of consequence within the RTB itself.
0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
I did the following (see code snippet), and forced a yellow background on the T's in my "CCCTTTGGGAAA" string.

Then let you code run.  It said yellow and didn't change any background colors.


Public Class Form4

    Dim FIRSTTIME As Boolean = True
 

    Sub DoIt()

        Dim i As Integer

        Dim X As Char

        Me.RichTextBox1.Select(3, 3)

        RichTextBox1.SelectionBackColor = Color.Yellow
 
 

        For i = 0 To Me.RichTextBox1.TextLength - 1  'begin to loop thru string one character at a time

            X = Me.RichTextBox1.Text.Substring(i, 1)

            Me.RichTextBox1.Select(i, 1) 'select the character

            Select Case Me.RichTextBox1.SelectionBackColor '

                Case Color.LightBlue

                    MsgBox("lb")

                Case Color.Yellow

                    MsgBox("yellow")

                Case Color.Orange

                    MsgBox("orange")

            End Select

        Next

    End Sub
 

    Private Sub Form4_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

        If FIRSTTIME Then

            FIRSTTIME = False

            DoIt()

        End If

    End Sub

End Class

Open in new window

0
 

Author Comment

by:K_Watkins
Comment Utility
hmmm, interesting.  When i do run your code, which has the select(3,3) and the selectedbackcolor=yellow within the procedure, i get the desired results.  but if i comment those lines out, it doesnt work.  I dont want those lines in there because the characters will already be colored.  Below is my code for setting the backcolor from a context menu item.  (The text does change color, and i do see the highlight codes in the .rtf after this click event.)  So i want the user to be able to change the back color, then later, examine the string for colors that were already entered. I appreciate your help!  

Private Sub ToolStripMenuItemLNA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItemLNA.Click
        Me.RichTextBox1.SelectionBackColor = Me.ToolStripMenuItemLNA.BackColor  'yellow
    End Sub
0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
I only did the select(3,3) because I didn't have previously colored text to deal with.
0
 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
Comment Utility
Ok, I changed my code and added Button1 (Yellow background, makes cur richtextbox selection yellow)
I commented out the other forced lines.

I added a btnExamine that calls doit to process things.  I copied the .RTF to the clipboard.  Here are the results and the code is in the snippet:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green255\blue0;}
\viewkind4\uc1\pard\f0\fs17 CCC\highlight1 TTT\highlight0 GGGAAA\par
}

It works fine for me and says "Yellow" 3 times
Public Class Form4
 

    Sub DoIt()

        Dim i As Integer

        Dim X As Char

        'Me.RichTextBox1.Select(3, 3)

        'RichTextBox1.SelectionBackColor = Color.Yellow
 
 

        For i = 0 To Me.RichTextBox1.TextLength - 1  'begin to loop thru string one character at a time

            X = Me.RichTextBox1.Text.Substring(i, 1)

            Me.RichTextBox1.Select(i, 1) 'select the character

            Select Case Me.RichTextBox1.SelectionBackColor '

                Case Color.LightBlue

                    MsgBox("lb")

                Case Color.Yellow

                    MsgBox("yellow")

                Case Color.Orange

                    MsgBox("orange")

            End Select

        Next

        Clipboard.Clear()

        Clipboard.SetText(RichTextBox1.Rtf)

    End Sub
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        RichTextBox1.SelectionBackColor = Button1.BackColor

    End Sub
 

    Private Sub btnExamine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExamine.Click

        DoIt()

    End Sub

End Class

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 25

Expert Comment

by:SStory
Comment Utility
I'm curious as to why your program works this way.  Do all T's get colored yellow?  If so why not just count the T's to find the weight?
0
 

Author Comment

by:K_Watkins
Comment Utility
CGAT are the components of DNA and CGAU are the components of RNA.  And i am using color to identify the RNA components.  So if my company is building a DNA strand and order entry clerk entered CGAT in the richtextbox will not be colored. but when it is a strand of RNA (and DNA could be included in the strand)  then it is important to specify the difference between DNA letters and the RNA portion.  DNA and RNA are manufactured differently but also priced differently.  So that is why i am coloring RNA yellow.  In my example, we colored T yellow. This was a bad example because  in actuality we would only color CGA letters in yellow.  the letter T is only found in DNA.  So there's a little genetic refresher.  More information than you wanted to know. this strand of letters could be hundreds of letters in length.  I have not looked at your posted code above yet this morning, so i will set off to do that now.
0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
I'm just curious though, is there no way to make the computer know what you know when selecting it so that selections are not necessary?  I would think so.  Maybe after all letters are typed in you could have a parser run through and color it all correctly based upon some rules...  

No, I am always interested in learning.  I took 2 biology classes in college and know a little about the subject...It is incredibly fascinating how we were designed.

Thanks

I was just thinking maybe you are a geneticist and not a programmer and may not have considered a simpler way.  I am a programmer and have been for over 22 years, so I think in a programming/logic way.  so I don't suggest in order to insult your intelligence, but just in case this is not your discipline and you hadn't thought about it.
0
 

Author Comment

by:K_Watkins
Comment Utility
Since your code ran perfectly, i assumed that some other event was causing problems in the textbox.  In my real form i was removing the spaces out of the richtextbox text with the following replace stmts.  

 Me.RichTextBox1.Text = Me.RichTextBox1.Text.Replace(" ", "")
        Me.RichTextBox1.Text = Me.RichTextBox1.Text.Replace(Chr(13), "")
        Me.RichTextBox1.Text = Me.RichTextBox1.Text.Replace(Chr(10), "")

This was the cause of the problem.  If I had included the call to the other function, we may have discovered the problem sooner.  Lesson to me to include all the code in the example!  So thank you very very much for causing me to look elsewhere.  I greatly appreciate your help and time!
0
 

Author Closing Comment

by:K_Watkins
Comment Utility
Thanks very much!  You've made my day! Should have joined this exchange sooner!  
0
 

Author Comment

by:K_Watkins
Comment Utility
Generally when the string is entered, there are no rules.  Each string that is entered is a combination of letters that our customers have requested and they are highly customized and usually unique to the order.  On some of our stock items, I could determine the color scheme, but not on the customized strands.  Very interesting business indeed!  I am a programmer, albiet not a very experienced vb.net programmer, and i need this exchange very much for new better ideas on how to accomplish solutions in the timeliest manner and one which meets best practices methodology. So  I welcome suggestions. Thanks again.
0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
Well good!  I'm glad I was able to help you.  I became an "expert" on here because this site usually has the answers to my questions and I wanted access and I enjoy helping people as well.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now