Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SelectedIndex = -1 still shows first item selected

1 view
Skip to first unread message

Scott Hodson

unread,
Sep 2, 2003, 11:52:19 AM9/2/03
to
I have a combo box with a DropDownStyle of DropDownList, and when I force
SelectedIndex = -1 it still shows the first item as selected. Am I doing
something wrong here?


Herfried K. Wagner [MVP]

unread,
Sep 2, 2003, 12:28:39 PM9/2/03
to
Hello,

"Scott Hodson" <sc...@ubero.com> schrieb:


> I have a combo box with a DropDownStyle of DropDownList,
> and when I force SelectedIndex = -1 it still shows the first
> item as selected. Am I doing something wrong here?

http://support.microsoft.com/default.aspx?scid=kb;en-us;327244

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Scott Hodson

unread,
Sep 2, 2003, 3:34:45 PM9/2/03
to
The -1 twice thing didn't work, it still displays the first item, and
setting it to 0 then -1 gave me an exception. This article is for .NET 1.0,
I'm on 1.1. I'm thinking they're emulating HTML combo boxes for
compatibility across Windows Forms and Web Forms.

"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message
news:ugReJ9Wc...@TK2MSFTNGP10.phx.gbl...

Ying-Shen Yu[MS]

unread,
Sep 2, 2003, 10:46:15 PM9/2/03
to
Hi Scott,
I've tested on .NET v1.1. the problem of the bounded ComboBox still
exists on v1.1 but the work around provided by Herfried works on v1.1, and
I also tried first setting the SelectedIndex to 0 then -1, it didn't throw
exception.
Could you tell us more about how you use the ComboBox? What exception did
it throw out? Have you define some event handler on ComboBox?
And here is my test code, you may try it and let me know the difference
between our codes.(you should change the dataConnection property)

thanks!

using System;
using System.Windows.Forms;

namespace ComboBox_SelectedIndex
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private ComboBox_SelectedIndex.DataSet1 dataSet11;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.dataSet11 = new ComboBox_SelectedIndex.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.AllowDrop = true;
this.comboBox1.DataSource = this.dataSet11.Customers;
this.comboBox1.DisplayMember = "ContactTitle";
this.comboBox1.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.Location = new System.Drawing.Point(16, 72);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(200, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.ValueMember = "Region";
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Deselect";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT Address, City,
CompanyName, ContactName, ContactTitle, Country, CustomerID" +
", Fax, Phone, PostalCode, Region FROM Customers";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = @"Jet OLEDB:Global Partial Bulk
Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet
OLEDB:Database Password=;Data Source=""C:\Program Files\Microsoft
Office\OFFICE11\1033\FPNWIND.MDB"";Password=;Jet OLEDB:Engine Type=5;Jet
OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet
OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share
Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System
Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet
OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt
Database=False";
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
this.oleDbDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table",
"Customers", new System.Data.Common.DataColumnMapping[] {
new
System.Data.Common.DataColumnMapping("Address", "Address"),
new
System.Data.Common.DataColumnMapping("City", "City"),
new
System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"),
new
System.Data.Common.DataColumnMapping("ContactName", "ContactName"),
new
System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"),
new
System.Data.Common.DataColumnMapping("Country", "Country"),
new
System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
new
System.Data.Common.DataColumnMapping("Fax", "Fax"),
new
System.Data.Common.DataColumnMapping("Phone", "Phone"),
new
System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"),
new
System.Data.Common.DataColumnMapping("Region", "Region")})});
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("en-US");
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
comboBox1.SelectedIndex = 0;
comboBox1.SelectedIndex = -1;
}

private void Form1_Load(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Fill(dataSet11);
}
}
}

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Scott Hodson" <sc...@ubero.com>
| References: <uxaNj4Wc...@tk2msftngp13.phx.gbl>
<ugReJ9Wc...@TK2MSFTNGP10.phx.gbl>
| Subject: Re: SelectedIndex = -1 still shows first item selected
| Date: Tue, 2 Sep 2003 12:34:45 -0700
| Lines: 25
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#VmxMqac...@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: 64-164-53-130.ded.pacbell.net 64.164.53.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51513
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms

Scott Hodson

