By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

ComboBox SelectedIndex

P: n/a
jim
How do I immediately set the SelectedIndex of a combobox
(dropdownlist) upon Form Load?

I'm getting out of range errors and I suspect it's due that the Form
has not been displayed yet.

Please help.. thanks.
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
It depends on when your combobox is loaded. If you are simply adding the
items at designtime via the combobox' Items property, then they should all
be available in the Form's Load event so this should work..

private void Form1_Load(object sender, System.EventArgs e)
{
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
}

Otherwise, remember that the items list is zero based. If you want to
select the last item in the list, you need to do it like this..

private void Form1_Load(object sender, System.EventArgs e)
{
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
}

because "comboBox1.Items.Count" *IS* out of range.

--
Leit Rachsor
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.