RichTextBox

  • Thread starter Thread starter Allen
  • Start date Start date
A

Allen

Hello. I have a windows form which contains a RichTextBox control,
rtNotes. At runtime this is filled with some data from a datatable using
the following code:

Dim sFont As New Font(rtNotes.Font, FontStyle.Regular)
Dim bFont As New Font(rtNotes.Font, FontStyle.Bold)

For Each dr As DataRow In dt.Rows
'this sets the first part to be bold
rtNotes.SelectionFont = bFont
rtNotes.Text = rtNotes.Text & dr("DateEvent").ToString & " " &
dr("AgendaCode").ToString & " " & dr("PartyName").ToString & vbCrLf
'this sets it back to standard
rtNotes.SelectionFont = sFont
rtNotes.Text = rtNotes.Text & dr("TranText").ToString & vbCrLf
& dr("longnote").ToString & vbCrLf & vbCrLf
Next

I am trying to get the first line of the text to be BOLD and the rest of
the note to be standard text. However, when I view the RichTextBox, all of
teh text is in standard font. I have also tried changing the
..SelectionFont to just .Font with the same result. How can I get teh first
line of each record to be bold?

TIA

--
ats@jbex

It's easy to lay down and hide
Where's the warrior without his pride?

Adam and The Ants - Dog Eat Dog
 
Hi!

Try this:

For Each dr As DataRow In dt.Rows
'this sets the first part to be bold

rtNotes.Text = rtNotes.Text & dr("DateEvent").ToString & " " &
dr("AgendaCode").ToString & " " & dr("PartyName").ToString & vbCrLf
rtNotes.SelectAll()
rtNotes.SelectionFont = bFont
rtNotes.SelectionLength = 0 ' This removes the selected area.
'this sets it back to standard
rtNotes.SelectionFont = sFont
rtNotes.Text = rtNotes.Text & dr("TranText").ToString & vbCrLf
& dr("longnote").ToString & vbCrLf & vbCrLf
Next

Actual You are trying to effect on selected text and nothing is selected
You don't need this line: rtNotes.SelectionFont = sFont
As there is nothing selected.

Regards!
Matti
 
Hi!

Try this:

For Each dr As DataRow In dt.Rows
'this sets the first part to be bold

rtNotes.Text = rtNotes.Text & dr("DateEvent").ToString & " " &
dr("AgendaCode").ToString & " " & dr("PartyName").ToString & vbCrLf
rtNotes.SelectAll()
rtNotes.SelectionFont = bFont
rtNotes.SelectionLength = 0 ' This removes the selected area.
'this sets it back to standard
rtNotes.SelectionFont = sFont
rtNotes.Text = rtNotes.Text & dr("TranText").ToString & vbCrLf
& dr("longnote").ToString & vbCrLf & vbCrLf
Next

Actual You are trying to effect on selected text and nothing is selected
You don't need this line: rtNotes.SelectionFont = sFont
As there is nothing selected.

Regards!
Matti

Thanks for this. I will give it a go and see what happens.
--
ats@jbex

But I learned to burn that bridge and delete
Those who compete...at a level that's obsolete
Instead I warm my hands upon the flames of the flag
As I recall our downfall
And the business that burned us all
See through the news and the views that twist reality

Rage Against The Machine - Bombtrack
 
Thanks for this. I will give it a go and see what happens.

Almost does it but the SelectAll line puts all preceeding text to Bold and
the SelectionLength = 0 means that nothing ever gets set to regular. I
will play around with it for a while though :-)
--
ats@jbex

The world is my expense
The cost of my desire
Jesus blessed me with its future
And I protect it with fire

Rage Against The Machine - Sleep Now In The Fire
 
Almost does it but the SelectAll line puts all preceeding text to Bold and
the SelectionLength = 0 means that nothing ever gets set to regular. I
will play around with it for a while though :-)

Got it. It works if I use the following:

For Each dr As DataRow In dt.Rows

rtNotes.Select(Position, 0)
rtNotes.SelectionFont = bFont
rtNotes.SelectedText = dr("DateEvent").ToString & " " &
dr("AgendaCode").ToString & " " & dr("PartyName").ToString & vbCrLf
Position += (dr("DateEvent").ToString & " " &
dr("AgendaCode").ToString & " " & dr("PartyName").ToString & vbCrLf).Length

rtNotes.Select(Position, 0)
rtNotes.SelectionFont = sFont
rtNotes.SelectedText = dr("TranText").ToString & vbCrLf &
dr("longnote").ToString & vbCrLf & vbCrLf
Position += (dr("TranText").ToString & vbCrLf &
dr("longnote").ToString & vbCrLf & vbCrLf).Length

Next

--
ats@jbex

Those who died are justified, for wearing the badge, they're the chosen
whites
You justify those that died by wearing the badge, they're the chosen whites

Rage Against The Machine - Killing In The Name
 
Back
Top