Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB Expert!

Join 244,116 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,386 people online right now. Registration is fast and FREE... Join Now!




Font ComboBox

 
Reply to this topicStart new topic

Font ComboBox

brottmayer
2 Feb, 2007 - 05:03 PM
Post #1

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
Hello,

I've created a Font ComboBox, but now i am trying to have the selected font change the highlighted text. How could I go about accomplishing that. Here's the code that I have to show the font:

CODE

Private Sub m_ComboBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontBox.Click
        Dim colFonts As Drawing.Text.InstalledFontCollection = New Drawing.Text.InstalledFontCollection
        Dim fontFamilies() As FontFamily = colFonts.Families
        Dim i As Integer

        For i = 0 To fontFamilies.Length - 1 Step 1
            Me.FontBox.Items.Add(fontFamilies(i).Name)
        Next

        Me.FontBox.Text = "Times New Roman"
        If Me.FontBox.Text = Windows.Forms.DialogResult.OK Then
            'Not sure what to put, i copy this code from the code
            'above utilizing the font dialog control. How do I get this
            'selected font from this combobox to change the font in
            'the RichTextBox?
            RichTextBox.SelectionFont = FontDialog.Font
        End If
    End Sub


Any help would be great thanks.

User is offlineProfile CardPM
+Quote Post


brottmayer
RE: Font ComboBox
6 Feb, 2007 - 05:37 PM
Post #2

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(brottmayer @ 2 Feb, 2007 - 06:03 PM) *

Hello,

I've created a Font ComboBox, but now i am trying to have the selected font change the highlighted text. How could I go about accomplishing that. Here's the code that I have to show the font:

CODE

Private Sub m_ComboBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontBox.Click
        Dim colFonts As Drawing.Text.InstalledFontCollection = New Drawing.Text.InstalledFontCollection
        Dim fontFamilies() As FontFamily = colFonts.Families
        Dim i As Integer

        For i = 0 To fontFamilies.Length - 1 Step 1
            Me.FontBox.Items.Add(fontFamilies(i).Name)
        Next

        Me.FontBox.Text = "Times New Roman"
        If Me.FontBox.Text = Windows.Forms.DialogResult.OK Then
            'Not sure what to put, i copy this code from the code
            'above utilizing the font dialog control. How do I get this
            'selected font from this combobox to change the font in
            'the RichTextBox?
            RichTextBox.SelectionFont = FontDialog.Font
        End If
    End Sub


Any help would be great thanks.


Maybe I need to give a little more explanation as to what I am trying to accomplish. When working with Microsoft Word, there is a combobox on a toolbar that allows you to change the selected font of your original text to another selected in that combobox. all this without having to go through the process of the FontDialog(). Does anyone have an idea of how i can accomplish that task? Any and all help would be great. THANKS!!!
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Font ComboBox
7 Feb, 2007 - 03:20 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,062



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
I think this will help you out.

http://www.startvbdotnet.com/controls/rtb.aspx

User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Font ComboBox
7 Feb, 2007 - 10:49 PM
Post #4

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 7 Feb, 2007 - 04:20 PM) *

I think this will help you out.

http://www.startvbdotnet.com/controls/rtb.aspx



No, I've been to that site a while back and that's not what I was thinking of. I was thinking of utilizing a combobox, that lists all of the fonts that is installed in the fonts folder and when you select a specific font, the RichTextBox.SelectedText (or SelectedFont, not sure exactly) will change to that particular selected font from the combobox. What do you think? Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Font ComboBox
8 Feb, 2007 - 09:57 AM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,062



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
The concept is exactly the same, the only difference is that you will select the font name from a combo box instead of providing it in your code.

Do you know how to read the names of the fonts on your system and load them into a combobox?
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Font ComboBox
8 Feb, 2007 - 10:05 AM
Post #6

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 8 Feb, 2007 - 10:57 AM) *

The concept is exactly the same, the only difference is that you will select the font name from a combo box instead of providing it in your code.

Do you know how to read the names of the fonts on your system and load them into a combobox?



No I don't actually, if you could help me that would be great. Also, I am a student of learning, and if you could explain the steps (to the best you can) that would also be great. Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Font ComboBox
8 Feb, 2007 - 11:20 AM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,062



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Reading the font name is actually quite easy. You will simply need a FOR loop to iterate through all of the True Type fonts on your system. You would put this loop inside your Forms Load event. It will populate, in this case ComboBox1, with all the fonts in your system.

CODE

        For Each font As FontFamily In FontFamily.Families
            Me.ComboBox1.Items.Add(font.Name)
        Next font


You could use the SelectedIndexChanged event of the combobox to cause the font to change. Which means when you SELECT, by clicking the font name in the combo box it will change the selected text automatically. You would put the following line of code inside the SelectedIndexChanged event of your combobox.

Now comes the important part causing the selected font in the RichTextBox to change.
CODE

Me.RichTextBox1.SelectionFont = New Font(Me.ComboBox1.Text, 12)


