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

S

Sakharam Phapale

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
 
S

Sakharam Phapale

Hi Cor,

I posted it, but even that time I didn't got answer.
That's why I have posted it again.

Thanks and regards
Sakharam Phapale
 
S

Sakharam Phapale

Hi Cor,

Following was our discussion on this issue.
But it was incomplete for my requirement.

Thanks and regards
Sakharam Phapale


From: Sakharam Phapale ([email protected])
Subject: Re: How to preserve font properties while changing some other
properties.


Newsgroups: microsoft.public.dotnet.languages.vb
Date: 2004-10-05 05:44:43 PST

Hello,

Your solution works fine for font style. But what about following situation,
I want to change font of richTextBox contents, which is having multiple
styles.
look at following example

richTextBox.Text = "This is just a test example"
"This" ---Times New Roman, Bold, 10
"is"-------arial, Regular,8
"just"-----Courier, Regular,9

Now, I select "This is just" in richTextBox and want to change the font size
to 10 of selected text.

How to accomplish this?
I have written following code. But it doesn't work properly in above
scenario.

Private sub ChangeFont(FontSize as integer)
Dim fOld As Font = rtbData.SelectionFont

rtbData.SelectionFont = New Font(fOld.FontFamily, FontSize,
fOld.Style)

End Sub

In above scenario fOld returns as Nothing so I can't used this.


Thanks and Regards
Sakharam Phapale


Cor Ligthert said:
Sakharam,

To show you the use of the boolean "OR" in this, see this old snippet from
me.

\\\
Me.Label1.Font = New Font(Me.Label1.Font, Me.Label1.Font.Style Or
FontStyle.Bold)
///

I hope this helps?

Cor

----------------------------------------------------------------------------
----
 
S

Sakharam Phapale

Hi All,

I have written following code to achieve the result.
It sets the font property character by character.
But I don't think that it's feasible solution because it selects character
and changes it's property looks awkward as compare to normal.
Please comment on this solution or offer a good one.


Private Sub UnderLineSelectedText()
Dim intCounter As Integer
For intCounter = rtbData.SelectionStart To rtbData.SelectionStart +
rtbData.SelectionLength - 1
rtbData.Select(intCounter, 1)
Dim fOld As Font = rtbData.SelectionFont
Dim fStyle As FontStyle = fOld.Style + (IIf(fOld.Underline,
FontStyle.Underline * -1, FontStyle.Underline))
rtbData.SelectionFont = New Font(fOld.FontFamily, fOld.Size,
fStyle)
Next
rtbData.SelectionLength = 0
End Sub

Thanks and regards,
Sakharam Phapale
 
C

Cor Ligthert

Sakharam,

At that time you did not ask as that it was for a rich textbox. (As you now
again not, when you look at your code and you know that it is about a
richtextbox than probably rtb is richtextbox)

I gave you an answer and than you said that it was correct for your purpose.
However than you came with a new question. The rich text box is not a
control I am often doing things with; nevertheless, there are enough in this
newsgroup who do. Therefore did I not answer on that question.

But when you ask again your question in a mystirious way, you will again
probably get an answer that does not help you to reach your goal.

I see however as well that you now again back to your old coding with the
IIF to set the font properties and not just the OR to add the font enums as
I advised you, so why are you asking for help?

Cor
 
S

Sakharam Phapale

Hi Cor,

I am very sorry, I made mistake by not mentioning RichTextBox control.
All of you peoples are very helpful and Since, I am not so strong in
english,
might not able to explain cleanly.

Thanks for your replies.
Sakharam Phapale

Note : Given solution works only when, whole text is having same font
properties.
for e.g.(As per my understanding Label.Text is either Bold or Regular)

But in RichTextBox some part of text can have Bold and other Regular.
In this situation given solution does not work.
 
C

Cor Ligthert

Sakharam,

When I was you I would make a new message with in the subject the text "in a
RichTextBox"

Cor

"Sakharam Phapale"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top