School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,079 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,794 people online right now. Registration is fast and FREE... Join Now!



Font ComboBox

Font ComboBox Rate Topic: -----

#1 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 02 February 2007 - 05: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:

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.
Was This Post Helpful? 0
  • +
  • -


#2 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 06 February 2007 - 05:37 PM

View Postbrottmayer, on 2 Feb, 2007 - 06:03 PM, said:

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:

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!!!
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is online

  • Student of Life
  • Icon
  • View blog
  • Group: Admins
  • Posts: 8,831
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Posted 07 February 2007 - 03:20 PM

I think this will help you out.

http://www.startvbdo...ntrols/rtb.aspx
Was This Post Helpful? 0
  • +
  • -

#4 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 07 February 2007 - 10:49 PM

View Postjayman9, on 7 Feb, 2007 - 04:20 PM, said:

I think this will help you out.

http://www.startvbdo...ntrols/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.
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is online

  • Student of Life
  • Icon
  • View blog
  • Group: Admins
  • Posts: 8,831
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Posted 08 February 2007 - 09: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?
Was This Post Helpful? 0
  • +
  • -

#6 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 08 February 2007 - 10:05 AM

View Postjayman9, on 8 Feb, 2007 - 10:57 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is online

  • Student of Life
  • Icon
  • View blog
  • Group: Admins
  • Posts: 8,831
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Posted 08 February 2007 - 11:20 AM

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.

		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.
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:
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

Was This Post Helpful? 0
  • +
  • -

#8 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 08 February 2007 - 11:31 AM

View Postjayman9, on 8 Feb, 2007 - 12:20 PM, said:




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.
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is online

  • Student of Life
  • Icon
  • View blog
  • Group: Admins
  • Posts: 8,831
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Posted 08 February 2007 - 11:35 AM

Yes it is exactly the same.
Was This Post Helpful? 0
  • +
  • -

#10 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 08 February 2007 - 12:39 PM

View Postjayman9, on 8 Feb, 2007 - 12:35 PM, said:

Yes it is exactly the same.



Sweet, thanks man!
Was This Post Helpful? 0
  • +
  • -

#11 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 08 February 2007 - 05:44 PM

View Postbrottmayer, on 8 Feb, 2007 - 01:39 PM, said:

View Postjayman9, on 8 Feb, 2007 - 12:35 PM, said:

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?

	'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.
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is online

  • Student of Life
  • Icon
  • View blog
  • Group: Admins
  • Posts: 8,831
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Posted 08 February 2007 - 07:05 PM

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.

 Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged


Here is the code I put together so you can compare.
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.
		Me.ComboBox1.SelectedItem = Me.RichTextBox1.Font.Name
		Me.ComboBox2.SelectedIndex = 9


Was This Post Helpful? 0
  • +
  • -

#13 brottmayer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 67
  • Joined: 21-January 07


Dream Kudos: 0

Posted 08 February 2007 - 07:57 PM

View Postjayman9, on 8 Feb, 2007 - 08:05 PM, said:



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.
Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is online

  • Student of Life
  • Icon
  • View blog
  • Group: Admins
  • Posts: 8,831
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Posted 08 February 2007 - 08:43 PM

See the end of my previous post for your answer and note where it is located in the Form Load event.
Was This Post Helpful? 0
  • +
  • -

#15 WrestlerOfWisdom  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 25-January 10


Dream Kudos: 0

Posted 25 January 2010 - 08:15 AM

It just won't work for me. The fonts will appear in the combo box like the font sizes in the other one but, instead of working, it throws an Inner Exception. Still, I think my syntax of the code is correct. The conclusion is totally yours. Check out my code below and tell me which parts of it to correct. Thanks in advance. By the way, I'm a new member ;)

Public Class Workspace
	Dim loading As Boolean = True
	Private Sub ButtonItem13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem13.Click
		Application.Exit()
	End Sub

	Private Sub Workspace_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		For Each font As FontFamily In FontFamily.Families
			Me.ComboBoxItem1.Items.Add(font.Name)
		Next font

		Dim x As Integer
		For x = 8 To 48
			Me.ComboBoxItem2.Items.Add(x)
		Next x

		Me.ComboBoxItem1.SelectedItem = Me.RichTextBox1.Font.Name
		Me.ComboBoxItem2.SelectedIndex = 1

		loading = False
	End Sub

	Private Sub ComboBoxItem1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxItem1.SelectedIndexChanged, ComboBoxItem2.SelectedIndexChanged
		If loading = False Then
			Me.RichTextBox1.SelectionFont = New Font(Me.ComboBoxItem1.Text, CInt(Me.ComboBoxItem2.Text))
		End If
	End Sub

	Private Sub ButtonItem14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem14.Click
		RichTextBox1.Copy()
	End Sub

	Private Sub ButtonItem15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem15.Click
		RichTextBox1.Paste()
	End Sub

	Private Sub ButtonItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem8.Click
		RichTextBox1.Cut()
	End Sub

	Private Sub ButtonItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem9.Click
		RichTextBox1.Clear()
	End Sub

	Private Sub ButtonItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem5.Click
		RichTextBox1.Undo()
	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month