Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Listbox - Moving Items Around


Reply
 
Thread Tools Display Modes
  #1  
Old 06-13-2005, 05:32 PM
JustAdotNETUser JustAdotNETUser is offline
Freshman
 
Join Date: Oct 2004
Posts: 42
Default 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
Reply With Quote
  #2  
Old 06-13-2005, 06:01 PM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

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.
Reply With Quote
  #3  
Old 06-13-2005, 10:18 PM
JustAdotNETUser JustAdotNETUser is offline
Freshman
 
Join Date: Oct 2004
Posts: 42
Default

Thank you very much, that worked great.
Reply With Quote
  #4  
Old 06-14-2005, 08:18 PM
JustAdotNETUser JustAdotNETUser is offline
Freshman
 
Join Date: Oct 2004
Posts: 42
Default

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
Reply With Quote
  #5  
Old 06-14-2005, 08:40 PM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

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.
Reply With Quote
  #6  
Old 06-15-2005, 01:29 AM
JustAdotNETUser JustAdotNETUser is offline
Freshman
 
Join Date: Oct 2004
Posts: 42
Default

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.
Reply With Quote
  #7  
Old 06-15-2005, 10:12 AM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->