Thursday, March 29, 2012

Pass Listitem between Listboxes

I have two listboxes. The first listbox is prepopulated with the selections
that the user has. The "Value" of the items is NOT the same as the "Text".
The user can select an item (or items) from one listbox and move it to the
other listbox. The code for it is:
Dim x, deleted As Integer
For x = 0 To lboxFrom.Items.Count - 1
If lboxFrom.Items.Item(x - deleted).Selected Then
lboxTo.Items.Add(lboxFrom.Items.Item(x - deleted))
lboxFrom.Items.Remove(lboxFrom.Items.Item(x - deleted))
deleted += 1
End If
Next
I have also tried:
Dim x, deleted As Integer
For x = 0 To lboxFrom.Items.Count - 1
If lboxFrom.Items.Item(x - deleted).Selected Then
Dim newItem As New ListItem()
newItem.Text = lboxFrom.Items.Item(x - deleted).Text
newItem.Value = lboxFrom.Items.Item(x - deleted).Value
lboxTo.Items.Add(newItem)
lboxFrom.Items.Remove(lboxFrom.Items.Item(x - deleted))
deleted += 1
End If
Next
The item that passes to the second listbox should have value of the first
listbox item and also the text of the first listbox item. My problem is tha
t
the value and the text of the item placed in the second listbox end up the
same and equal the text of the item in the first listbox. Why is it doing
this and what can I do to get the value for the item from the first listbox
to pass as the value for the item in the second listbox?
Thanks.Hi,
Your code seems fine. Try debugging it, specifically the second way and
place a breakpoint on

> newItem.Text = lboxFrom.Items.Item(x - deleted).Text
then step into until you see what are the runtime values of
lboxFrom.Items.Item(x - deleted).Text and lboxFrom.Items.Item(x -
deleted).Value
I tested (in C# though) with the following handler of the button click:
protected void b1_Click(object s, EventArgs e)
{
System.Web.UI.WebControls.ListItem li;
int i = 0;
while(i < lboxFrom.Items.Count)
{
if(lboxFrom.Items[i].Selected)
{
li = new System.Web.UI.WebControls.ListItem(
lboxFrom.Items[i].Text, lboxFrom.Items[i].Value);
lboxTo.Items.Add(li);
lboxFrom.Items.RemoveAt(i);
continue;
}
i++;
}
}
and the new items in the "To" ListBox have the text *and* the value of the
ones removed from the "From" ListBox
Hope this helps
Martin
"ScoutLee" <ScoutLee@.discussions.microsoft.com> wrote in message
news:D6DA1B2E-1030-4618-94AE-E43069303EDB@.microsoft.com...
> I have two listboxes. The first listbox is prepopulated with the
selections
> that the user has. The "Value" of the items is NOT the same as the
"Text".
> The user can select an item (or items) from one listbox and move it to the
> other listbox. The code for it is:
> Dim x, deleted As Integer
> For x = 0 To lboxFrom.Items.Count - 1
> If lboxFrom.Items.Item(x - deleted).Selected Then
> lboxTo.Items.Add(lboxFrom.Items.Item(x - deleted))
> lboxFrom.Items.Remove(lboxFrom.Items.Item(x - deleted))
> deleted += 1
> End If
> Next
> I have also tried:
> Dim x, deleted As Integer
> For x = 0 To lboxFrom.Items.Count - 1
> If lboxFrom.Items.Item(x - deleted).Selected Then
> Dim newItem As New ListItem()
> newItem.Text = lboxFrom.Items.Item(x - deleted).Text
> newItem.Value = lboxFrom.Items.Item(x - deleted).Value
> lboxTo.Items.Add(newItem)
> lboxFrom.Items.Remove(lboxFrom.Items.Item(x - deleted))
> deleted += 1
> End If
> Next
> The item that passes to the second listbox should have value of the first
> listbox item and also the text of the first listbox item. My problem is
that
> the value and the text of the item placed in the second listbox end up the
> same and equal the text of the item in the first listbox. Why is it doing
> this and what can I do to get the value for the item from the first
listbox
> to pass as the value for the item in the second listbox?
> Thanks.
>

0 comments:

Post a Comment