Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Toolstrip font combobox

Toolstrip font combobox

Scheduled Pinned Locked Moved C#
csharphelptutoriallearning
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    malcomhfc
    wrote on last edited by
    #1

    I have a toolstrip and added a combobox, i wish to populate that combo box with a list of fonts that will change the font in the textbox or richtextbox. I have searched everywhere and only found one example but it's code didn't seem to work. I have tried myself but since this is all for learning. Im using visual c# express. Any help will be great thanks, Malcom Malcom

    0 B 2 Replies Last reply
    0
    • M malcomhfc

      I have a toolstrip and added a combobox, i wish to populate that combo box with a list of fonts that will change the font in the textbox or richtextbox. I have searched everywhere and only found one example but it's code didn't seem to work. I have tried myself but since this is all for learning. Im using visual c# express. Any help will be great thanks, Malcom Malcom

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      If you're looking to get the list of all fonts on the system, then use something like this:

      System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();

      foreach(FontFamily family in families)
      {
      if(family.IsStyleAvailable(FontStyle.Regular))
      {
      Font font = new Font(family, 12, FontStyle.Regular);
      }
      }

      That gets every font style and creates a Font object from it. From there it's simple enough to add the Font name to a combo box, and hook the SelectionChanged event to create a font from its name and set the textbox's Font property

      M 1 Reply Last reply
      0
      • 0 0x3c0

        If you're looking to get the list of all fonts on the system, then use something like this:

        System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();

        foreach(FontFamily family in families)
        {
        if(family.IsStyleAvailable(FontStyle.Regular))
        {
        Font font = new Font(family, 12, FontStyle.Regular);
        }
        }

        That gets every font style and creates a Font object from it. From there it's simple enough to add the Font name to a combo box, and hook the SelectionChanged event to create a font from its name and set the textbox's Font property

        M Offline
        M Offline
        malcomhfc
        wrote on last edited by
        #3

        So in visual i create a new class and add that code in. Then i go to combobox and do the rest..? Sorry, still getting to grips with all of this, when i first started learning i was using smallbasic.

        0 1 Reply Last reply
        0
        • M malcomhfc

          So in visual i create a new class and add that code in. Then i go to combobox and do the rest..? Sorry, still getting to grips with all of this, when i first started learning i was using smallbasic.

          0 Offline
          0 Offline
          0x3c0
          wrote on last edited by
          #4

          That code will get a list of every installed font in the system. Double click on the Form, and you'll get a method called xxx_Load, where xxx is the name of your Form (not the Text property, that's just the caption). Put the code in there (don't forget to add a using System.Drawing statement at the top of your code file), but just after the line with new Font(..) in it, add an item to the combobox with the text of font.Name That lists the fonts used in the combobox. When you want to set the textbox's Font property, double click in the combobox, and put some code in there which creates a Font (using new) from the SelectedItem property of the combobox. Set the textbox's Font property to that Font object you just created

          M 1 Reply Last reply
          0
          • 0 0x3c0

            That code will get a list of every installed font in the system. Double click on the Form, and you'll get a method called xxx_Load, where xxx is the name of your Form (not the Text property, that's just the caption). Put the code in there (don't forget to add a using System.Drawing statement at the top of your code file), but just after the line with new Font(..) in it, add an item to the combobox with the text of font.Name That lists the fonts used in the combobox. When you want to set the textbox's Font property, double click in the combobox, and put some code in there which creates a Font (using new) from the SelectedItem property of the combobox. Set the textbox's Font property to that Font object you just created

            M Offline
            M Offline
            malcomhfc
            wrote on last edited by
            #5

            Ok, thanks alot ill give it a shot ;) edit: the word 'famlies' "foreach(FontFamily family in families)" is underlined in red: word does not exist in current context. Sorry again, ive added the using system statement etc, wont go away. :( edit: before i start to annoy you maybe i presented this out wrong. I created a project just to try this out and see if i can get it working. Thats why i still have all the names like form1 :P

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;
            using System.Drawing.Text;

            namespace WindowsFormsApplication1
            {
            public partial class Form1 : Form
            {
            public Form1()
            {
            InitializeComponent();
            }

                private void Form1\_Load(object sender, EventArgs e)
                {
                    System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
            
                    foreach (FontFamily family in families)
                    {
                        if (family.IsStyleAvailable(FontStyle.Regular))
                        {
                            Font font = new Font(family, 12, FontStyle.Regular);
                        }
                    }
                }
            
                private void comboBox1\_SelectedIndexChanged(object sender, EventArgs e)
                {
            
                }
            }
            

            }

            modified on Tuesday, May 19, 2009 11:53 AM

            M 0 3 Replies Last reply
            0
            • M malcomhfc

              Ok, thanks alot ill give it a shot ;) edit: the word 'famlies' "foreach(FontFamily family in families)" is underlined in red: word does not exist in current context. Sorry again, ive added the using system statement etc, wont go away. :( edit: before i start to annoy you maybe i presented this out wrong. I created a project just to try this out and see if i can get it working. Thats why i still have all the names like form1 :P

              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.Data;
              using System.Drawing;
              using System.Linq;
              using System.Text;
              using System.Windows.Forms;
              using System.Drawing.Text;

              namespace WindowsFormsApplication1
              {
              public partial class Form1 : Form
              {
              public Form1()
              {
              InitializeComponent();
              }

                  private void Form1\_Load(object sender, EventArgs e)
                  {
                      System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
              
                      foreach (FontFamily family in families)
                      {
                          if (family.IsStyleAvailable(FontStyle.Regular))
                          {
                              Font font = new Font(family, 12, FontStyle.Regular);
                          }
                      }
                  }
              
                  private void comboBox1\_SelectedIndexChanged(object sender, EventArgs e)
                  {
              
                  }
              }
              

              }

              modified on Tuesday, May 19, 2009 11:53 AM

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              the error is that 'families' does not exist. change it to 'fonts'. Its a mistake in the code

              Life goes very fast. Tomorrow, today is already yesterday.

              1 Reply Last reply
              0
              • M malcomhfc

                Ok, thanks alot ill give it a shot ;) edit: the word 'famlies' "foreach(FontFamily family in families)" is underlined in red: word does not exist in current context. Sorry again, ive added the using system statement etc, wont go away. :( edit: before i start to annoy you maybe i presented this out wrong. I created a project just to try this out and see if i can get it working. Thats why i still have all the names like form1 :P

                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.Data;
                using System.Drawing;
                using System.Linq;
                using System.Text;
                using System.Windows.Forms;
                using System.Drawing.Text;

                namespace WindowsFormsApplication1
                {
                public partial class Form1 : Form
                {
                public Form1()
                {
                InitializeComponent();
                }

                    private void Form1\_Load(object sender, EventArgs e)
                    {
                        System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
                
                        foreach (FontFamily family in families)
                        {
                            if (family.IsStyleAvailable(FontStyle.Regular))
                            {
                                Font font = new Font(family, 12, FontStyle.Regular);
                            }
                        }
                    }
                
                    private void comboBox1\_SelectedIndexChanged(object sender, EventArgs e)
                    {
                
                    }
                }
                

                }

                modified on Tuesday, May 19, 2009 11:53 AM

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                Even with that error fixed the code you have posted wont do what you want. so here are the fixes, the should work... 1. in you load event change the 'families' to 'fonts' 2. just before you foreach loop add the following line...

                comboBox1.Items.Clear();

                3. inside your foreach loop replace all code with this line only...

                comboBox1.Items.Add(family.Name);

                this should then populate you combo box with all the fonts 4. in your selected index changed event add the following code to alter you textbox font...

                textbox1.Font = new Font(comboBox1.Text, 10);//where 10 is the size of the font

                Life goes very fast. Tomorrow, today is already yesterday.

                M 1 Reply Last reply
                0
                • M malcomhfc

                  Ok, thanks alot ill give it a shot ;) edit: the word 'famlies' "foreach(FontFamily family in families)" is underlined in red: word does not exist in current context. Sorry again, ive added the using system statement etc, wont go away. :( edit: before i start to annoy you maybe i presented this out wrong. I created a project just to try this out and see if i can get it working. Thats why i still have all the names like form1 :P

                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Data;
                  using System.Drawing;
                  using System.Linq;
                  using System.Text;
                  using System.Windows.Forms;
                  using System.Drawing.Text;

                  namespace WindowsFormsApplication1
                  {
                  public partial class Form1 : Form
                  {
                  public Form1()
                  {
                  InitializeComponent();
                  }

                      private void Form1\_Load(object sender, EventArgs e)
                      {
                          System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
                  
                          foreach (FontFamily family in families)
                          {
                              if (family.IsStyleAvailable(FontStyle.Regular))
                              {
                                  Font font = new Font(family, 12, FontStyle.Regular);
                              }
                          }
                      }
                  
                      private void comboBox1\_SelectedIndexChanged(object sender, EventArgs e)
                      {
                  
                      }
                  }
                  

                  }

                  modified on Tuesday, May 19, 2009 11:53 AM

                  0 Offline
                  0 Offline
                  0x3c0
                  wrote on last edited by
                  #8

                  Whoops. I took the code from one of my snippets, so had to adapt it for some reason. Don't worry though, I'm not annoyed in the slightest. You're trying to learn, and that's always a good thing

                  1 Reply Last reply
                  0
                  • M musefan

                    Even with that error fixed the code you have posted wont do what you want. so here are the fixes, the should work... 1. in you load event change the 'families' to 'fonts' 2. just before you foreach loop add the following line...

                    comboBox1.Items.Clear();

                    3. inside your foreach loop replace all code with this line only...

                    comboBox1.Items.Add(family.Name);

                    this should then populate you combo box with all the fonts 4. in your selected index changed event add the following code to alter you textbox font...

                    textbox1.Font = new Font(comboBox1.Text, 10);//where 10 is the size of the font

                    Life goes very fast. Tomorrow, today is already yesterday.

                    M Offline
                    M Offline
                    malcomhfc
                    wrote on last edited by
                    #9

                    I get a error: foreach statement cannot operate on variables of type 'System.Drawing.Text.InstalledFontCollection' because <'System.Drawing.Text.InstalledFontCollection' does not contain a public definition for 'GetEnumerator' ................................... Thats after foreach (FontFamily family in fonts) - changing it from 'family' to 'fonts' This is so much harder than small basic but i like challenges, ones that not go so well :(

                    0 1 Reply Last reply
                    0
                    • M malcomhfc

                      I have a toolstrip and added a combobox, i wish to populate that combo box with a list of fonts that will change the font in the textbox or richtextbox. I have searched everywhere and only found one example but it's code didn't seem to work. I have tried myself but since this is all for learning. Im using visual c# express. Any help will be great thanks, Malcom Malcom

                      B Offline
                      B Offline
                      blackhattrick
                      wrote on last edited by
                      #10

                      Have you tried FontDialog? I dont know if its useful for u:

                      using System.Windows.Forms;

                      FontDialog fontDialog = new FontDialog();
                      if ( fontDialog.ShowDialog() != DialogResult.Cancel )
                      {
                      textBox.Font = fontDialog.Font;
                      }

                      M 1 Reply Last reply
                      0
                      • B blackhattrick

                        Have you tried FontDialog? I dont know if its useful for u:

                        using System.Windows.Forms;

                        FontDialog fontDialog = new FontDialog();
                        if ( fontDialog.ShowDialog() != DialogResult.Cancel )
                        {
                        textBox.Font = fontDialog.Font;
                        }

                        M Offline
                        M Offline
                        malcomhfc
                        wrote on last edited by
                        #11

                        Thanks for the imput, ive tried that one :) Just wanting the one for combo box i believe it's all me getting confused easy :D

                        1 Reply Last reply
                        0
                        • M malcomhfc

                          I get a error: foreach statement cannot operate on variables of type 'System.Drawing.Text.InstalledFontCollection' because <'System.Drawing.Text.InstalledFontCollection' does not contain a public definition for 'GetEnumerator' ................................... Thats after foreach (FontFamily family in fonts) - changing it from 'family' to 'fonts' This is so much harder than small basic but i like challenges, ones that not go so well :(

                          0 Offline
                          0 Offline
                          0x3c0
                          wrote on last edited by
                          #12

                          Change the foreach from fonts to fonts.Families

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups