zzgg5kmy
asked on
Urgent Help required
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
I would be really greatful if someone could give me the code for this
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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. :-)
ASKER
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
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
ASKER
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
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
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".
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".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Coun t];
listBox1.Items.CopyTo(file s, 0);
and this in another
string file = (string)files[0];
I need to do this in one class file
object[] files = new object[listBox1.Items.Coun
listBox1.Items.CopyTo(file
and this in another
string file = (string)files[0];
ASKER
I have done it i have used a bit from each of you code, so i will award both of you 155 points each
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