473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 11467
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luca | last post by:
Hello, I'm using a windows form in which there is a standard ListBox control. I want to add/remove objects from the ArrayList associated to the ListBox, and I want the ListBox immediately shows...
1
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The...
3
by: Alpha | last post by:
I have 3 radio buttons for include, exclued or 'select all' from the listbox items. If a user selects the 'Select All' button' then all items in listbox is hi-lited as selected. Now, when user...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
1
by: mg | last post by:
How can I remove (using C#) the items previously added to a System.Web.UI.WebControls.ListBox
8
by: tshad | last post by:
I have a string that I read from my database: 1|8|5620|541 These are all values in my ListBox. I want to select each of these items (4 of them - but could be many more). At the moment I am...
3
by: thomasp | last post by:
Has anyone got some sample code to do drag and drop from one listbox to another listbox using VB.Net 2005. The below code works for draging and droping one at a time, but not for multiselected...
6
by: rahulnag22 | last post by:
Hi, Is it possible to have different items in a listbox in different colors? Or is it just one color for all items in a listbox? Thanks Rahul
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.