Using Rich Text Box - make a line bold

J

Jason

If I've got

Me.RichTextBox1.Text = "In Ricn Tex Box"
Me.RichTextBox1.Text &= "Line two" & vbCrLf


How do I make the first line appear BOLD?
 
G

Guest

It is a very interesting question. I am working on that too.
I would like to know also how could we make PORTION of a line Bold ?
I am looking to a solution like:
RichTextBox1.text="In
ricn"&please_make_the_following_bold...&"Text"&make_the
following_not_bold_anymore...&"Box"
I am looking for a solution to build nice reports on screen as I was doing
under Basic for DOS 7.1
 
G

Guest

I already have read that segment of the documentation. It is just not easy to
understand for a beginner. How can we select text or portion of text ? We
just have no idea !

I have try to use Word and to save a report to a rtf file format: when
loaded into a Rich Text Box, all the controls characters were shown... I gave
up !

A step by step approach will be very appreciated.
 
H

Herfried K. Wagner [MVP]

Jason said:
Me.RichTextBox1.Text = "In Ricn Tex Box"
Me.RichTextBox1.Text &= "Line two" & vbCrLf

How do I make the first line appear BOLD?

\\\
With Me.RichTextBox1
.Select(10, 20)
.SelectionFont = _
New Font(Me.RichTextBox1.SelectionFont, FontStyle.Bold)
End With
///
 
G

Guest

Mr Wagner,
It is working very well. I've got lost with the With End With, to give you
an idea of how far we have to go, as beginners...
 
C

Cerebrus99

Hi,

Mr. Herfried Wagner already answered it perfectly, but I will elaborate,
since someone asked for a "step-by-step" method.

1. If you want to make changes to a particular set of text in the RTBox,
just select that text using the
RTB.Select(start,length) method.
If some text is already selected in the RTBox, and you want to change that
text, then you don't need
to apply the above line.

2. Create a Font object setting properties as required.
Dim myFont as new Font("Arial", 12, FontStyle.Bold)

3. Apply that Font object to the RTB.SelectionFont property. You should see
the changes in the RTB.

If RTB.SelectionFont.Bold = False Then
RTB.SelectionFont = myFont
End if

Hope this helps,

Regards,

Cerebrus.
=========================================================================
 
A

Armin Zingler

Marcel Saucier said:
I already have read that segment of the documentation. It is just
not easy to understand for a beginner. How can we select text or
portion of text ? We just have no idea !

I have try to use Word and to save a report to a rtf file format:
when loaded into a Rich Text Box, all the controls characters were
shown... I gave up !

A step by step approach will be very appreciated.


I have never done it yet, but here are some better instructions how to do
it:

http://msdn.microsoft.com/library/en-us/vbcon/html/vbconSettingFontAttributes.asp

See also the link in the hints sections at the bottom (pointing to the
Textboxbase.Select method).


Armin
 
J

Jason

Ok, Maybe I'm worse off then a beginner

I've got my Rich Text Box on my Form. The information I'll be displaying is
simple computer properties. I want to display a heading for each section in
BOLD

for Example

Me.RichTextBox1.Text = "HEADING ONE"
Me.RichTextBox1.Text &= "Property one" &VAR1 & vbCrLf
Me.RichTextBox1.Text &= "Property one" & VAR2 & vbCrLf
Me.RichTextBox1.Text &= "Property one" & VAR3 & vbCrLf
Me.RichTextBox1.Text &= "HEADING TWO" & vbCrLf

I'd like to simple have HEADING ONE and HEADING TWO displayed in bold.
How do I do that?
 
C

Cerebrus99

Alrighty, here you go... Copy and paste the following code...
------------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Var1, Var2, Var3, MyText As String
Var1 = " is good"
Var2 = " is better"
Var3 = " is the best"
MyText = "HEADING ONE" & vbCrLf
MyText &= "Property one" & Var1 & vbCrLf
MyText &= "Property one" & Var2 & vbCrLf
MyText &= "Property one" & Var3 & vbCrLf
MyText &= "HEADING TWO"
RTB.Text = MyText
MakeBold()
End Sub


Private Sub MakeBold()
Dim myFont As New Font("Tahoma", 10, FontStyle.Bold)
'Find Location of "Heading1"
Dim txt2Srch As String = "HEADING ONE"
Dim loc As Integer
loc = InStr(1, RTB.Text, txt2Srch, CompareMethod.Text)
If loc > 0 Then
'The string was found at loc
RTB.Select((loc - 1), txt2Srch.Length)

If RTB.SelectionFont.Bold = False Then
RTB.SelectionFont = myFont
End If
End If

'Repeat procedure for "HEADING TWO"
txt2Srch = "HEADING TWO"
loc = InStr(1, RTB.Text, txt2Srch, CompareMethod.Text)
If loc > 0 Then
'The string was found at loc
RTB.Select((loc - 1), txt2Srch.Length)
If RTB.SelectionFont.Bold = False Then
RTB.SelectionFont = myFont
End If
End If
'Move the caret back to start.
RTB.Select(0,0)

End Sub
------------------------------------------------------------

I think this should get you up to speed. Cheers !

Regards,

Cerebrus.

===========================================
 

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