How to change color line by line in a rich textbox?

G

GY2

I want to step through the rows returned by my DataView, extract some values
from some of its columns and append them as separate lines 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
..ForeColor instead of 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)

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

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

With myRTB

For Each myDRV In dvEventDataByDate

.SelectionColor = FetchColor 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
 
K

Ken Halter

GY2 said:
I want to step through the rows returned by my DataView, extract some
values
from some of its columns and append them as separate lines to the text of
various rich textbox

What was once "easy" is now "painful"...

This code, which works perfectly in VB4/5/6.....
'===============
Private Sub Form_Load()
With RichTextBox1
'Clear all
.Text = ""
.SelFontName = "Arial"
.SelColor = vbRed
.SelText = "This is red Arial text" & vbCrLf
.SelBold = True
.SelColor = vbBlue
.SelFontName = "MS Sans Serif"
.SelText = "This is MS Sans Serif, blue and bold" & vbCrLf
.SelFontSize = 14
.SelFontName = "Courier"
.SelColor = vbGreen
.SelText = "This is Courier bold, green and 14 points high" & vbCrLf
End With
End Sub
'===============

....."migrates" to this mess (that almost works) in B#
'===============
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs
As System.EventArgs) Handles MyBase.Load
With RichTextBox1
'Clear all
.Text = ""
.SelectionFont = VB6.FontChangeName(.SelectionFont, "Arial")
.SelectionColor = System.Drawing.Color.Red
.SelectedText = "This is red Arial text" & vbCrLf
.Font = VB6.FontChangeBold(.SelectionFont, True)
.SelectionColor = System.Drawing.Color.Blue
'UPGRADE_WARNING: Only TrueType and OpenType fonts are supported in
Windows Forms. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="971F4DF4-254E-44F4-861D-3AA0031FE361"'
.SelectionFont = VB6.FontChangeName(.SelectionFont, "MS Sans Serif")
.SelectedText = "This is MS Sans Serif, blue and bold" & vbCrLf
.SelectionFont = VB6.FontChangeSize(.SelectionFont, 14)
'UPGRADE_WARNING: Only TrueType and OpenType fonts are supported in
Windows Forms. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="971F4DF4-254E-44F4-861D-3AA0031FE361"'
.SelectionFont = VB6.FontChangeName(.SelectionFont, "Courier")
.SelectionColor = System.Drawing.Color.Lime
.SelectedText = "This is Courier bold, green and 14 points high" & vbCrLf
End With
End Sub
'===============
 
G

GY2

Jeeez! Almost sorry I asked. Thanks Ken.

Ken Halter said:
What was once "easy" is now "painful"...

This code, which works perfectly in VB4/5/6.....
'===============
Private Sub Form_Load()
With RichTextBox1
'Clear all
.Text = ""
.SelFontName = "Arial"
.SelColor = vbRed
.SelText = "This is red Arial text" & vbCrLf
.SelBold = True
.SelColor = vbBlue
.SelFontName = "MS Sans Serif"
.SelText = "This is MS Sans Serif, blue and bold" & vbCrLf
.SelFontSize = 14
.SelFontName = "Courier"
.SelColor = vbGreen
.SelText = "This is Courier bold, green and 14 points high" & vbCrLf
End With
End Sub
'===============

...."migrates" to this mess (that almost works) in B#
'===============
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs
As System.EventArgs) Handles MyBase.Load
With RichTextBox1
'Clear all
.Text = ""
.SelectionFont = VB6.FontChangeName(.SelectionFont, "Arial")
.SelectionColor = System.Drawing.Color.Red
.SelectedText = "This is red Arial text" & vbCrLf
.Font = VB6.FontChangeBold(.SelectionFont, True)
.SelectionColor = System.Drawing.Color.Blue
'UPGRADE_WARNING: Only TrueType and OpenType fonts are supported in
Windows Forms. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="971F4DF4-254E-44F4-861D-3AA0031FE361"'
.SelectionFont = VB6.FontChangeName(.SelectionFont, "MS Sans Serif")
.SelectedText = "This is MS Sans Serif, blue and bold" & vbCrLf
.SelectionFont = VB6.FontChangeSize(.SelectionFont, 14)
'UPGRADE_WARNING: Only TrueType and OpenType fonts are supported in
Windows Forms. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="971F4DF4-254E-44F4-861D-3AA0031FE361"'
.SelectionFont = VB6.FontChangeName(.SelectionFont, "Courier")
.SelectionColor = System.Drawing.Color.Lime
.SelectedText = "This is Courier bold, green and 14 points high" &
vbCrLf
End With
End Sub
'===============
 
G

GY2

Thanks Cerebrus. I only copied the post when I received no help here for a
while--too impatient (or desperate) I guess.Sorry for the double and thanks
for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top