Solved

Rich box text font

Posted on 2006-10-21
6
450 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
export data table to excel in vb.net 10 37
vb.net class 3 22
Error message when using PrintSocket.Send 7 19
how to hit button in form and close application from main() 4 22
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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