Discussion:
Found bug in ListBox control (.NET 3.5)
(too old to reply)
Jeff
2010-05-19 15:30:10 UTC
Permalink
Hello world,

I am just trying to get this written down somewhere, in hopes that
someone at Microsoft will see it and turn in the bug.

The ListBox control does not properly sort its items if you set the
Sorted property to true, and bind it to an IList.

For example, here is a simple class that I can make an array of
(arrays implement IList):
public class ItemBox
{
public string DisplayMemberStr
{
get;
set;
}
public string ValueMemberStr
{
get;
set;
}
}

Now, I initialize my listbox in a form_load method like so:
ItemBox[] iBox = new ItemBox[7];
iBox[0] = new ItemBox() { DisplayMemberStr = "Display5",
ValueMemberStr = "Value5" };
iBox[1] = new ItemBox() { DisplayMemberStr = "Display1",
ValueMemberStr = "Value1" };
iBox[2] = new ItemBox() { DisplayMemberStr = "Display3",
ValueMemberStr = "Value3" };
iBox[3] = new ItemBox() { DisplayMemberStr = "Display4",
ValueMemberStr = "Value4" };
iBox[4] = new ItemBox() { DisplayMemberStr = "Display6",
ValueMemberStr = "Value6" };
iBox[5] = new ItemBox() { DisplayMemberStr = "Display7",
ValueMemberStr = "Value7" };
iBox[6] = new ItemBox() { DisplayMemberStr = "Display2",
ValueMemberStr = "Value2" };

lbTest.DataSource = iBox;


Note that in my designer, I have set the Sorted property to true, and
set the display and value member properties at design-time, like this:
//
// lbTest
//
this.lbTest.DisplayMember = "DisplayMemberStr";
this.lbTest.FormattingEnabled = true;
this.lbTest.Location = new System.Drawing.Point(12, 12);
this.lbTest.Name = "lbTest";
this.lbTest.Size = new System.Drawing.Size(266, 251);
this.lbTest.Sorted = true;
this.lbTest.TabIndex = 0;
this.lbTest.ValueMember = "ValueMemberStr";
this.lbTest.SelectedIndexChanged += new
System.EventHandler(this.lbTest_SelectedIndexChanged);

I also attached a simple onSelectedIndexChanges event, like this:

private void lbTest_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbTest.SelectedIndex > -1)
{
if (lbTest.SelectedValue == null)
{
txtTest.Text = "NULL";
}
else
{
txtTest.Text = lbTest.SelectedValue.ToString();
}
}
}

This simply puts the value in a textbox as I click items in the
listbox.

If you run a Visual Studio solution that does this, you'll see the
bug. Clicking Display2 will show Value1, for example.

FYI. I could care less if it gets fixed. I found no good way to
submit a Microsoft bug (about like this person's experience:
http://weblog.timaltman.com/archive/2006/03/22/reporting-bugs-microsoft
)

There you go! Sure would be great to see an easier place to turn in
Microsoft bugs without charging your client base.
Herfried K. Wagner [MVP]
2010-05-30 00:24:51 UTC
Permalink
Post by Jeff
FYI. I could care less if it gets fixed. I found no good way to
http://weblog.timaltman.com/archive/2006/03/22/reporting-bugs-microsoft
)
You are looking for Microsoft Connect.

<URL:http://connect.microsoft.com/>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Loading...