Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rich box text font

Posted on 2006-10-21
6
Medium Priority
?
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Accepted Solution

by:
Mohamed Zedan earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

722 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