Solved

Urgent Help required

Posted on 2003-11-06
11
243 Views
Last Modified: 2010-04-16
I need to know how i can save all of the entries in a listbox1 into seperate variables e.g entry1,entry2,entry3

I would be really greatful if someone could give me the code for this
0
Comment
Question by:zzgg5kmy
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

by:purpleblob
ID: 9695101
Can you elaborate ? I mean to get the items within a ListBox you simply interact with the listbox1.Items collection

So to interact with items (when you're storing strings in the list box) you simply do something like

foreach(string i in listBox1.Items)
{
   MessageBox.Show(i);
}

Obviously you could create an Array or ArrayList (or other collection and within the foreach assign i to your new collection.

But in terms of storing to specific variables, such as entry1, entry2 etc. you'd need to know in advance how large you listbox items collection is and thus declare enough entry variables. The simply do the following

entry1 = (string)listBox1.Items[0];
entry2 = (string)listBox1.Items[1];
etc.

Again assuming you're storing strings in the listbox
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9695132
Can you elaborate ? I mean to get the items within a ListBox you simply interact with the listbox1.Items collection

So to interact with items (when you're storing strings in the list box) you simply do something like

foreach(string i in listBox1.Items)
{
   MessageBox.Show(i);
}

Obviously you could create an Array or ArrayList (or other collection) and within the foreach assign i to your new collection.

But in terms of storing to specific variables, such as entry1, entry2 etc. you'd need to know in advance how large you listbox items collection is and thus declare enough entry variables. The simply do the following

entry1 = (string)listBox1.Items[0];
entry2 = (string)listBox1.Items[1];
etc.

Again assuming you're storing strings in the listbox
0
 
LVL 4

Assisted Solution

by:Tonylmiller
Tonylmiller earned 155 total points
ID: 9695141
This will print a list to the console of up to 6 items.  You can substitute individual variables if you wish.

                  string[] listboxItems = new string[6];

                  for (int i = 0; i < listBox1.Items.Count; i++)
                  {
                        listboxItems[i] = listBox1.Items[i].ToString();
                  }
                  for (int i = 1; i <= listBox1.Items.Count; i++)
                  {
                        Console.WriteLine("Item #" + i.ToString() + " = " + listboxItems[i-1]);
                  }
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 6

Expert Comment

by:purpleblob
ID: 9695146
Can you elaborate ? I mean to get the items within a ListBox you simply interact with the listbox1.Items collection

So to interact with items (when you're storing strings in the list box) you simply do something like

foreach(string i in listBox1.Items)
{
   MessageBox.Show(i);
}

Obviously you could create an Array or ArrayList (or other collection) and within the foreach assign i to your new collection.

But in terms of storing to specific variables, such as entry1, entry2 etc. you'd need to know in advance how large you listbox items collection is and thus declare enough entry variables. The simply do the following

entry1 = (string)listBox1.Items[0];
entry2 = (string)listBox1.Items[1];
etc.

Again assuming you're storing strings in the listbox
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9695153
Sorry about the second post of the same answer experts-exchange.com seemed to a have died on me, but obviously it did get through. :-)
0
 

Author Comment

by:zzgg5kmy
ID: 9695180
Ok to clear thing up, i have a listbox in one class file i need to pass them across to another class, i think in an array would be best as i need to some how to something like this


for loop
i =0
filename = listboxarray [i]

if regex match file1, within the contents of filename

          {
              // Do stuff
          }

else
          {
              i++
          }


Basically i need all of the values from the listbox into an array so everytime the for loop happens it uses the next filename that was in the listbox, therefore it is checking each file for the contents of file1, if it is successful it does the stuff i need, if not it increments i and trys the next filename
0
 

Author Comment

by:zzgg5kmy
ID: 9695194
Also i need to know how i would get the value from the array.

i.e

file = array[0]

i dont know if that is right what i am trying to say if file = the text at position 0, could i put a varibale in there like (i) so that on the second loop it would assign the value at position 1 to file and so on
0
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9695249
My example shows how to put them in and get them out.

Good luck!

Tony

By the way, you might want to use more descriptive subjects.  "How to Get Items List from a ListBox" would be much better than "Urgent Help required".
0
 
LVL 6

Accepted Solution

by:
purpleblob earned 155 total points
ID: 9695255
Well firstly to copy all elements out of the listBox1.Items use CopyTo

Thus

object[] files = new object[listBox1.Items.Count];
listBox1.Items.CopyTo(files, 0);

Shows the CopyTo used and then to get at an element in the array simply use

// assigns the element from array files to a string
string file = (string)files[0];

Is this what you're after ?

0
 

Author Comment

by:zzgg5kmy
ID: 9695340
How do i pass these across to another another class file.

I need to do this in one class file

object[] files = new object[listBox1.Items.Count];
listBox1.Items.CopyTo(files, 0);


and this in another

string file = (string)files[0];
0
 

Author Comment

by:zzgg5kmy
ID: 9695497
I have done it i have used a bit from each of you code, so i will award both of you 155 points each
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to reference objects of the calling class without passing them as parameters 29 176
Error in script 11 56
C# Single Form 8 42
What namespace do I need to import? 2 28
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question