12 Replies - 1448 Views - Last Post: 23 March 2012 - 01:47 AM Rate Topic: -----

#1 Rhymer  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 282
  • Joined: 21-April 09

Move list item and selected property up / down

Posted 20 March 2012 - 09:26 AM

I have two command buttons in an array to move items and their selected property up or down, in this case up.
I'm just stuck at what the command should be. Basically I want to swap two items.
Lists always give me problems (sigh).
Private Sub cmdMoveUp_Click(Index As Integer)
Dim Temp As String
Dim Chkd As Integer
Dim D As Integer
D = lstMachines.ListIndex
If D < 0 Then Exit Sub
If Index = 0 Then  'up
    If lstDetails(D).ListCount < 0 Then Exit Sub  'empty list
    If lstDetails(D).ListIndex < 1 Then Exit Sub   'item zero cannot move up
' save list() and selected property of item above
    Temp = lstDetails(D).List(lstDetails(D).ListIndex - 1)  'item above selected item
    Chkd = lstDetails(D).List(lstDetails(D).Selected) ' <--- wrong
' assign item above to what is to be moved
    lstDetails(D).List(lstDetails(D).ListIndex - 1) = lstDetails(D).List(lstDetails(D).ListIndex)
    ' Do same for selected property 
' move item saved down
    lstDetails(D).List(lstDetails(D).ListIndex) = Temp
    lstDetails(D).List(lstDetails(D).Selected) = Chkd  ' <--- wrong
End If
If Index = 1 Then  'down
' similar to move up
End If
End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Move list item and selected property up / down

#2 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 268
  • Joined: 21-May 09

Re: Move list item and selected property up / down

Posted 20 March 2012 - 10:41 AM

if i get it right you have 2 command buttons and 1 listbox right?
then the command buttons should not be in an array because no matter which one you'll click both will trigger the same part of the code.
only if the index of the button matter you should put it in array.
for example in a program i wrote i have 10 textboxes and 10 buttons.
all buttons and textboxes are in array and i use their index number to know from which textbox to get a file path.

as for your problem you could try to load the items from the listbox into variable array (type string) and depened on the button you click and the item you selected from the list you'll swap the items in the variable array, clear the list and with a loop load each item back into the list.

you may need few more variables for this and remember the zero based array start with element 0 so if you make array like this:

Dim Arr(5) as string


it will have 6 elements in it, from 0 to 5.
you'll need zero based array (its default in VB unless you turn Option Base 1) since listbox always start with number 0 to its first item.

you should consider counting how many items you have in your list and then Redim the array with that number before loading data into it.
Was This Post Helpful? 0
  • +
  • -

#3 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,778
  • Joined: 26-March 09

Re: Move list item and selected property up / down

Posted 20 March 2012 - 10:55 AM

