0

I have a combobox and list of it was from separate strings declared on a class .

sample:
as
asd
asdf
asdfg
asdfg

Everytime when i run it, it always selects the last part of list of combobox instead of firstpart. It is selecting asdfg instead of as.My question is how to make the list to be selected from first, that is starting from as as the selected index for the combobox? (or always in accending mode when alphabetically arranged)? thanks in advance..

coder
  • 1,980
  • 3
  • 21
  • 32

2 Answers2

1

You can manually set the SelectedIndex to 0 after adding elements to the ComboBox.

Thus you get:

comboBox.Items.Add("as");
comboBox.Items.Add("asd");
comboBox.Items.Add("asdf");
comboBox.Items.Add("asdfg");
comboBox.SelectedIndex = 0;
sohil
  • 508
  • 1
  • 3
  • 14
  • InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex –  Mar 19 '13 at 10:59
  • Can you edit your original question to reflect the complete current code? – sohil Mar 19 '13 at 11:06
0

you can try by sort and reverse :

 private void Form1_Load(object sender, System.EventArgs e)
 {
   ArrayList list = ArrayList.Adapter(comboBox1.Items);
   list.Sort();
   // if you want to reverse
   list.Reverse();
   comboBox1.SelectedItem=0;
 }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • From what I understand, the OP wants to select the first item by default and not sort existing items. – sohil Mar 19 '13 at 12:16
  • the arraylist doesnt exist –  Mar 20 '13 at 03:19
  • i put this: using System.Collections; and i worked .problem was it just reverse but selected item was still the last :( ... –  Mar 20 '13 at 03:44
  • i thought if it was reverse then selected item will change also but i was wrong ... –  Mar 20 '13 at 03:44
  • it worked when i changed it to lb.SelectedItem = 1; bigthanks! –  Mar 20 '13 at 03:51