Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 105,765 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,595 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Font ComboBox

2 Pages V  1 2 >  
Reply to this topicStart new topic

Font ComboBox

brottmayer
post 2 Feb, 2007 - 06: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

Go to the top of the page


brottmayer
post 6 Feb, 2007 - 06: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

Go to the top of the page

jayman9
post 7 Feb, 2007 - 04:20 PM
Post #3


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,241



Thanked 21 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


I think this will help you out.

http://www.startvbdotnet.com/controls/rtb.aspx
User is offlineProfile CardPM

Go to the top of the page

brottmayer
post 7 Feb, 2007 - 11: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

Go to the top of the page

jayman9
post 8 Feb, 2007 - 10:57 AM
Post #5


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,241



Thanked 21 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

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

Go to the top of the page

brottmayer
post 8 Feb, 2007 - 11: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

Go to the top of the page

jayman9
post 8 Feb, 2007 - 12:20 PM
Post #7


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,241



Thanked 21 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

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

Go to the top of the page

brottmayer
post 8 Feb, 2007 - 12:31 PM
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

Go to the top of the page

jayman9
post 8 Feb, 2007 - 12:35 PM
Post #9


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,241



Thanked 21 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Yes it is exactly the same.
User is offlineProfile CardPM

Go to the top of the page

brottmayer
post 8 Feb, 2007 - 01: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

Go to the top of the page

brottmayer
post 8 Feb, 2007 - 06: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

Go to the top of the page

jayman9
post 8 Feb, 2007 - 08:05 PM
Post #12


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,241



Thanked 21 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

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

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 02:28PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month