You could do something like below (obviously you'll need to put some wrappers around it to check that your not moving past the end of the list etc.)

Dim intPos As Integer
Dim strData As String
    
    'Get the position of the selected item
    intPos = List1.ListIndex
    'Store the data
    strData = List1.List(intPos)
    'remove the item
    List1.RemoveItem (intPos)
    're-add the item at the new position
    List1.AddItem strData, intPos + 1

Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Move list item and selected property up / down

Posted 20 March 2012 - 11:16 AM

View PostNeku, on 20 March 2012 - 06:41 PM, said:

if i get it right you have 2 command buttons and 1 listbox right?
then the command buttons should not be in an array because no matter which one you'll click both will trigger the same part of the code.

Neku, while I think you understand control arrays because you mention index, this is confusing so I'll attempt to clarify for other readers. You can have command buttons in an array, but you have to make a control array. You do this by giving them the same name, and providing different values for their index property. Then they can share the same event handler (the "part of the code" you mention).

As you can see, rhymer's event handler has an "Index As Integer" value passed as an argument. Index will be equal to the value of the Index property of the command button that was clicked. So, he's done this right.
Was This Post Helpful? 0
  • +
  • -

#5 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 268
  • Joined: 21-May 09

Re: Move list item and selected property up / down

Posted 20 March 2012 - 11:22 AM

View PostBobRodes, on 20 March 2012 - 08:16 PM, said:

View PostNeku, on 20 March 2012 - 06:41 PM, said:

if i get it right you have 2 command buttons and 1 listbox right?
then the command buttons should not be in an array because no matter which one you'll click both will trigger the same part of the code.

Neku, while I think you understand control arrays because you mention index, this is confusing so I'll attempt to clarify for other readers. You can have command buttons in an array, but you have to make a control array. You do this by giving them the same name, and providing different values for their index property. Then they can share the same event handler (the "part of the code" you mention).

As you can see, rhymer's event handler has an "Index As Integer" value passed as an argument. Index will be equal to the value of the Index property of the command button that was clicked. So, he's done this right.



oops >.<" you right on that one... forgot the index can be used... *shooting myself* xD
Was This Post Helpful? 0
  • +
  • -

#6 Rhymer  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 282
  • Joined: 21-April 09

Re: Move list item and selected property up / down

Posted 20 March 2012 - 11:50 AM

Maj, that is a way to do it, store the data I need, remove it, then insert it where I want it.
I don't see the post now, but was also mentioned to copy all data into a variable array and do the swap there, clear the list and re-populate the list from the variable array. The confusion for me is there seems to be TWO selected properties: 1. if the item is highlighted (selected) and whether the item is checked (selected). I think I'll go with a variable array (probably longer but I understand it better than maj's method). Hey maybe it's time I should learn that trick. Could be the coolest thing I learn today (Caffeine Lounge thread). Heck, maj just gave me the codz and it would be by FAR simpler/shorter.
Thanks y'all.

Was This Post Helpful? 0
  • +
  • -

#7 Rhymer  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 282
  • Joined: 21-April 09

Re: Move list item and selected property up / down

Posted 20 March 2012 - 12:57 PM

OK< I got it to work using maj's idea of remove,add items.
Now all I gotta do is the move-down part.
Private Sub cmdMoveUp_Click(Index As Integer)
Dim Temp As String
Dim Chkd As Integer
Dim D As Integer
Dim E As Integer
D = lstMachines.ListIndex
If D < 0 Then Exit Sub
If Index = 0 Then  'up
    If lstDetails(D).ListCount < 0 Then Exit Sub  'empty
    If lstDetails(D).ListIndex < 1 Then Exit Sub   'item zero selected
Rem save list() and selected property of items
    Moving = 1
    E = lstDetails(D).ListIndex
    Temp = lstDetails(D).List(lstDetails(D).ListIndex)
    Chkd = lstDetails(D).Selected(lstDetails(D).ListIndex)
Rem move item saved down
    lstDetails(D).RemoveItem (E)
    lstDetails(D).AddItem Temp, E - 1
    lstDetails(D).Selected(E - 1) = Chkd
    lstDetails(D).ListIndex = E - 1
    Moving = 0
End If
If Index = 1 Then  'down
Rem similar to move up
End If
End Sub

Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Move list item and selected property up / down

Posted 21 March 2012 - 10:45 AM

Quote

*shooting myself* xD


That's a bit extreme. Perhaps you could just burn yourself a little or something. :)

This post has been edited by BobRodes: 21 March 2012 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Move list item and selected property up / down

Posted 21 March 2012 - 10:58 AM

Rhymer, this is a great example of when to use the With block. Like this:
If Index = 0 Then  'up
If Index = 0 Then
  If .ListCount < 0 Then Exit Sub  'empty
  If .ListIndex < 1 Then Exit Sub   'item zero selected
  'save list() and selected property of items
  Moving = 1
  With lstDetails(D)
    E = .ListIndex
    Temp = .List(.ListIndex)
    Chkd = .Selected(.ListIndex)
    'move item saved down
    .RemoveItem (E)
    .AddItem Temp, E - 1
    .Selected(E - 1) = Chkd
    .ListIndex = E - 1
  End With
Moving = 0
End If
Not only does it look cleaner, but it's more efficient. VB only has to resolve the pointer to the object once for the entire With block, rather than once each time you reference the object.
Was This Post Helpful? 0
  • +
  • -

#10 Rhymer  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 282
  • Joined: 21-April 09

Re: Move list item and selected property up / down

Posted 22 March 2012 - 11:18 AM

That is an excellent suggestion, Bob. I'll implement it (With).
Was This Post Helpful? 0
  • +
  • -

#11 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Move list item and selected property up / down

Posted 22 March 2012 - 03:10 PM

I find it to be a good habit in general. Another big use of With is when you are using a "throwaway object", such as a recordset that you are only using to populate a text box:
With New ADODB.Recordset
    .Open "select * from myList", "myConnectionstring"
    Do Until .EOF
        cboMyDropdown.AddItem .Fields("myFieldNameOrNumber") 'field number is considerably faster than name
        .MoveNext
    Loop
    .Close
End With
What's neat about this is that the scope and lifetime of the object are limited to the With block, which therefore affords a means of having a finer granularity of scope and lifetime than local scope. There are all kinds of uses for this "With new classname" construct.
Was This Post Helpful? 1
  • +
  • -

#12 Rhymer  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 282
  • Joined: 21-April 09

Re: Move list item and selected property up / down

Posted 22 March 2012 - 04:46 PM

Bob, congratulations to you. You are a true 'white hat' hacker. I would love to explain an episode of tracing, via debug (DOS 2?), Brown Bag's release of PC-write 2.3, when Button let it go.
I was wise to assembly and cracked the relatively simple password sequence. As I already had the 2.3 version, I was doing it just for fun using debug.
Favorite languages: bBin2exe, Exe2com

My database skills are poor. I did a pretty large program (6-7 forms) and used ADO, CSI, INS, POL, DOA, ABC, NBC, JPG, BMP, Jk'g :dangole: but all she had to do was run the program and it worked. HAHa :o

Why does LIST seem so complicated? It's like I am missing a fundamental element that prevents me from understanding this animal/control.

Selected?

Was This Post Helpful? 0
  • +
  • -

#13 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,778
  • Joined: 26-March 09

Re: Move list item and selected property up / down

Posted 23 March 2012 - 01:47 AM

View PostBobRodes, on 22 March 2012 - 10:10 PM, said:

I find it to be a good habit in general. Another big use of With is when you are using a "throwaway object", such as a recordset that you are only using to populate a text box:
With New ADODB.Recordset
    .Open "select * from myList", "myConnectionstring"
    Do Until .EOF
        cboMyDropdown.AddItem .Fields("myFieldNameOrNumber") 'field number is considerably faster than name
        .MoveNext
    Loop
    .Close
End With
What's neat about this is that the scope and lifetime of the object are limited to the With block, which therefore affords a means of having a finer granularity of scope and lifetime than local scope. There are all kinds of uses for this "With new classname" construct.



Thanks for the tip Bob, it never ceases to amaze me, how little I know about this language, but yet have used it for years! :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1