How to preserve font properties while changing some other properties.

  • Thread starter Sakharam Phapale
  • Start date
S

Sakharam Phapale

Hi All,

I am using a richTextBox control for text editing purpose. I have written
following procedure which change the font style of richTextBox.

Dim f as font = rtbData.selectionFont()

rtbData.SelectionFont = New Font(f, f.Style + (IIf(f.Bold, FontStyle.Bold
* -1, FontStyle.Bold)))


Using above code I am able to preserve old font style while changing style
like Bold.

But, How to change the font name and size property.

Thanks in advance
Sakharam Phapale
 
S

Sakharam Phapale

Hi All,

I got the answer as follows.


Dim fOld As Font = rtbData.SelectionFont
Dim fontFamily As New FontFamily(FontFamilyName)

rtbData.SelectionFont = New Font(fontFamily, fOld.Size)

Dim fNew As Font = rtbData.SelectionFont

rtbData.SelectionFont = New Font(fNew, fOld.Style)


Thanks
Sakharam Phapale
 
C

Cor Ligthert

Sakharam,

To show you the use of the boolean "OR" in this, see this old snippet from
me.

\\\
Me.Label1.Font = New Font(Me.Label1.Font, Me.Label1.Font.Style Or
FontStyle.Bold)
///

I hope this helps?

Cor
 
S

Sakharam Phapale

Hello,

Your solution works fine for font style. But what about following situation,
I want to change font of richTextBox contents, which is having multiple
styles.
look at following example

richTextBox.Text = "This is just a test example"
"This" ---Times New Roman, Bold, 10
"is"-------arial, Regular,8
"just"-----Courier, Regular,9

Now, I select "This is just" in richTextBox and want to change the font size
to 10 of selected text.

How to accomplish this?
I have written following code. But it doesn't work properly in above
scenario.

Private sub ChangeFont(FontSize as integer)
Dim fOld As Font = rtbData.SelectionFont

rtbData.SelectionFont = New Font(fOld.FontFamily, FontSize,
fOld.Style)

End Sub

In above scenario fOld returns as Nothing so I can't used this.


Thanks and Regards
Sakharam Phapale
 

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