Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 989
  • Last Modified:

Richtextbox Selection BackColor

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
K_Watkins
Asked:
K_Watkins
  • 8
  • 6
1 Solution
 
SStoryCommented:
Are you responding to any events when the RichTextBox (RTB) selection changes?
0
 
SStoryCommented:
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
 
K_WatkinsAuthor Commented:
In the textchanged event i change some navigator button statuses and activate some tabpages, but do nothing of consequence within the RTB itself.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
SStoryCommented:
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
 
K_WatkinsAuthor Commented:
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
 
SStoryCommented:
I only did the select(3,3) because I didn't have previously colored text to deal with.
0
 
SStoryCommented:
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
 
SStoryCommented:
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
 
K_WatkinsAuthor Commented:
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
 
SStoryCommented:
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
 
K_WatkinsAuthor Commented:
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
 
K_WatkinsAuthor Commented:
Thanks very much!  You've made my day! Should have joined this exchange sooner!  
0
 
K_WatkinsAuthor Commented:
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
 
SStoryCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now