 |
 |

06-13-2005, 05:32 PM
|
|
Freshman
|
|
Join Date: Oct 2004
Posts: 42
|
|
Listbox - Moving Items Around
|
There's something I need to and I just aint got a clue.
I'm adding stuff to a listbox, namely a filename, which via a varible and array containing a structure is working fine.
But what I need to do now is to be able to select an item in the listbox and move it up or down, so it runs in a different order...... also remove an item.
Please help
|
|

06-13-2005, 06:01 PM
|
|
Ultimate Contributor
|
|
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
|
|
Here is an example on how to move an item up the list,
Code:
Dim index As Integer
index = ListBox1.SelectedIndex
If index > 0 Then 'make sure someting is selected and you cant move the top item up
Dim item As String
item = ListBox1.Items(index)
ListBox1.Items.RemoveAt(index)
ListBox1.Items.Insert(index - 1, item)
ListBox1.SelectedIndex = index - 1
End If
to remove an item, you can either use ListBox1.Items.Remove (), which will remove all the items in the list that have the same value as the parameter you pass in, or you can use ListBox1.Items.RemoveAt (), which will remove just the specific item at the given index number.
What my code does it gets the selected item, removes it from the list, then re-adds it one position higher, then reselects the item. This shouldnt be hard to modify to move the item down the list, it only means adding one to the index number as opposed to subtracting one.
|
|

06-13-2005, 10:18 PM
|
|
Freshman
|
|
Join Date: Oct 2004
Posts: 42
|
|
|
Thank you very much, that worked great.
|
|

06-14-2005, 08:18 PM
|
|
Freshman
|
|
Join Date: Oct 2004
Posts: 42
|
|
Thanks again for your answer. As with moving the listbox items up and down the list I also have to move the contents of an array (not arraylist) to coincide with the listbox. My array contains a 'structure' and when I move an item up the listbox this code is also called (which works fine):
Code:
Dim InfoOnTop As CURRENT_FILE = MYARRAY(Index - 1)
MYARRAY(Index - 1) = MYARRAY(Index)
MYARRAY(Index) = InfoOnTop
I also have one for move down, all is fine.
The problem I have now is I have to find a way to remove an item. Removing it from the listbox is fine, but I now have to remove from the array at the same time so and array like this:
1:1
2:2
3:3
4:4
removing 3 would produce an array of:
1:1
2:2
4:4
Thanks
|
|

06-14-2005, 08:40 PM
|
|
Ultimate Contributor
|
|
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
|
|
|
One idea that comes to mind (if you dont want to switch to an array list, but rather stay with an array), is to redim to array to the new size and then refil it.
you could redim the array, then fill it up with the values from the list box.
|
|

06-15-2005, 01:29 AM
|
|
Freshman
|
|
Join Date: Oct 2004
Posts: 42
|
|
|
I tried changing to an arraylist but dont know how to do things like: Public FILE(16) As CURRENT_FILE
current file is a structure
Let me just expand on that....
Lets say the CURRENT_FILE structure contains:
FILENAME As String
FILESIZE As Integer
and in my code I would have things like:
TextBox1.Text = FILE(I).FILENAME
and such like, well if I change over to ArrayList, which I'd like, how do I amend the 'normal' array stuff so I can access my data like a normal array?
What do I need to change: 'Public FILE(16) As CURRENT_FILE' to?
Thanks For Any Help
|
Last edited by JustAdotNETUser; 06-15-2005 at 02:47 AM.
|

06-15-2005, 10:12 AM
|
|
Ultimate Contributor
|
|
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
|
|
So your question is, how would you add and access your data once it is put into an array list?
It is very similar to accessing it from an array, you just need to know the items index number that you want to access, for example, lets say you have an array list called "array_list", that has one item in it, and that item is a CurrentFile file,
Code:
Private Structure CurrentFile 'defines the structure
Dim FileName As String
Dim FileSize As String
End Structure
Code:
'here, you declare the arry list and the file, then add the file to the array list
Dim array_list As New ArrayList
Dim file As CurrentFile
file.FileName = "test Name"
file.FileSize = "Size"
array_list.Add(file)
now, to access the file you just put into the list, you can use array_list(0), if you want to know its FileName, array_list(0).FileName, or to know its FileSize, array_list(0).FileName
before you were declaring an array of CurrentFiles, now you only need one at a time to add to the array list.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|