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

ComboBox SelectedIndex = -1 not working

P: n/a
I would like the user to have to select something in a
System.Windows.Forms.ComboBox.

private void MyForm_Load(object sender, System.EventArgs e)
{
...
comboBox.DataSource = data;
comboBox.SelectedIndex = -1;
...
}

Above code does work.

Any thoughts?

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <O4**************@tk2msftngp13.phx.gbl>, no****@cox.net
says...
I would like the user to have to select something in a
System.Windows.Forms.ComboBox.

private void MyForm_Load(object sender, System.EventArgs e)
{
...
comboBox.DataSource = data;
comboBox.SelectedIndex = -1;
...
}

Above code does work.


Working code? Usually people post here when they have problems with
non-working code. :)

I'm not clear on what the "problem" is. Are you saying you don't want
"-1" to clear the list?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 16 '05 #2

P: n/a
Hi Scott,

Instead of
comboBox.SelectedIndex = -1;

try using
comboBox.SelectedItem = null;
--
HTH
Stoitcho Goutsev (100) [C# MVP]
Nov 16 '05 #3

P: n/a
Everyone,

Thanks for the responses.

It was exactly what you mentioned. SelectedIndexChanged event to fire twice
and code some code behind effecting it.

Welcome me to the world of Component programming and Event Handlers.

"C Addison Ritchie" <CA*************@discussions.microsoft.com> wrote in
message news:8D**********************************@microsof t.com...
This code works fine for me. One thing to look out for though... setting
the SelectedIndex to any value will cause the SelectedIndexChanged event
to fire. Ensure that you don't have any code in that event that would
cause the issue you are experiencing.

In fact the following code causes the SelectedIndexChanged event to fire
twice.

ArrayList al = new ArrayList(5);

al.Add("Item1");
al.Add("Item2");
al.Add("Item3");
al.Add("Item4");
al.Add("Item5");

comboBox1.DataSource = al;

One other thing I noticed is that after setting the DataSource I didn't
have to even set the SelectedIndex to -1 as the combobox didn't have an
item selected.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.
"ScottO" wrote:
I would like the user to have to select something in a
System.Windows.Forms.ComboBox.

private void MyForm_Load(object sender, System.EventArgs e)
{
...
comboBox.DataSource = data;
comboBox.SelectedIndex = -1;
...
}

Above code does work.

Any thoughts?

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.