Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need code for Combobox SelectedIndexChanged-event

Posted on 2014-01-23
2
Medium Priority
?
334 Views
Last Modified: 2014-01-23
Dear Experts,

I want to try to make a Character Map just like Windows has. I have a combobox on my
form to get the System Fonts and I have used a DataGridView to display the characters.
Now only thing I need is to program the SelectedIndexChanged-event to display the
characters in DataGridView which belongs to the fontname when the user selects a font in the combobox. I have searched and found only vb-code that I don't understand. Who can help me?

        private const int NoOfCols = 16;
        private const int ColWidth = 24;

        public CharMap()
        {
            InitializeComponent();
        }

        private void CharMap_Load(object sender, EventArgs e)
        {
            //Set txtCopyChar as focus
            this.ActiveControl = txtCopyChar;

            //Get a list of font names into combo box.
            InstalledFontCollection fc = new InstalledFontCollection();
            foreach (FontFamily Item in fc.Families)
            {
                cboFontName.Items.Add(Item.Name);
            }
            cboFontName.SelectedIndex = 0;

            //Setup datagrid
            SetupDataGrid();
            //Select the first item
            CharGrid[1, 0].Selected = true;
        }

        private void SetupDataGrid()
        {
            //Number of columns to create.
            CharGrid.ColumnCount = NoOfCols;

            foreach (DataGridViewColumn Column in CharGrid.Columns)
            {
                //Set column width.
                Column.Width = ColWidth;
            }

            //Add rows
            CharGrid.Rows.Add(14);

            //Add chars to data grid
            AddCharsToGrid(0, 32);
            AddCharsToGrid(1, 48);
            AddCharsToGrid(2, 64);
            AddCharsToGrid(3, 80);
            AddCharsToGrid(4, 96);
            AddCharsToGrid(5, 112);
            AddCharsToGrid(6, 128);
            AddCharsToGrid(7, 144);
            AddCharsToGrid(8, 160);
            AddCharsToGrid(9, 176);
            AddCharsToGrid(10, 196);
            AddCharsToGrid(11, 208);
            AddCharsToGrid(12, 224);
            AddCharsToGrid(13, 240);

        }

        private void AddCharsToGrid(int Row, int Index)
        {
            for (int Counter = 0; Counter <= 15; Counter++)
            {
                CharGrid[Counter, Row].Value = (char)(Index + Counter);
            }
        }

Open in new window


Greetings, Peter Kiers
0
Comment
Question by:peterkiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 39803473
In the CharMap_Load function, add this line between your existing lines 19/20 as posted above):
cboFontName.SelectedIndexChanged += new EventHandler(cboFontName_SelectedIndexChanged); // attach the event handler

Open in new window

Then, add the event handler itself:
        void cboFontName_SelectedIndexChanged(object sender, EventArgs e) {
            try {
                CharGrid.Font = new Font(cboFontName.SelectedItem.ToString(), CharGrid.Font.Size);
            }
            catch (Exception ex) {
                //MessageBox.Show("error while setting font: " + ex.Message);
            }
        }

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 39804271
Thanks it works great. Greetings, Peter Kiers
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question