unread,
Sep 3, 2003, 2:45:06 AM9/3/03
to
Here's my code...

private void populateStatuses()

{

string sql = "" +

"SELECT jobStatusGuid AS [id], " +

"jobStatus AS [description] " +

"FROM JobStatus ";

contactBinding = Binder.BindComboBox(comboJobStatus, "JobStatus", sql,
db.Connection);

comboJobStatus.SelectedIndex = -1;

comboJobStatus.SelectedIndex = -1;

}

BindComboBox() is just a tool I use to bind data to combo boxes, given a SQL
statement and a connection.

I am not capturing any events on this combobox.

If I try the 0/-1 tip I get

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument
was out of the range of valid values.

Parameter name: '0' is not a valid value for 'index'.

Your code works because you are setting the listindex on a button click, I
am trying to set it while the form is loading. Even though I have bound the
data to the control it still acts like it doesn't have any data in it
because when I set the index to 0 it barks at me, and -1/-1 doesn't cause an
exception, but it's as if the data is actually loaded into the control at a
later time, even though I've already bound it via BindComboBox(). Here is
what that method does

public static BindingResults BindComboBox(ComboBox box, string tableName,
string sql, SqlConnection connection)

{

SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);

DataSet dataSet = new DataSet();

SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

adapter.Fill(dataSet, tableName);


box.DataSource = dataSet.Tables[tableName];

box.DisplayMember = "description";

box.ValueMember = "id";

return new BindingResults(dataSet, adapter);

}

However, I just got it to work. I called -1/-1 AFTER I show the form, which
must force the data bound data to be rendered/filled, then, when I set -1/-1
it appears blank. Weird.


"Ying-Shen Yu[MS]" <v-...@online.microsoft.com> wrote in message
news:7QXyJWcc...@cpmsftngxa06.phx.gbl...

Ying-Shen Yu[MS]

unread,
Sep 5, 2003, 4:05:51 AM9/5/03
to
Hi Scott,
the ComboBox will not get data right when you set databinding.
It uses a lazy initialization mechanism, and delays the realy getting data
work until it really needs displaying.
It's right to set SelectedItemIndex = -1 after the ComboBox is shown.


Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------

| From: "Scott Hodson" <sho...@yahoo.com>
| References: <uxaNj4Wc...@tk2msftngp13.phx.gbl>
<ugReJ9Wc...@TK2MSFTNGP10.phx.gbl>
<#VmxMqac...@tk2msftngp13.phx.gbl>
<7QXyJWcc...@cpmsftngxa06.phx.gbl>


| Subject: Re: SelectedIndex = -1 still shows first item selected

| Date: Tue, 2 Sep 2003 23:45:06 -0700
| Lines: 321


| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165

| Message-ID: <Oa2cVO3c...@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: ip68-4-169-213.oc.oc.cox.net 68.4.169.213
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51686
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms

Scott Hodson

unread,
Sep 5, 2003, 12:18:42 PM9/5/03
to
Yeah, that's what I eventually figured out. I just created a new Show()
method in my form

public new void Show()
{
base.Show();
combo.SelectedIndex = -1;
combo.SelectedIndex = -1;
}

"Ying-Shen Yu[MS]" <v-...@online.microsoft.com> wrote in message

news:cQOTiT4c...@cpmsftngxa06.phx.gbl...

Ahto Truu

unread,
Sep 9, 2003, 9:40:29 AM9/9/03
to

> public new void Show()
> {
> base.Show();
> combo.SelectedIndex = -1;
> combo.SelectedIndex = -1;
> }

Seeing this code reminds me of an old question: is there a reason why
the most common and trivial methods and properties (like the Show()
method above and the Text property of a TreeNode, to name just the ones
that bother me most) are not overloadable? Is there a real reason, or is
it just because the release date was looming and nobody had the time to
stop and think?

Best,
Ahto

Joe White

unread,
Sep 13, 2003, 9:15:25 PM9/13/03
to
I can't disagree. However, you *can* override Show(), indirectly. Just
override SetVisibleCore().

Don't know if there's anything similar for TreeNode.Text or not. You
can always fire up Reflector and take a look at the code.

0 new messages