473,507 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add all values from listbox to a arraylist?

i cant seem to find this anywhere online, i am new to programming, started 2 weeks ago, but i cant seem to get this function totally figured out.

can someone just give me a base to work off? will i need to use for loops?

or is there a simpler method?

thanks in advance
Oct 18 '10 #1
1 10504
Curtis Rutland
3,256 Recognized Expert Specialist
First, I wouldn't suggest using an ArrayList, unless you are stuck on .NET Framework 1.1. System.Collections.Generic.List<T> is a much better, much more convenient construct to work with.

Now, it's actually pretty simple to just get a List of the items. If we're going to use a List, you have to be sure to cast properly. I'm assuming you're using Strings, but you could be using any object, so make sure to cast it to the proper type where I cast to string here:

Expand|Select|Wrap|Line Numbers
  1. List<string> list = new List<string>();
  2. foreach(object item in listBox.Items)
  3.   list.Add(item as string);
Just replace string with the type of object you added originally to the ListBox.

Alternatively, you can use LINQ:
Expand|Select|Wrap|Line Numbers
  1. List<string> list = listBox.Items.Select(item => item as string).ToList();
Oct 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1720
by: Sonny Sablan | last post by:
AryUnderNav = New ArrayList() AryUnderNav.Add("Under $50") AryUnderNav.Add("$50 - $100") AryUnderNav.Add("$100 - $200") AryUnderNav.Add("$200 +") UnderNav.DataSource = AryUnderNav ...
13
2292
by: Larry Woods | last post by:
I am creating a "from-to" set of listboxes where the "left" listbox had a list of values and I want to be able to select these values, 1 at a time, and move them into a "right" listbox, removing...
0
1675
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
2
1265
by: segue | last post by:
Another question. I'm using a checkbox list (appname) that has fields associated with each checkbox list appname. You click a button and I instantiate this newform where you can choose the order...
7
2086
by: Amit | last post by:
Hi everybody,I have 2 dropdowns and on selected index change the second dropdown is populated accordingly and so on.i am displaying the selected value in a label for the user so that he comes to...
6
5243
by: psuaudi | last post by:
I am trying to run through a tutorial on recordsets, so I am following the tutorial here: http://www.devdos.com/vb/lesson4.shtml I have made the table as described, and tried to run this...
1
3508
by: harikap | last post by:
hi, im using vb.net 2005 how can i add values to arraylist & how can i retreive those values into one variable or windowsdatagrid view
2
12514
GTXY20
by: GTXY20 | last post by:
Hi all, I have an ArrayList1 of multiple values. Here is an exaple of the a potential ArrayList1: a,a,b,,c,a,b,c,c,c,a,b Does anyone have any suggestion whereby I could output the count for...
2
11639
by: macky caliston | last post by:
Hi, How can I convert hashtable.Values to an ArrayList? Thanks a Lot, Macky
1
2998
by: cowboyrocks2009 | last post by:
Hi. I want to return values from multiple Arraylist. How can I do that ? later I want to use these values in another class. Can somebody help class myClass{ public ArrayList<Rectangle>...
0
7223
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
7111
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
7319
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
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7031
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...
1
5042
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
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1542
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 ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.