Home > DeveloperSection > Forums > how to set default value for comboBox?
Goti Bandu

Total Post:119

Points:835
Posted on    January-28-2014 12:04 AM

 C# C# 
Ratings:


 1 Reply(s)
 756  View(s)
Rate this:

Hey I am making a comboBox that is holding a list of all the fonts name on my system, however at the beginning it does not hold any value and user needs to click on it to open it and select an item from the list. my question is how to set the default value for my comboBox (for example 'Arial') in case if nothing has been selected by the user and not to give error.

        foreach (FontFamily fnt in fonts.Families)

        {

            comboBox1.Items.Add(fnt.Name);

        }



Pravesh Singh

Total Post:411

Points:2881
Posted on    January-28-2014 12:50 AM

Hi Goti,

You could search for a value such as "Arial" using FindString:

if (comboBox1.SelectedIndex == -1)

    comboBox1.SelectedIndex = comboBox1.FindString("Arial");

If you've got multiple entries starting with "Arial", the above will return the first match starting with Arial, so you may need to search for the exact string:

if (comboBox1.SelectedIndex == -1)

    comboBox1.SelectedIndex = comboBox1.FindStringExact("Arial Rounded MT");


Don't want to miss updates? Please click the below button!

Follow MindStick