471,579 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

ASP.NET/C# - Listbox.Items.Remove only selecting index 0

Ron
I've got a listbox that holds a list of groups. Users can
select a group, hit the remove button and the group
should be removed from the listbox. The only problem is
that no matter which group you select, the first one in
the listbox is always removed.(The listitem with an index
of 0. Box is set to single selection mode) I've looked at
multiple examples and they all do it this way. What's
wrong? (variables are also being set to the values of the
first item)
private void RemoveGroupButton_Click(object sender,
System.EventArgs e)
{
string groupName;
string toolID;

groupName = GroupsListBox.SelectedItem.ToString();
toolID = GroupsListBox.SelectedValue;

//Remove from the listbox
//************************************************
**********************
GroupsListBox.Items.Remove
(GroupsListBox.SelectedItem);
//************************************************
**********************

}//End Button
Nov 19 '05 #1
4 11352
Have you tried something like this:

GroupsListBox.Items.RemoveAt(GroupsListBox.Selecte dIndex);

Or even as a test:

GroupsListBox.Items.RemoveAt(2);

Also, you should be checking to make sure that an item is actually
selected before allowing the user to remove items. Otherwise the system
will throw an exception when there are no more items left and the user
clicks the remove button.

Lowell

Ron wrote:
I've got a listbox that holds a list of groups. Users can
select a group, hit the remove button and the group
should be removed from the listbox. The only problem is
that no matter which group you select, the first one in
the listbox is always removed.(The listitem with an index
of 0. Box is set to single selection mode) I've looked at
multiple examples and they all do it this way. What's
wrong? (variables are also being set to the values of the
first item)
private void RemoveGroupButton_Click(object sender,
System.EventArgs e)
{
string groupName;
string toolID;

groupName = GroupsListBox.SelectedItem.ToString();
toolID = GroupsListBox.SelectedValue;

//Remove from the listbox
//************************************************
**********************
GroupsListBox.Items.Remove
(GroupsListBox.SelectedItem);
//************************************************
**********************

}//End Button

Nov 19 '05 #2
Ron
I've already tried what you suggest, and when I do that,
when I have any item selected, it's returning an index of
0. If I don't select anything and hit the remove, it'll
generate an exception.

You suggested the GroupsListBox.Items.RemoveAt(2) code,
and this works provided there is something with an index
of 2. The whole problem I'm having is that I'm always
being returned an index of 0, not matter which item is
picked.
-----Original Message-----
Have you tried something like this:

GroupsListBox.Items.RemoveAt (GroupsListBox.SelectedIndex);
Or even as a test:

GroupsListBox.Items.RemoveAt(2);

Also, you should be checking to make sure that an item is actuallyselected before allowing the user to remove items. Otherwise the systemwill throw an exception when there are no more items left and the userclicks the remove button.

Lowell

Ron wrote:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group
should be removed from the listbox. The only problem is that no matter which group you select, the first one in the listbox is always removed.(The listitem with an index of 0. Box is set to single selection mode) I've looked at multiple examples and they all do it this way. What's
wrong? (variables are also being set to the values of the first item)
private void RemoveGroupButton_Click(object sender,
System.EventArgs e)
{
string groupName;
string toolID;

groupName = GroupsListBox.SelectedItem.ToString();
toolID = GroupsListBox.SelectedValue;

//Remove from the listbox
//********************************************* *** **********************
GroupsListBox.Items.Remove
(GroupsListBox.SelectedItem);
//********************************************* *** **********************

}//End Button

.

Nov 19 '05 #3
Ron wrote:
I've got a listbox that holds a list of groups. Users can
select a group, hit the remove button and the group
should be removed from the listbox.

I've got some code that does this kind of thing if it helps (in my case
there are two listboxes and you can move the items between the listboxes
using "add" and "remove" buttons, but there may be something here you can
use)

<code snippet>
protected void btnRemoveItems_Click(Object sender, ImageClickEventArgs e)
{
//ADD TO AVAILABLE ITEMS
ListItemCollection toRemove = new ListItemCollection();
foreach (ListItem item in lstAvailable.Items)
{
if (item.Selected)
{
lstAvailable.Items.Add(new ListItem(item.Text, item.Value));
toRemove.Add(item);
}
}

//REMOVE FROM SELECTED
foreach (ListItem item in toRemove)
{
lstSelected.Items.Remove(item);
}
}
</code snippet>

Obviously there's another function which adds the items to the "selected
items" listbox but it just does the same as this function with the listbox
names swapped (which reminds me, I could probably turn this into one
function that takes two listboxes as parameters...hmm..)

Anyway...this kind of approach may help.

There is also the possibility that you're binding the contents of the
listbox before the click event is being fired (if you're doing this in
Page_Load and not checking IsPostBack, then this is almost certainly your
problem).

HTH

--
jo inferis
Nov 19 '05 #4
I just noticed that this was an ASP.NET listbox and not Windows Forms...
doh!

Are you rebinding the listbox on the page postback? You need to make
sure that on the postback you aren't rebinding the listbox, and also
that you have the viewstate enabled for the control(Which is how .NET
keeps the list of items even when you don't rebind it.)

I suspect that is your problem.

Lowell

Ron wrote:
I've already tried what you suggest, and when I do that,
when I have any item selected, it's returning an index of
0. If I don't select anything and hit the remove, it'll
generate an exception.

You suggested the GroupsListBox.Items.RemoveAt(2) code,
and this works provided there is something with an index
of 2. The whole problem I'm having is that I'm always
being returned an index of 0, not matter which item is
picked.

-----Original Message-----
Have you tried something like this:

GroupsListBox.Items.RemoveAt


(GroupsListBox.SelectedIndex);
Or even as a test:

GroupsListBox.Items.RemoveAt(2);

Also, you should be checking to make sure that an item


is actually
selected before allowing the user to remove items.


Otherwise the system
will throw an exception when there are no more items


left and the user
clicks the remove button.

Lowell

Ron wrote:
I've got a listbox that holds a list of groups. Users
can
select a group, hit the remove button and the group
should be removed from the listbox. The only problem
is
that no matter which group you select, the first one
in
the listbox is always removed.(The listitem with an
index
of 0. Box is set to single selection mode) I've looked
at
multiple examples and they all do it this way. What's
wrong? (variables are also being set to the values of
the
first item)
private void RemoveGroupButton_Click(object sender,
System.EventArgs e)
{
string groupName;
string toolID;

groupName = GroupsListBox.SelectedItem.ToString();
toolID = GroupsListBox.SelectedValue;

//Remove from the listbox
//*********************************************
***
**********************
GroupsListBox.Items.Remove
(GroupsListBox.SelectedItem);
//*********************************************
***
**********************

}//End Button


.

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Luca | last post: by
1 post views Thread by mg | last post: by
8 posts views Thread by tshad | last post: by
3 posts views Thread by thomasp | last post: by
7 posts views Thread by Lit | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | 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.