Windows Develop Bookmark and Share   
 index > Windows Forms General > ToolStripComboBox setting SelectedIndex to -1 doesn't work
 

ToolStripComboBox setting SelectedIndex to -1 doesn't work

Right, this is very simple. The code is supposed to set ToolStripComboBox.SelectedIndex = -1. What happens when you try it, is that the selected index turns to -1 only when the current selectedIndex is set to 0. A bug?

Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tests
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}

public struct MyStruct
{
int x;
string name;
public int ID
{
get { return x; }
}
public string Name
{
get { return name; }
}
public MyStruct(int x, string n)
{
this.x = x;
name = n;
}
}

private void Form4_Load(object sender, EventArgs e)
{

MyStruct[] ms = { new MyStruct(1, "one"), new MyStruct(2, "two"), new MyStruct(3, "three") };

DataTable t = new DataTable();
t.Columns.Add("ID", typeof(int));
t.Columns.Add("Name", typeof(string));
foreach (MyStruct s in ms)
{
DataRow r = t.NewRow();
r[0] = s.ID;
r[1] = s.Name;
t.Rows.Add(r);
}
comboBox1.ComboBox.DisplayMember = "Name";
comboBox1.ComboBox.ValueMember = "ID";
comboBox1.ComboBox.DataSource = t;
}

private void ToolStripComboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
comboBox1.ComboBox.SelectedIndex = -1;

//Same goes forcomboBox1.ComboBox.SelectedItem = null;

e.SuppressKeyPress = true;
}
}
}
}


Any ideas?

Nieve  Tuesday, September 25, 2007 3:24 PM

Hi,

It seems you have renamed your ToolStripComBox1 control to comboBox1. However the event is still registered as ToolStripComboBox1_KeyDown. Recreate theevent so that it is comboBox1_KeyDown

Code Snippet

public struct MyStruct

{

int x;

string name;

public int ID

{

get { return x; }

}

public string Name

{

get { return name; }

}

public MyStruct(int x, string n)

{

this.x = x;

name = n;

}

}

private void toolStripComboBox1_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Delete)

{

toolStripComboBox1.ComboBox.SelectedIndex = -1;

//Same goes for comboBox1.ComboBox.SelectedItem = null;

e.SuppressKeyPress = true;

}

}

private void Form1_Load(object sender, EventArgs e)

{

MyStruct[] ms = { new MyStruct(1, "one"), new MyStruct(2, "two"), new MyStruct(3, "three") };

DataTable t = new DataTable();

t.Columns.Add("ID", typeof(int));

t.Columns.Add("Name", typeof(string));

foreach (MyStruct s in ms)

{

DataRow r = t.NewRow();

r[0] = s.ID;

r[1] = s.Name;

t.Rows.Add(r);

}

toolStripComboBox1.ComboBox.DisplayMember = "Name";

toolStripComboBox1.ComboBox.ValueMember = "ID";

toolStripComboBox1.ComboBox.DataSource = t;

}

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----

Suprotim Agarwal  Tuesday, September 25, 2007 3:41 PM

Hi,

It seems you have renamed your ToolStripComBox1 control to comboBox1. However the event is still registered as ToolStripComboBox1_KeyDown. Recreate theevent so that it is comboBox1_KeyDown

Code Snippet

public struct MyStruct

{

int x;

string name;

public int ID

{

get { return x; }

}

public string Name

{

get { return name; }

}

public MyStruct(int x, string n)

{

this.x = x;

name = n;

}

}

private void toolStripComboBox1_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Delete)

{

toolStripComboBox1.ComboBox.SelectedIndex = -1;

//Same goes for comboBox1.ComboBox.SelectedItem = null;

e.SuppressKeyPress = true;

}

}

private void Form1_Load(object sender, EventArgs e)

{

MyStruct[] ms = { new MyStruct(1, "one"), new MyStruct(2, "two"), new MyStruct(3, "three") };

DataTable t = new DataTable();

t.Columns.Add("ID", typeof(int));

t.Columns.Add("Name", typeof(string));

foreach (MyStruct s in ms)

{

DataRow r = t.NewRow();

r[0] = s.ID;

r[1] = s.Name;

t.Rows.Add(r);

}

toolStripComboBox1.ComboBox.DisplayMember = "Name";

toolStripComboBox1.ComboBox.ValueMember = "ID";

toolStripComboBox1.ComboBox.DataSource = t;

}

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----

Suprotim Agarwal  Tuesday, September 25, 2007 3:41 PM

You can use google to search for other answers

Custom Search

More Threads

• Mouse over button shows highlighted background with custom image
• ActiveX control crashing app when hitting 'x", but not "stop" in debugger. Started in VS2005
• Properties of a control
• custom method no longer triggers form_load event
• resources.GetObject appears to be failing. Why?
• Controlling a form controls from an opposite form.
• signature capturing from third party tool
• Datagridview question?
• customized caption bar
• BackgroundWorker problem: "object is currently in use elsewhere"