473,403 Members | 2,366 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,403 software developers and data experts.

Remove from ListBox

How can I remove some selected items from the listBox in the run time ??

Nov 16 '05 #1
4 18268
Hi mostafa atalla,

ListBox.SelectedObjectCollection s = listBox1.SelectedItems;

while(s.Count > 0)
listBox1.Items.Remove(s[0]);

The SelectedObjectCollection is directly tied to the listbox items and when you remove an item, the item is also gone from the collection.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2


"Morten Wennevik" wrote:
Hi mostafa atalla,

ListBox.SelectedObjectCollection s = listBox1.SelectedItems;

while(s.Count > 0)
listBox1.Items.Remove(s[0]);

The SelectedObjectCollection is directly tied to the listbox items and when you remove an item, the item is also gone from the collection.

--
Happy coding!
Morten Wennevik [C# MVP]

Dear sir :
when I wrote the previous code and run , the following exception appeared :
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Cannot modify the Items collection when the DataSource property is set.

the DataSource property must be set to the database,what can i do to solve this problem??
Nov 16 '05 #3
When you use a datasource, you need to remove the items from the source. The ListBox will update itself when the source changes.

If you only have one selected item you can remove it like this

DataRowView drw = (DataRowView)listBox1.SelectedItem;
dataSet1.Tables[0].Rows.Remove(drw.Row);

However, I couldn't manage to get the previous while loop to work with the DataSet as it ended up removing all rows, selected or not, or it only removed the first row.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
You need to save the selected rows before you start removing them.

ListBox.SelectedObjectCollection s = listBox1.SelectedItems;

ArrayList list = new ArrayList();

foreach(object o in s) // store the rows outside selectedobjectcollection
list.Add(o);

foreach(DataRowView drw in list)
dataSet1.Tables[0].Rows.Remove(drw.Row);

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Supra | last post by:
in vb6 listbox1.remove item 5 how will i do in vb.net? regards
4
by: Bilo | last post by:
I dont know what i am doing false. the code : private void button2_Click(object sender, System.EventArgs e) { foreach (string filename in listBox1.SelectedItems)...
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: Hrcko | last post by:
How to remove a selected item from listBox? Hrcko
4
by: Ron | last post by:
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...
1
by: deepak | last post by:
hi , i have 2 listbox and 2 buttons like add and remove, both listbox have multiple selection true so that i can either add or remove multiple selected items. the first listbox is populated with...
3
by: Bill Nguyen | last post by:
I use the following example (from another post) and it seemed to work fine. However, when I add the syntax to remove the selected item from the senderbox, I got error....
7
by: Mike Johnson | last post by:
How do I remove a item that's been selected from a listview? I'm using Visual Basic 2005 express edition. Thanks
11
by: Andrus | last post by:
1. Run code 2. Resize window Observed: Listbox flashes during resize. How to remove this flashing ? Andrus.
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.