PC Review


Reply
Thread Tools Rating: Thread Rating: 5 votes, 1.80 average.

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

 
 
Ron
Guest
Posts: n/a
 
      8th Dec 2004
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
 
Reply With Quote
 
 
 
 
Lowell Heddings
Guest
Posts: n/a
 
      8th Dec 2004
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

 
Reply With Quote
 
 
 
 
Ron
Guest
Posts: n/a
 
      9th Dec 2004
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

>.
>

 
Reply With Quote
 
Jo Inferis
Guest
Posts: n/a
 
      9th Dec 2004
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


 
Reply With Quote
 
Lowell Heddings
Guest
Posts: n/a
 
      10th Dec 2004
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

>>
>>.
>>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not Selecting Field names When Selecting Some Data mcl Microsoft Access Queries 0 8th May 2008 12:57 AM
Selecting only from index for combobox =?Utf-8?B?U3VwcGVyRHVjaw==?= Microsoft Excel Programming 2 6th Jul 2007 08:36 PM
Re: Selecting whole word while selecting Charles Kenyon Microsoft Word Document Management 0 13th Apr 2005 08:14 PM
Excel dragging/selecting and not de-selecting. Mike D Microsoft Excel Crashes 5 21st Oct 2004 08:53 PM
Selecting a cell from an index Canuck Microsoft Excel Programming 1 16th May 2004 09:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:53 AM.