473,508 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ComboBox SelectedIndex = -1 not working

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
3 16177
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
Hi Scott,

Instead of
comboBox.SelectedIndex = -1;

try using
comboBox.SelectedItem = null;
--
HTH
Stoitcho Goutsev (100) [C# MVP]
Nov 16 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2744
by: Mr. B | last post by:
Here's the situation... You've a combobox with Items already added. Say they look like this (or even lines of text): 10-00-232 10-00-256 10-01-006 10-01-213 10-02-200
9
56500
by: Steffen Laser | last post by:
Hi group, I have a problem that I already have posted to the german C# newsgroup. Since nobody could help me there, I'd like to try it here again: I set the selected item of a combobox like...
2
4316
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
3
3793
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
5
1599
by: Mika M | last post by:
Hello! I have Windows Forms application form containing TextBoxes, six ComboBoxes, and DataGrid for details. I have created DataSet with needed tables, and created relations between tables, and...
5
9149
by: Eric A. Johnson | last post by:
Hi Everyone, I am at my wit's end here. I have a combobox (combyQueryTitle) that I need to use in order to select a query for my database project. Therefore, I am using the...
6
3485
by: Scotty | last post by:
Hi, I need some info How to return the value from the database My combobox Me.cboTypeAdres.Items.Add("Type Adres") Me.cboTypeAdres.Items.Add("Leverancier")...
0
1992
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
3
1906
by: Koliber (js) | last post by:
how to put a text value in edit field of combobox when this text value is none of contained if combobox or how to change selected by click text value from combobox without removing it and...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.