|
-
Oct 24th, 2003, 04:26 PM
#1
Thread Starter
Hyperactive Member
Listview, move item up in list
I have a listview with 5 items in it..
item1
item2
item3
item4
item5
i have two buttons, move up and move down, how do i make it so that the user can move item4 up one position which would then move item3 down one position. more or less swap...
Dim lvwItem As New ListViewItem
Dim intIndex As Integer
Dim strItem As String
Dim intPrevItem As Integer
Dim newIndex As Integer
For Each lvwItem In lvwServers.Items
If lvwItem.Selected = True Then
strItem = lvwItem.Text
intIndex = lvwItem.Index
newIndex = intIndex - 1
If newIndex > -1 Then
lvwServers.Items.RemoveAt(intIndex)
lvwServers.Items.Insert(newIndex, strItem)
End If
End If
Next
it doesn't want to work..... any help would be great thanks.
-
Oct 24th, 2003, 06:43 PM
#2
Lively Member
VB Code:
For Each lvwItem In lvwServers.Items
[b]If lvwItem.Selected = True Then[/b]
strItem = lvwItem.Text
intIndex = lvwItem.Index
newIndex = intIndex - 1
If newIndex > -1 Then
lvwServers.Items.RemoveAt(intIndex)
lvwServers.Items.Insert(newIndex, strItem)
End If
End If
Next
That's a good question, I'll bang on that a bit. ListView is my favoritest control Dinner first though.
-
Oct 25th, 2003, 09:46 AM
#3
Lively Member
-
Oct 25th, 2003, 10:37 AM
#4
Lively Member
This works:
VB Code:
Private Sub btnMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnMoveUp.Click
'If there is no selected item in the listview, exit
If lvwItems.SelectedItems.Count = 0 Then
Exit Sub
End If
'Unless you allow multi-select, and you probably don't want to,
'then there's no real reason to loop because there will only
'be one selected item, which is:
Dim lvwItem As ListViewItem = lvwItems.SelectedItems(0)
Dim intIndex, newIndex As Integer
intIndex = lvwItem.Index
newIndex = intIndex - 1
If newIndex > -1 Then
Dim iCount As Integer
'Make a new ListViewItem and copy all subitems
Dim newItem As New ListViewItem
'Looks weird but lvwItem.SubItem(0) is actually the
'same as lvwItem.Text
For iCount = 0 To lvwItem.SubItems.Count
newItem.SubItems.Add(lvwItem.SubItems(iCount))
Next
lvwItems.Items.RemoveAt(intIndex)
lvwItems.Items.Insert(newIndex, newItem)
End If
End Sub
Last edited by Crunch; Oct 25th, 2003 at 10:45 AM.
-
Oct 25th, 2003, 03:46 PM
#5
Addicted Member
this is working fine for me
Code:
Private Sub btn_moveup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_moveup.Click
Dim index As Integer = lst_clients.SelectedIndex
lst_clients.Items.Insert(index - 1, lst_clients.SelectedItem)
lst_clients.Items.RemoveAt(index + 1)
lst_clients.SelectedIndex = index - 1
End Sub
Private Sub btn_movedown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_movedown.Click
Dim index As Integer = lst_clients.SelectedIndex
lst_clients.Items.Insert(index + 2 , lst_clients.SelectedItem)
lst_clients.Items.RemoveAt(index)
lst_clients.SelectedIndex = index + 1
End Sub
-
Oct 25th, 2003, 03:50 PM
#6
Addicted Member
you should be carefull when tring to move the lastitem up or the first item down, my program controls this somewhere esle outside the handler of the click button, you should write some code for that ( in my program in the SelectedIndexChanged event handler of the listbox it gets the selected index and enables and disables the up or down button when needed)
-
Oct 25th, 2003, 09:06 PM
#7
Lively Member
//you should be carefull when tring to move the lastitem up or the first item down,
What's wrong with the code I already posted, for that matter gmatteson's too? Last and first items work correctly. That's what the If newIndex > -1 is for (to stop you from moving the first item). What's the catch for the last item?
Edit: oh you mean for moving down, yes you're right. And your way is definitely cleaner than mine.
-
Oct 28th, 2003, 03:50 PM
#8
Thread Starter
Hyperactive Member
listview
i believe the problem occurs when i have 2 columns in the listview. one column moves up while the other disappears...
-
Oct 28th, 2003, 04:22 PM
#9
this works ok for me, keeping the subitems also....
VB Code:
[Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button1_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button1.Click
[Color=Blue]If[/COLOR] [Color=Blue]Not[/COLOR] ListView1.SelectedItems [Color=Blue]Is[/COLOR] [Color=Blue]Nothing[/COLOR] [Color=Blue]Then[/COLOR] [Color=Green]'///[/COLOR] [Color=Green]incase[/COLOR] [Color=Green]nothing[/COLOR] [Color=Green]is[/COLOR] [Color=Green]selected[/COLOR] [Color=Green].[/COLOR] [Color=Green]
[/COLOR] [Color=Blue]Dim[/COLOR] x [Color=Blue]As[/COLOR] [Color=Blue]Integer[/COLOR] = ListView1.SelectedItems(0).Index - 1
[Color=Blue]Dim[/COLOR] lvi [Color=Blue]As[/COLOR] ListViewItem = [Color=Blue]DirectCast[/COLOR](ListView1.SelectedItems(0), ListViewItem)
ListView1.Items.Remove(lvi)
ListView1.Items.Insert(x, lvi)
[Color=Blue]End[/COLOR] [Color=Blue]If
[/COLOR]
[Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Oct 29th, 2003, 11:11 AM
#10
Frenzied Member
Your example fails if no item in the ListView is selected dynamic_sysop. You need to add a line from Crunch's example:
Code:
If ListView1.SelectedItems.Count = 0 Then Exit Sub
~Peter

-
Oct 29th, 2003, 03:12 PM
#11
strange , but the line " If Not ListView1.SelectedItems Is Nothing " works fine for me ( if nothing is selected it does nothing , otherwise it carries out the code )
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Oct 31st, 2003, 01:59 PM
#12
Lively Member
That's not what I would have expected, I guess the collection ListView.SelectedItems is junked (de-instantiated? I dunno) if there are no selected items. I would have thought the collection was always present though. Interesting to know!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|