472,793 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,793 software developers and data experts.

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 16037
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
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
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
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
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
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
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
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
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
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.