In this example I am supplying two parameters to the New Font. The first parameter is the Text, which is the font name, of the combobox and the second parameter is the size of the font. I chose to keep it static for this example at 12 points.

But you could create a second combobox which contains all the different font sizes in points.
In which case the code would look like this:
CODE

Me.RichTextBox1.SelectionFont = New Font(Me.ComboBox1.Text, CInt(Me.ComboBox2.Text))


You need to convert the text value into a integer value for the point size of the font, hence the CInt.

Thats all you need to change the font of selected text in a RichTextBox.

Here is a screenshot of a simple one I put together for this example.


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Font ComboBox
8 Feb, 2007 - 11:31 AM
Post #8

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 8 Feb, 2007 - 12:20 PM) *



Jayman9, that looks really simple, I will try that tonight when I get to my personal computer. Thanks.

Also, another question, that goes along the same line...the style box that is within Microsoft word, would technically follow the same function as you described? Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Font ComboBox
8 Feb, 2007 - 11:35 AM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,062



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Yes it is exactly the same.
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Font ComboBox
8 Feb, 2007 - 12:39 PM
Post #10

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 8 Feb, 2007 - 12:35 PM) *

Yes it is exactly the same.



Sweet, thanks man!
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Font ComboBox
8 Feb, 2007 - 05:44 PM
Post #11

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(brottmayer @ 8 Feb, 2007 - 01:39 PM) *

QUOTE(jayman9 @ 8 Feb, 2007 - 12:35 PM) *

Yes it is exactly the same.



Sweet, thanks man!



Jayman9, the changing of the fonts worked perfectly, but for some reason, when i try to implement the code to change the selected text size, it's giving me an error. But the error also occurs when I try to change the selected text's font and it would give me an error with this particular code: Me.RichTextBox.SelectionFont = New Font(Me.FontBox.Text, CInt(Me.SizeBox.Text))

Here's the code that I used, which you provided and for somereason it's not working, what do you think's going on?

CODE

    'Font ComboBox
    Private Sub FontBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontBox.Click
        For Each font As FontFamily In FontFamily.Families
            Me.FontBox.Items.Add(font.Name)
        Next font
    End Sub

    Private Sub FontBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontBox.SelectedIndexChanged
        'Me.RichTextBox.SelectionFont = New Font(Me.FontBox.Text, 12)
        Me.RichTextBox.SelectionFont = New Font(Me.FontBox.Text, CInt(Me.SizeBox.Text))
    End Sub


Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Font ComboBox
8 Feb, 2007 - 07:05 PM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,062



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Oops, my bad I forgot the SelectedIndexChanged method will fire during the load. And since there is no values in the second combobox while it is filling the first combobox, it is causing an exception.

Simple solution though, just create a module level Boolean variable to be set to true only when the form is loading, the last statement in your Load event will be to set it to false.

Then enclose the statement inside the SelectedIndexChanged method in an IF statement that will only allow it to run if the boolean is false.

One last thing I forgot to tell you since you now have two seperate comboboxes you want the SelectedIndexChange method to handle both of them. To make one SelectedIndexChanged method handle more than on combobox just add it after the word Handles as I've done.

CODE

Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged


Here is the code I put together so you can compare.
CODE

Public Class Form1

    'boolean is set to true only on Form Load
    Dim loading As Boolean = True

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.RichTextBox1.Text = "This is a test of the properties of changing the font style inside a richtextbox"

        For Each font As FontFamily In FontFamily.Families
            Me.ComboBox1.Items.Add(font.Name)
        Next font

        Dim x As Integer
        'loading values into the font size combobox
        For x = 1 To 48
            Me.ComboBox2.Items.Add(x)
        Next x

        'set the default values in the combo boxes
        Me.ComboBox1.SelectedItem = Me.RichTextBox1.Font.Name
        Me.ComboBox2.SelectedIndex = 9

        'Done loading comboboxes set to False
        loading = False
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged

        If loading = False Then
            Me.RichTextBox1.SelectionFont = New Font(Me.ComboBox1.Text, CInt(Me.ComboBox2.Text))
        End If
    End Sub

End Class


Did you notice this bit of code, it sets the comboboxes to the current font and sets the font size to 10.
CODE

        Me.ComboBox1.SelectedItem = Me.RichTextBox1.Font.Name
        Me.ComboBox2.SelectedIndex = 9

User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Font ComboBox
8 Feb, 2007 - 07:57 PM
Post #13

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(jayman9 @ 8 Feb, 2007 - 08:05 PM) *



Jayman, thanks, it's working now and I understand to functions of it as well. One quick question and then we'd be done. Is there a wayto have it automatically show the font and size in their respective combobox the moment the form is loaded? Right now it automatically fills in when you click on the comboboxes. Any thoughts? Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Font ComboBox
8 Feb, 2007 - 08:43 PM
Post #14

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,062



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
See the end of my previous post for your answer and note where it is located in the Form Load event.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 02:28AM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month