Member Avatar

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?

Recommended Answers

[QUOTE=CriticalError;1781617]Line 341 is causing the error:

that this code

[code]Document.SelectionFont = New System.Drawing.Font(fontSelection.Text, Document.SelectionFont.Size, Document.SelectionFont.Style)[/code][/QUOTE]
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 …

Jump to Post

All 7 Replies

Re: Want combobox fonts to apply to richtextbox 80 80

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
Re: Want combobox fonts to apply to richtextbox 80 80
Member Avatar

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.

Re: Want combobox fonts to apply to richtextbox 80 80

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
Re: Want combobox fonts to apply to richtextbox 80 80
Member Avatar

Line 341 is causing the error:

that this code

Document.SelectionFont = New System.Drawing.Font(fontSelection.Text, Document.SelectionFont.Size, Document.SelectionFont.Style)
Re: Want combobox fonts to apply to richtextbox 80 80

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)
Re: Want combobox fonts to apply to richtextbox 80 80
Member Avatar

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

Re: Want combobox fonts to apply to richtextbox 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.