471,066 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

ListBox not returning correct SelectedIndex

I'm going crazy with this. I just added two Listboxes (lbxSource->multiple
selection and lbxDestination -> single selection).
Then:
a) button "Load":
ListItem l0 = new ListItem("01");
lbxDestination.Items.Add(l0); //1st goes to Destination Box
ListItem l1 = new ListItem("02");
lbxSource.Items.Add(l1);
ListItem l2 = new ListItem("03");
lbxSource.Items.Add(l2);
ListItem l3 = new ListItem("04");
lbxSource.Items.Add(l3);
b) button "Insert" (to move an item from Source to Destination):
foreach (ListItem li in lbxSource.Items)
{
if (li.Selected)
{
lbxDestination.Items.Add(li);
lbxDestination.SelectedItem.Value = "01";
li.Selected = false; //otherwise I got a runtime error 'multiple not
allowed in s
}
}
c) a "Show" button that shows the context of the Selected Destination Item:
tbxText.Text = lbxDestination.SelectedItem.Text;

And it's unbelievable, but tbxText.Text always shows "01", no matter what I
selected.
I had this in a real world application and since I couldn't believe it I
wrote a small test-page and it behaves exactly the same.
Is there a workaround to this ? Or am I doing something wrong ?

Nov 19 '05 #1
1 2538
your step (b) is your problem
First off, chances are you want to create new instance of ListItems instead
of simply copying the reference...so instead of

add(li) do add(new ListItem(li.Text));

secondly, when you do SelectedItem.Value = "01" you are telling it to take
the selected item and putting it's value to 01....since you are copying
references (as above) each new item IS selected (since it was selected in
the previous checkbox) and you are overwriting its value to "01" hence no
matter which you select, they all have the same value...

your insert should look something like:

foreach (ListItem li in lbxSource.Items) {
if (li.Selected) {
lbxDestination.Items.Add(new ListItem(li.Text));
li.Selected = false;
}
}
lbxDestination.SelectedIndex = 0;
karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Carlo Marchesoni" <Ca*************@discussions.microsoft.com> wrote in
message news:34**********************************@microsof t.com...
I'm going crazy with this. I just added two Listboxes (lbxSource->multiple
selection and lbxDestination -> single selection).
Then:
a) button "Load":
ListItem l0 = new ListItem("01");
lbxDestination.Items.Add(l0); //1st goes to Destination Box
ListItem l1 = new ListItem("02");
lbxSource.Items.Add(l1);
ListItem l2 = new ListItem("03");
lbxSource.Items.Add(l2);
ListItem l3 = new ListItem("04");
lbxSource.Items.Add(l3);
b) button "Insert" (to move an item from Source to Destination):
foreach (ListItem li in lbxSource.Items)
{
if (li.Selected)
{
lbxDestination.Items.Add(li);
lbxDestination.SelectedItem.Value = "01";
li.Selected = false; //otherwise I got a runtime error 'multiple not allowed in s
}
}
c) a "Show" button that shows the context of the Selected Destination Item: tbxText.Text = lbxDestination.SelectedItem.Text;

And it's unbelievable, but tbxText.Text always shows "01", no matter what I selected.
I had this in a real world application and since I couldn't believe it I
wrote a small test-page and it behaves exactly the same.
Is there a workaround to this ? Or am I doing something wrong ?

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by James Goodman | last post: by
1 post views Thread by don | last post: by
3 posts views Thread by John S | last post: by
1 post views Thread by Josema | last post: by
7 posts views Thread by Dave | last post: by
7 posts views Thread by Lit | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.