Solved

Rich box text font

Posted on 2006-10-21
6
453 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
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…
Suggested Courses

623 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