Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using Rich Textbox .Lines property

204 views
Skip to first unread message

GY2

unread,
Feb 20, 2006, 4:31:48 PM2/20/06
to
I want to step through the rows returned by my DataView, extract some values
from some of the columns and append them to the text of various rich textbox
controls while possibly changing the color of each new line.

It seems as though I should use the Lines property but can't find a good
example. Below is my attempt to do it with the .Text property (which doesn't
work because no colors are displayed). I have also tried this with
.SelectionColor.

What I am doing wrong here and/or what is the right way to do it with
.Lines?

Imports System.Drawing.Color
Private Sub LoadRTB(ByRef myRTB As RichTextBox, ByVal dtWhichDate As Date)

dvEventDataByDate.RowFilter = "EventDate = #" & dtWhichDate & "#" 'set
up DataView filter for the single specified date

Dim myDRV As DataRowView 'a single row of the filetered DataView

With myRTB

For Each myDRV In dvEventDataByDate

.ForeColor =
FetchColor(Microsoft.VisualBasic.Left(myDRV("EventName"), 1))

.Text = .Text & myDRV("EventName") & Space(1) &
myDRV("PatientID") & vbCrLf

Next myDRV

End With

End Sub

Private Function FetchColor(ByVal EventType$) As System.Drawing.Color

Select Case EventType$

Case "B"

FetchColor = Cyan

Case "D"

FetchColor = Blue

Case "I"

FetchColor = Green

Case "K"

FetchColor = Yellow

Case "L"

FetchColor = Black

Case "N"

FetchColor = Magenta

Case "S"

FetchColor = Red

Case Else

FetchColor = Black

End Select

End Function


Cerebrus99

unread,
Feb 21, 2006, 5:13:38 PM2/21/06
to
Hi GY2,

>> What I am doing wrong here and/or what is the right way to do it with .Lines?

The way to change the format of any text in a RichTextBox (referred to
as RTB hereafter), is to select the text first, and then to change it's
font or colors as required. So, what you're doing wrong here, is
setting the ForeColor instead of the SelectionColor (and not selecting
the line at all.)

Here is some code that I have tested in your scenario.

I have made the following changes to avoid setting up a DataView and
multiple RTB's, but I think you'll get the idea.

- You are checking the first letter of the EventName field, while I
have checked the first letter of the line we are reading.
- This method uses the Lines() property as you wanted.

The Code (Copy and paste it into a new form which contains a RTB named
"RTB" and a Button named "btn"...)

---------------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
RTB.Text = "The quick brown fox jumps over the lazy dog." & vbCrLf &
"Quick brown fox jumps over the lazy dog." & vbCrLf & "Brown fox jumps
over the lazy dog." & vbCrLf & "Fox jumps over the lazy dog." & vbCrLf
& "Jumps over the lazy dog." & vbCrLf & "Over the lazy dog." & vbCrLf &
"The lazy dog." & vbCrLf & "Lazy dog." & vbCrLf & "Dog."
End Sub

Private Sub btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn.Click
SetLineColors()
End Sub

Private Sub SetLineColors()
Dim allLines() As String = RTB.Lines
Dim eachLine As String
For Each eachLine In allLines
'Get the first letter of this line.
Dim firstLetter As String = Microsoft.VisualBasic.Left(eachLine, 1)
Dim LineColor As Color = FetchColor(firstLetter)
'Select this line.
Dim idx As Integer = RTB.Text.IndexOf(eachLine)
RTB.Select(idx, eachLine.Length)
RTB.SelectionColor = LineColor
Next
End Sub

Private Function FetchColor(ByVal FirstLetter As String) As
System.Drawing.Color
Select Case FirstLetter
Case "T"
FetchColor = Color.Pink
Case "Q"
FetchColor = Color.Blue
Case "B"
FetchColor = Color.Green
Case "F"
FetchColor = Color.Red
Case "J"
FetchColor = Color.Black
Case "O"
FetchColor = Color.Gold
Case "L"
FetchColor = Color.LemonChiffon
Case "D"
FetchColor = Color.DarkGray
Case Else
FetchColor = Color.White
End Select
End Function

---------------------------------------------------------------

Hope this helps,

Regards,

Cerebrus.

0 new messages