I have a RichTextBox. I process all the formatting and everything works fine. There is an exception. When I have selected text that have two states of formatting in it, the SelectionFont member is set to Nothing. I notice that WordPad recognizes this condition and kindof greys out the affected format. Let's take this example:

This text is in Bold.

The rest of the comments here and not in bold.

When I select these two lines, the SelectionFont gets set to Nothing. WordPad sets the Bold button grey, but still clickable. If I click the Bold button in this state, the entire selection is set to bold. How do I achieve this same functionality?

Hi!

I did it like the attached code, I used CommandButton, you can use ToolBar's Button and set its state to "Pressed" and "UnPressed":

Private Sub Bold_Click(sender As Object, e As EventArgs)
	If Not richTextBox1.SelectionFont.Bold Then
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Or FontStyle.Bold)
	Else
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Xor FontStyle.Bold)
	End If
End Sub

Private Sub Italic_Click(sender As Object, e As EventArgs)
	If Not richTextBox1.SelectionFont.Italic Then
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Or FontStyle.Italic)
	Else
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Xor FontStyle.Italic)
	End If
End Sub

My code looks the same, but like I mentioned in my original statement, when the selected text has both Bold and Normal text in it, the richTextBox1.SelectionFont will be set to Nothing...

I have a RichTextBox. I process all the formatting and everything works fine. There is an exception. When I have selected text that have two states of formatting in it, the SelectionFont member is set to Nothing. I notice that WordPad recognizes this condition and kindof greys out the affected format. Let's take this example:

This text is in Bold.

The rest of the comments here and not in bold.

When I select these two lines, the SelectionFont gets set to Nothing. WordPad sets the Bold button grey, but still clickable. If I click the Bold button in this state, the entire selection is set to bold. How do I achieve this same functionality?

Hi!

Okay I got your reply.

>>>>How do I achieve this same functionality?

Are you using this code in Toolbar's Button OR CommandButton ???

Toolbar Button, but it doesn't really matter where. When a selection of Bold and Normal is selected, the RichTextBox.SelectionFont gets set to Nothing...

Edited 5 Years Ago by tim8w: n/a

Toolbar Button, but it doesn't really matter where. When a selection of Bold and Normal is selected, the RichTextBox.SelectionFont gets set to Nothing...

I know it doesn't matter, I was thinking from different point of view.

Here is a solution, in which I have set the text of form according to the status I printed. Where I set "this.text" you can set the image of the button like greyed scale image, original button image etc.

Note: (please test it in a separate project to see its working.)

Private Sub Bold_Click(sender As Object, e As EventArgs)
	If Not richTextBox1.SelectionFont.Bold Then
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Or FontStyle.Bold)
	Else
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Xor FontStyle.Bold)
	End If
	If Me.Text <> "default" Then
		Me.Text = "default"
	End If
End Sub

Private Sub Italic_Click(sender As Object, e As EventArgs)
	If Not richTextBox1.SelectionFont.Italic Then
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Or FontStyle.Italic)
	Else
		richTextBox1.SelectionFont = New Font(DirectCast(richTextBox1.SelectionFont.Clone(), Font), richTextBox1.SelectionFont.Style Xor FontStyle.Italic)
	End If
	If Me.Text <> "default" Then
		Me.Text = "default"
	End If
End Sub

Private Sub richTextBox1_MouseCaptureChanged(sender As Object, e As EventArgs)
	If richTextBox1.SelectionFont.Style = FontStyle.Regular Then
		If richTextBox1.SelectedRtf.Contains("\b") Then
			Me.Text = "Bold greyed."
		Else
			Me.Text = "default"
		End If
		If richTextBox1.SelectedRtf.Contains("\i") Then
			Me.Text = "Italic greyed."
		Else
			Me.Text = "default"
		End If
	Else
		Me.Text = richTextBox1.SelectionFont.Style.ToString()
	End If
End Sub

If you need any clarification, please let me know.

Edited 5 Years Ago by ShahanDev: Code converted to VB.NET

This article has been dead for over six months. Start a new discussion instead.