Solved

Rich box text font

Posted on 2006-10-21
6
442 Views
Last Modified: 2013-12-03
Hi experts

how can i change the font in my rich text box,

The following routines dont work in the way i want.

I want to be able to allow underline, bold and italic at once

Private Sub BBold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BBold.Click
        If Me.BBold.ImageIndex = 3 Then
            Me.RTBSendText.Font = New Font(Me.RTBSendText.Font, FontStyle.Bold)
            Me.BBold.ImageIndex = 4
        Else
            Me.RTBSendText.Font = New Font(Me.RTBSendText.Font, FontStyle.Regular)
            Me.BBold.ImageIndex = 3
        End If
    End Sub

    Private Sub BItalic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BItalic.Click
        If Me.BItalic.ImageIndex = 5 Then
            Me.RTBSendText.Font = New Font(Me.RTBSendText.Font, FontStyle.Italic)
            Me.BItalic.ImageIndex = 6
        Else
            Me.RTBSendText.Font = New Font(Me.RTBSendText.Font, FontStyle.Regular)
            Me.BItalic.ImageIndex = 5
        End If
    End Sub

    Private Sub BUnderline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BUnderline.Click
        If Me.BUnderline.ImageIndex = 7 Then
            Me.RTBSendText.Font = New Font(Me.RTBSendText.Font, FontStyle.Underline)
            Me.BUnderline.ImageIndex = 8
        Else
            Me.RTBSendText.Font = New Font(Me.RTBSendText.Font, FontStyle.Regular)
            Me.BUnderline.ImageIndex = 7
        End If
    End Sub

These methods do not preserve the format. If the font was bold before underline is clicked; the boldness is removed and just underline is checked.

Cheers,

Tom
0
Comment
Question by:TommyTwoPints
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17780554
use Selection Font maybe? try this:

If RTBSendText.SelectionFont.Bold Then
   RTBSendText.SelectionFont = New Font(RTBSendText.SelectionFont, (RTBSendText.SelectionFont.Style And Not               (RTBSendText.SelectionFont.Style And FontStyle.Bold)))
Else
    RTBSendText.SelectionFont = New Font(RTBSendText.SelectionFont, (RTBSendText.SelectionFont.Style Or FontStyle.Bold))
End If

0
 
LVL 3

Author Comment

by:TommyTwoPints
ID: 17780577
no, i want to allow bold, italic and underline together.

Cheers.

Tom
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17780583
for bold as an example do the new font like this.....

Me.RTBSendText.Font = New Font(Me.RTBSendText.Font.FontFamily, Me.RTBSendText.Font.SizeInPoints, Me.RTBSendText.Font.Style Or FontStyle.Bold)

for underline and italic ... change fontstyles.bold to the respective choices...

glad to be of service :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Accepted Solution

by:
Mohamed Zedan earned 500 total points
ID: 17780872
here is a complete function for what you want

Public Function ToggleFontStyle(ByVal f As Font, ByVal Style As FontStyle) As Font
        Dim bold As Boolean = (f.Style And FontStyle.Bold = FontStyle.Bold)
        Dim Italic As Boolean = (f.Style And FontStyle.Italic = FontStyle.Italic)
        Dim UnderLined As Boolean = (f.Style And FontStyle.Underline = FontStyle.Underline)
        Dim Strikeout As Boolean = (f.Style And FontStyle.Strikeout = FontStyle.Strikeout)

        Select Case Style
            Case FontStyle.Bold
                bold = Not bold
            Case FontStyle.Italic
                Italic = Not Italic
            Case FontStyle.Strikeout
                Strikeout = Not Strikeout
            Case FontStyle.Underline
                UnderLined = Not UnderLined
        End Select

        Dim NewFont As Font
        NewFont = New Font(f.FontFamily, f.SizeInPoints, CType(IIf(bold, FontStyle.Bold, FontStyle.Regular), FontStyle) Or _
                                                       CType(IIf(Italic, FontStyle.Italic, FontStyle.Regular), FontStyle) Or _
                                                       CType(IIf(Strikeout, FontStyle.Strikeout, FontStyle.Regular), FontStyle) Or _
                                                       CType(IIf(UnderLined, FontStyle.Underline, FontStyle.Regular), FontStyle))
        Return NewFont
    End Function

just pass it the richtextbox font and the style you want to toggle.
and it will return the new font that you should set to the richtextnox
0
 
LVL 3

Author Comment

by:TommyTwoPints
ID: 17781011
Thanks Alot:)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17781984
you're welcome :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now