Member Avatar for CriticalError

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?

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
Member Avatar for CriticalError

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
Member Avatar for CriticalError

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)
Member Avatar for CriticalError

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.