0

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);

        }
CowBoy
  • 185
  • 5
  • 19

2 Answers2

1

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");
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • i actually don't want to get the first value, but i want to get only 'Arial' as a default value. i want to set it as a default value in case if nothing has been selected from the comboBox. do i need to use any 'if' statement to check whether an item has selected or not? – CowBoy Jan 24 '14 at 17:24
  • and how should i set the value to i.e. 'Arial'? – CowBoy Jan 24 '14 at 17:28
  • and where should i put the code? in my form? or in my button? – CowBoy Jan 24 '14 at 17:30
-1

You can just do:

comboBox1.SelectedValue = "Arial";

(assuming there is an element with value = Arial)