RichTextBox FontStyles
-
I am trying to apply different FontStyles to text within a RichtextBox control that was dynamically created. For some reason I do not get the .Selection??? anything Methods on a dynamically created control. All I have been able to successfully do is all or nothing. Here is the code I have been working with:
Try If tsbBold.Checked = False Then tsbBold.Checked = True For Each ctl As Control In TabControl1.SelectedTab.Controls If TypeOf ctl Is RichTextBox Then Dim selBold As New Font(ctl.Font, FontStyle.Bold) ctl.Font = selBold End If Next ElseIf tsbBold.Checked = True Then tsbBold.Checked = False For Each ctl As Control In TabControl1.SelectedTab.Controls If TypeOf ctl Is RichTextBox Then Dim selNotBold As New Font(ctl.Font, FontStyle.Regular) ctl.Font = selNotBold End If Next End If Catch ex As Exception MessageBox.Show(MsgBoxStyle.Exclamation, "An error has occured. Please try your selection again") End Try
Any suggestions? Thanks, Taen Karth -
I am trying to apply different FontStyles to text within a RichtextBox control that was dynamically created. For some reason I do not get the .Selection??? anything Methods on a dynamically created control. All I have been able to successfully do is all or nothing. Here is the code I have been working with:
Try If tsbBold.Checked = False Then tsbBold.Checked = True For Each ctl As Control In TabControl1.SelectedTab.Controls If TypeOf ctl Is RichTextBox Then Dim selBold As New Font(ctl.Font, FontStyle.Bold) ctl.Font = selBold End If Next ElseIf tsbBold.Checked = True Then tsbBold.Checked = False For Each ctl As Control In TabControl1.SelectedTab.Controls If TypeOf ctl Is RichTextBox Then Dim selNotBold As New Font(ctl.Font, FontStyle.Regular) ctl.Font = selNotBold End If Next End If Catch ex As Exception MessageBox.Show(MsgBoxStyle.Exclamation, "An error has occured. Please try your selection again") End Try
Any suggestions? Thanks, Taen KarthNevermind I figured out a way to work around it by using the following code.
Try If tsbBold.Checked = False Then tsbBold.Checked = True For Each ctl As Control In TabControl1.SelectedTab.Controls If TypeOf ctl Is RichTextBox Then Dim rtb As RichTextBox rtb = ctl Dim selBold As New Font(rtb.Font, FontStyle.Bold) rtb.SelectionFont = selBold End If Next ElseIf tsbBold.Checked = True Then tsbBold.Checked = False For Each ctl As Control In TabControl1.SelectedTab.Controls If TypeOf ctl Is RichTextBox Then Dim rtb As RichTextBox rtb = ctl Dim selNotBold As New Font(rtb.Font, FontStyle.Regular) rtb.SelectionFont = selNotBold End If Next End If Catch ex As Exception MessageBox.Show(MsgBoxStyle.Exclamation, "An error has occured. Please try your selection again") End Try
If there is a better or easier way please let me know. Thanks, Taen Karth