By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

Listbox selected items

P: n/a
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?
Aug 18 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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();
}
}
}
Aug 18 '06 #2

P: n/a
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?
"Mitch" <Mi***@discussions.microsoft.comwrote in message
news:45**********************************@microsof t.com...
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?

Aug 18 '06 #3

P: n/a
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();
}
}
}
Aug 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.