K_Watkins
asked on
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.Subst ring(i, 1)
Me.RichTextBox1.Select(i, 1) 'select the character
Select Case Me.RichTextBox1.SelectionB ackColor'
Case Color.LightBlue
MsgBox("lb")
Case Color.Yellow
MsgBox("yellow")
Case Color.Orange
MsgBox("orange")
End Select
Next
For i = 0 To Me.RichTextBox1.TextLength
x = Me.RichTextBox1.Text.Subst
Me.RichTextBox1.Select(i, 1) 'select the character
Select Case Me.RichTextBox1.SelectionB
Case Color.LightBlue
MsgBox("lb")
Case Color.Yellow
MsgBox("yellow")
Case Color.Orange
MsgBox("orange")
End Select
Next
Are you responding to any events when the RichTextBox (RTB) selection changes?
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)
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)
ASKER
In the textchanged event i change some navigator button statuses and activate some tabpages, but do nothing of consequence within the RTB itself.
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.
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
ASKER
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.SelectionB ackColor = Me.ToolStripMenuItemLNA.Ba ckColor 'yellow
End Sub
Private Sub ToolStripMenuItemLNA_Click
Me.RichTextBox1.SelectionB
End Sub
I only did the select(3,3) because I didn't have previously colored text to deal with.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
ASKER
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.
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.
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.
ASKER
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.Repla ce(" ", "")
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Repla ce(Chr(13) , "")
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Repla ce(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!
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Repla
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Repla
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Repla
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!
ASKER
Thanks very much! You've made my day! Should have joined this exchange sooner!
ASKER
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.
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.