Hi Guys.

I got two combo boxes, one has numbers from 1-75 and the other one has all system fonts. I have a richtextbox as well. I basically want the user to be able to choose the font and font size from the combo box and then apply to the richtextbox. However I want about two functions. If the user has highlighted selected text then changes should only be applied to the highlighted text, if the user has not highlighted an item but changes fonts or font size, then the changes should be made to the next characters inputed.

How can I do this?

CriticalError
Deleted Member

Try this :

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    RichTextBox1.SelectionFont = New Font("Tahoma", CInt(ComboBox1.SelectedItem.ToString), RichTextBox1.SelectionFont.Style)
End Sub

Hi Jx

I got this code

Dim ComboFonts As System.Drawing.Font


        ComboFonts = Document.SelectionFont
        Document.SelectionFont = New System.Drawing.Font(fontSelection.Text, Document.SelectionFont.Size, Document.SelectionFont.Style)

it works but sometimes I get the error below:

Object reference not set to an instance of an object.

I don't exactly know how it happens just randomly.

Place the code in a try catch block

Try
'Your code here
Catch ex As Exception
   MsgBox(ex.stacktrace.Tostring)
End Try

This will show the exact line that is throwing the error.

Or, alternately. Test each value before performing the action.

If fontSelection.Text <> "" AND Document.SelectionFont.Size ISNOT Nothing AND Document.SelectionFont.Style ISNOT Nothing Then
End If

Edited 4 Years Ago by Begginnerdev: n/a

Line 341 is causing the error:

that this code

Document.SelectionFont = New System.Drawing.Font(fontSelection.Text, Document.SelectionFont.Size, Document.SelectionFont.Style)

Line 341 is causing the error:

that this code

Document.SelectionFont = New System.Drawing.Font(fontSelection.Text, Document.SelectionFont.Size, Document.SelectionFont.Style)

In your first post, you have two comboboxes for Font name and font size.
You said that you want to select a font size from combo box and apply it to richtextbox. But i didn't see it in your code. Did you tried my code?

Document.SelectionFont = New Font(ComboBox2.SelectedItem.ToString, CInt(ComboBox1.SelectedItem.ToString), Document.SelectionFont.Style)

Edited 4 Years Ago by Jx_Man: n/a

That works! Thanks, but your other code never worked. ( the first post)

Edited 4 Years Ago by CriticalError: n/a

That works! Thanks, but your other code never worked. ( the first post)

You're Welcome. :D
Don't forget Mark this thread as Solved.

Edited 4 Years Ago by Jx_Man: n/a

This question has already been answered. Start a new discussion instead.