How to preserve the old font properties while changing new one?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Sakharam Phapale (sphapale_at_annetsite.com)
Date: 11/10/04


Date: Wed, 10 Nov 2004 12:29:48 +0530

Hi All,

How to preserve the old font properties while changing new one?
I posted same question 2 months back, but I had very small time then.

eg. "Shopping for" is a text in RichTextBox and already formatted as
"Shopping" ---Bold
"for" -----Regular

Now I want to underline whole text by preserving old style i.e. Bold and
regular.
So that I should get result as follows

"Shopping" ---Bold + Underline
"for" -----Regular + Underline

When I press on UnderLine Button whole string becomes
Regular+Underline

I have written following code. You can just copy and paste it and thus can
be taste it.
So please tell me where is a mistake?
I have tried lot of things, but couldn't get the result.

Thanks in advance

Regards,
Sakharam Phapale

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents cmdBold As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents rtbData As System.Windows.Forms.RichTextBox
    Friend WithEvents cmbFontSize As System.Windows.Forms.ComboBox
    Friend WithEvents cmbFontFamily As System.Windows.Forms.ComboBox
    Friend WithEvents cmdUnderLine As System.Windows.Forms.Button
    Friend WithEvents cmdItalic As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        Me.rtbData = New System.Windows.Forms.RichTextBox()
        Me.cmdBold = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.cmdUnderLine = New System.Windows.Forms.Button()
        Me.cmdItalic = New System.Windows.Forms.Button()
        Me.cmbFontSize = New System.Windows.Forms.ComboBox()
        Me.cmbFontFamily = New System.Windows.Forms.ComboBox()
        Me.SuspendLayout()
        '
        'rtbData
        '
        Me.rtbData.Font = New System.Drawing.Font("Times New Roman", 11.25!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
        Me.rtbData.HideSelection = False
        Me.rtbData.Location = New System.Drawing.Point(24, 96)
        Me.rtbData.Name = "rtbData"
        Me.rtbData.Size = New System.Drawing.Size(592, 248)
        Me.rtbData.TabIndex = 1
        Me.rtbData.Text = "Shopping for hourly and daily parking on this
web"
        '
        'cmdBold
        '
        Me.cmdBold.Font = New System.Drawing.Font("Arial", 12.0!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
        Me.cmdBold.Location = New System.Drawing.Point(344, 40)
        Me.cmdBold.Name = "cmdBold"
        Me.cmdBold.Size = New System.Drawing.Size(24, 24)
        Me.cmdBold.TabIndex = 2
        Me.cmdBold.Text = "B"
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Times New Roman", 11.25!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(32, 41)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 24)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Font Name"
        '
        'cmdUnderLine
        '
        Me.cmdUnderLine.Font = New System.Drawing.Font("Arial", 12.0!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
        Me.cmdUnderLine.Location = New System.Drawing.Point(376, 40)
        Me.cmdUnderLine.Name = "cmdUnderLine"
        Me.cmdUnderLine.Size = New System.Drawing.Size(24, 24)
        Me.cmdUnderLine.TabIndex = 6
        Me.cmdUnderLine.Text = "U"
        '
        'cmdItalic
        '
        Me.cmdItalic.Font = New System.Drawing.Font("Arial", 12.0!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
        Me.cmdItalic.Location = New System.Drawing.Point(408, 40)
        Me.cmdItalic.Name = "cmdItalic"
        Me.cmdItalic.Size = New System.Drawing.Size(24, 24)
        Me.cmdItalic.TabIndex = 7
        Me.cmdItalic.Text = "I"
        '
        'cmbFontSize
        '
        Me.cmbFontSize.Font = New System.Drawing.Font("Times New Roman",
9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
        Me.cmbFontSize.Items.AddRange(New Object() {"8", "9", "10", "11",
"12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72"})
        Me.cmbFontSize.Location = New System.Drawing.Point(272, 40)
        Me.cmbFontSize.Name = "cmbFontSize"
        Me.cmbFontSize.Size = New System.Drawing.Size(56, 23)
        Me.cmbFontSize.TabIndex = 21
        '
        'cmbFontFamily
        '
        Me.cmbFontFamily.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cmbFontFamily.Font = New System.Drawing.Font("Times New Roman",
9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
        Me.cmbFontFamily.Location = New System.Drawing.Point(120, 40)
        Me.cmbFontFamily.Name = "cmbFontFamily"
        Me.cmbFontFamily.Size = New System.Drawing.Size(152, 23)
        Me.cmbFontFamily.TabIndex = 20
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(640, 365)
        Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.cmbFontSize, Me.cmbFontFamily, Me.cmdItalic, Me.cmdUnderLine, Me.Label1,
Me.cmdBold, Me.rtbData})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        cmbFontFamily.Items.Clear()
        Dim myFontFamily() As FontFamily
        Dim objFontFamily As FontFamily
        myFontFamily = objFontFamily.Families()
        Dim i As Integer
        For i = 0 To myFontFamily.GetUpperBound(0)
            cmbFontFamily.Items.Add(myFontFamily(i).Name)
        Next
        cmbFontFamily.SelectedIndex = 0
        cmbFontSize.SelectedIndex = 0
    End Sub

 'Change Font Family

    Private Sub cmbFontFamily_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbFontFamily.SelectedIndexChanged
        Try
            Dim fOld As Font = rtbData.SelectionFont
            Dim fontFamily As New FontFamily(cmbFontFamily.Text)
            If fOld Is Nothing Then
            Else
                rtbData.SelectionFont = New Font(fontFamily, fOld.Size)
                Dim fNew As Font = rtbData.SelectionFont
                rtbData.SelectionFont = New Font(fNew, fOld.Style)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

 'Change font size

    Private Sub cmbFontSize_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbFontSize.SelectedIndexChanged
        Try
            Dim fOld As Font = rtbData.SelectionFont
            If fOld Is Nothing Then

            Else
                rtbData.SelectionFont = New Font(fOld.FontFamily,
CSng(cmbFontSize.Text), fOld.Style)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

 'Change Bold format

    Private Sub cmdBold_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdBold.Click
        Dim fOld As Font = rtbData.SelectionFont
        If fOld Is Nothing Then

        Else
            rtbData.SelectionFont = New Font(fOld, fOld.Style +
(IIf(fOld.Bold, FontStyle.Bold * -1, FontStyle.Bold)))
        End If
    End Sub

 'Change Underline format

    Private Sub cmdUnderLine_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdUnderLine.Click
        Dim fOld As Font = rtbData.SelectionFont
        If fOld Is Nothing Then

        Else
            rtbData.SelectionFont = New Font(fOld, fOld.Style +
(IIf(fOld.Underline, FontStyle.Underline * -1, FontStyle.Underline)))
        End If
    End Sub

 'Change Italic format

    Private Sub cmdItalic_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdItalic.Click
        Dim fOld As Font = rtbData.SelectionFont
        If fOld Is Nothing Then

        Else
            rtbData.SelectionFont = New Font(fOld, fOld.Style +
(IIf(fOld.Italic, FontStyle.Italic * -1, FontStyle.Italic)))
        End If
    End Sub

    Private Sub rtbData_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles rtbData.SelectionChanged
        If rtbData.SelectionFont Is Nothing Then Exit Sub
        cmbFontFamily.Text = rtbData.SelectionFont.FontFamily.Name
        cmbFontSize.Text = rtbData.SelectionFont.Size.ToString
    End Sub

End Class


Quantcast