Listbox selected items

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is the following a correct representation of the relationship of the selected
index to selected item:
INDEX ITEM
0 "item 1"
1 "item 2"
2 "item 3"
and so on.
I keep getting the same string for selected index 0 and 1; i.e. both are
"item 1".
I'm new to C# and not exactly an accomplished programmer. Any suggestions?
 
Do you have a short sample that illustrates this? The following works fine:

using System;
using System.Windows.Forms;
class Program {
static void Main() {
using(Form f = new Form())
using (ListBox lb = new ListBox()) {
lb.Items.AddRange(new object[] { "Item1", "Item2", "Item3" });
lb.SelectedIndexChanged += delegate {
f.Text = string.Format("{0}: {1}", lb.SelectedIndex,
lb.SelectedItem);
};
f.Controls.Add(lb);
f.ShowDialog();
}
}
}
 
No, it is not a correct representation of the relationship of the selected
index to the item. I'll tell you why.

A ListBox has an Items Collection, which is a Collection of objects. The
SelectedIndex of the ListBox is the index in the Collection of the Selected
Item. This has no correspondence whatsoever with the values viewed in the
ListBox, other than the fact that each value viewed is a ListItem, and
naturally that they are in the order of their indices. For example, consider
the following:

INDEX ITEM
0 "item 3"
1 "item 1"
2 "item 2"

Notice that the item with index 0 is named "item 3". They will appear in the
ListBox in the order on the left, but will display the values in the order
on the right.

Now, here's another example:

INDEX ITEM
0 "item 3"
1 "item 3"
2 "item 3"

Each of these is a separate item, but they all have the same text. There is
nothing to prevent you from putting multiple items with the same displayed
value in a ListBox.

So, what I'm getting at is, you have not established that the string you see
is the same string, or whether both values are the same. In addition, you
haven't specified how you "keep getting the same thing." That is a very
human and inexact way to describe your experience. It would be more helpful
to say "when I click the listbox, I see this in the first item, that in the
second..." and so on. Or whatever it is that you did, and what you
observed. In other words, describe your experiment, what you did, what you
saw, when and where you saw it, in detail. Then we can help you out more.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
And for multi-select:

using System;
using System.Windows.Forms;
class Program {
static void Main() {
using(Form f = new Form())
using (ListBox lb = new ListBox()) {
lb.SelectionMode = SelectionMode.MultiExtended;
lb.Items.AddRange(new object[] { "Item1", "Item2", "Item3" });
lb.SelectedIndexChanged += delegate {
string text = ""; // low throughput, we'll use concatenation
for now...
foreach (int index in lb.SelectedIndices) {
text+=string.Format("{0}: {1};", index,
lb.Items[index]);
}
f.Text = text;
};
f.Controls.Add(lb);
f.ShowDialog();
}
}
}
 
Back
Top