473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Listbox, array, item deletion

Hi

I have an string[] array containing file paths. I have added them to a
ListBox with AddRange();

When I delete an item from the ListBox, how can I delete the same item
from the original array?

Thanks
Nov 3 '08 #1
3 3454
It's not possible. All you can do is to create an new array(leaving the
deleted string) and assign it to the same old array variable/field.

Sweetiecakes wrote:
Hi

I have an string[] array containing file paths. I have added them to a
ListBox with AddRange();

When I delete an item from the ListBox, how can I delete the same item
from the original array?

Thanks
Nov 3 '08 #2
"Sweetiecakes" <x@x.comwrote in message
news:49***********************@news.fv.fi...
I have an string[] array containing file paths. I have added them to a
ListBox with AddRange();

When I delete an item from the ListBox, how can I delete the same item
from the original array?
Like Ashutosh said, there's no way to delete an arbitrary element from an
array. Next time consider using a List<stringinstead and it will handle
removing items for you.
Nov 3 '08 #3
On Mon, 03 Nov 2008 10:07:37 -0800, Sweetiecakes <x@x.comwrote:
Hi

I have an string[] array containing file paths. I have added them to a
ListBox with AddRange();

When I delete an item from the ListBox, how can I delete the same item
from the original array?
To match the ListBox semantics, it would be better to store your objects
in a List<Trather than an array. Then, when you call List<T>.Remove()
to remove a specific object, it will automatically find the first matching
object in the List<T>, remove it without leaving a gap in the collection.

Even better is that, as long as you _always_ add or remove elements from
the List<Tin the same way as they are added or removed to the ListBox,
you can just use the item index from the ListBox as the index in the
List<Tand then you can call List<T>.RemoveAt() instead of making the
collection look for the object each time.

You can implement the same basic functionality using an array, but you'll
either have to use IndexOf() to look for the element, or you'll have to
shift down all the elements after the one you're removing so that the
indices continue to match the ListBox indices.

Pete
Nov 3 '08 #4

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

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
6
by: Deano | last post by:
I needed to have a listbox populated by locations which are stored in tblLocations. However I wanted an "All locations" entry to be at the top of the listbox. This is not in the tblLocations. The...
7
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if...
0
by: The Zakman | last post by:
Please help me with a listbox and postback issue that has me baffled. I am storing my database connection strings in my web.config. At page_load, the db strings that are currently stored in the...
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
5
by: John Veldthuis | last post by:
My code works perfectly 100% when adding items to my ArrayList and updating the listbox. Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is the last...
6
by: segue | last post by:
Hi; I need to change the order of a listbox array from a form app where I select order up or down. What's a good way to do that? Regards; Segue
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.