Bolding a RichTextBox in Child Form

vartan

Member
Joined
Feb 26, 2006
Messages
7
Location
Irvine, California
Programming Experience
1-3
:) Hi, I'm new to this forum.

I have a problem with my Windows application.

I have an MDI form that creates a new child form when the new button is clicked. The child form contains a rich text box called CRTB which is made to fill the whole child form:

VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ShowNewForm([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
[/SIZE][SIZE=2][COLOR=#008000]' Create a new instance of the child form.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ChildForm [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Form
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] CRTB [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.RichTextBox
[/SIZE][SIZE=2][COLOR=#008000]'Dim CRTB As New System.Windows.Forms.RichTextBox
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Make it a child of this MDI form before showing it.
[/COLOR][/SIZE][SIZE=2]ChildForm.MdiParent = [/SIZE][SIZE=2][COLOR=#0000ff]Me
[/COLOR][/SIZE][SIZE=2]m_ChildFormNumber += 1
ChildForm.Text = [/SIZE][SIZE=2][COLOR=#800000]"Window "[/COLOR][/SIZE][SIZE=2] & m_ChildFormNumber
CRTB.Dock = DockStyle.Fill
CRTB.Parent = ChildForm
ChildForm.Show()
[COLOR=seagreen]'CRTB.Text = [/COLOR][/SIZE][SIZE=2][COLOR=seagreen]"Hello!"[/COLOR]
[/SIZE][COLOR=seagreen][SIZE=2]'CRTB.SaveFile([/SIZE][SIZE=2]"format.rtf"[/SIZE][/COLOR][SIZE=2][COLOR=seagreen])[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Then when a button on the MDI form with the caption Bold is clicked, it runs this:

VB.NET:
 [SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] BoldBut_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] BoldBut.Click
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] ChildForm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].MdiChildren
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ChildForm.ContainsFocus [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] CRTB [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] RichTextBox [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] ChildForm.Controls
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] CRTB.SelectionFont.Bold <> [/SIZE][SIZE=2][COLOR=#0000ff]False [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]CRTB.SelectionFont.Bold = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Else [/COLOR][/SIZE]
[COLOR=black]CRTB.SelectionFont.Bold = [/COLOR][COLOR=blue]False[/COLOR]
[COLOR=blue]End If[/COLOR][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Exit [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

VB tells me that CRTB.SelectionFont.Bold is Read Only and that I cannot change its value.

How do I set the selection of the text to be bold?

Many thanks to anyone who can help me.

P.S. I have attached the project in a compressed folder (.zip) format.
 

Attachments

  • WinApp1.zip
    138.8 KB · Views: 18
You can't change the bold property of the font because it is readonly, but you can change the font. Use the existing font and provide a new fontstyle, which is where bold/italic etc belongs. The FontStyle allows for bitwise operations so it is possible to combine the different styles, and it also allows operation to turn on/off a certain style without changing the existing (for instance Italic Underline):
VB.NET:
For Each ...
  Dim fs As FontStyle = CRTB.SelectionFont.Style
  If CRTB.SelectionFont.Bold = False Then
    'add bold style
    CRTB.SelectionFont = New Font(CRTB.SelectionFont, fs Or Drawing.FontStyle.Bold)
  Else
    'remove bold style
    CRTB.SelectionFont = New Font(CRTB.SelectionFont, fs Xor Drawing.FontStyle.Bold)
  End If
Next
 
So if I wanted italics I would use:

VB.NET:
For Each ...
  Dim fs As FontStyle = CRTB.SelectionFont.Style
  If CRTB.SelectionFont.Italic = False Then
    'add italic style
    CRTB.SelectionFont = New Font(CRTB.SelectionFont, fs Or Drawing.FontStyle.Italic)
  Else
    'remove italic style
    CRTB.SelectionFont = New Font(CRTB.SelectionFont, fs Xor Drawing.FontStyle.Italic)
  End If
Next
:confused: :confused: :confused:
?????????
 
Last edited:
Actually you don't need the If statement at all. You can simply use Xor to toggle the style. If it's currently set then Xor will clear it, and if it's not Xor will set it. That will work for Bold, Italic, Underline and Strikeout. What you would normally do is handle the SelectionChanged event of the RTB to determine whether the buttons should be depressed or not based on code like this:
VB.NET:
Dim fontBold As Boolean = CRTB.SelectionFont.Style And FontStyle.Bold
This will return True if the current font is Bold. You would do similar things for the other styles. You would then simply toggle the style when the button was pressed:
VB.NET:
CRTB.SelectionFont = New Font(CRTB.SelectionFont, CRTB.SelectionFont Xor FontStyle.Bold)
 
Back
Top