We help IT Professionals succeed at work.

2 Listboxes with index problem

thaburner asked
Last Modified: 2010-05-01
I have two listboxes and the first one is Priv. Availb. and the other is Priv. Applied. I figured out how to move priv. from one list to another. but how do i set the index when i move it over to the priv applied list?  I tried adding the list index option after the additem for the other list but i got an error Invalid procedure call or argument. Also i have a button to move all items from one list to another is their a easy way of doing this. As well when i go to save the settings i need to read the listbox to see which permissions where moved over to Priv applied how can I read through them all?

Private Sub cmdL1toL2_Click()

    lstPrivApplied.AddItem lstPrivAvil.Text
    lstPrivAvil.RemoveItem lstPrivAvil.ListIndex

End Sub

Private Sub cmdL2toL1_Click()

    lstPrivAvil.AddItem lstPrivApplied.Text
    lstPrivAvil.RemoveItem lstPrivAvil.ListIndex

End Sub

Private Sub AddPrivToList()
lstPrivAvil.AddItem "SELECT", 0
lstPrivAvil.AddItem "INSERT", 1
lstPrivAvil.AddItem "UPDATE", 2
lstPrivAvil.AddItem "DELETE", 3
lstPrivAvil.AddItem "CREATE", 4
lstPrivAvil.AddItem "DROP", 5
lstPrivAvil.AddItem "GRANT", 6
lstPrivAvil.AddItem "REFERENCES", 7
lstPrivAvil.AddItem "INDEX", 8
lstPrivAvil.AddItem "ALTER", 9
lstPrivAvil.AddItem "CREATE_TMP_TABLE", 10
lstPrivAvil.AddItem "LOCK_TABLES", 11
lstPrivAvil.AddItem "CREATE VIEW", 12
lstPrivAvil.AddItem "SHOW VIEW", 13
lstPrivAvil.AddItem "CREATE ROUTINE", 14
lstPrivAvil.AddItem "ALTER ROUTINE", 15
End Sub
Watch Question

I don't like using the index as an ID for items in a listbox. Its Better to use the ItemData property for each item because the ItemData doesn't change when moving or reordering items and its a user option to assign the data (number) you want to each item:

Private Sub cmdL1toL2_Click()
    Dim tmpIndex As Integer
    tmpIndex = lstPrivAvil.ListIndex
    lstPrivApplied.AddItem lstPrivAvil.Text
    lstPrivApplied.ItemData(lstPrivAvil.NewIndex) = lstPrivAvil.ItemData(tmpIndex)

    lstPrivAvil.RemoveItem tmpIndex

End Sub

Private Sub cmdL2toL1_Click()
    Dim tmpIndex As Integer
    tmpIndex = lstPrivApplied.ListIndex
    lstPrivAvil.AddItem lstPrivApplied.Text
    lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = lstPrivApplied.ItemData(tmpIndex)

    lstPrivApplied.RemoveItem tmpIndex

End Sub

Private Sub AddPrivToList()
' We start numbering from 1 because 0 is the default number

lstPrivAvil.AddItem "SELECT"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 1

lstPrivAvil.AddItem "INSERT"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 2

lstPrivAvil.AddItem "UPDATE"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 3

lstPrivAvil.AddItem "DELETE"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 4

lstPrivAvil.AddItem "CREATE"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 5

lstPrivAvil.AddItem "DROP"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 6

lstPrivAvil.AddItem "GRANT"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 7

lstPrivAvil.AddItem "REFERENCES"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 8

lstPrivAvil.AddItem "INDEX"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 9

lstPrivAvil.AddItem "ALTER"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 10

lstPrivAvil.AddItem "CREATE_TMP_TABLE"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 11

lstPrivAvil.AddItem "LOCK_TABLES"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 12

lstPrivAvil.AddItem "CREATE VIEW"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 13

lstPrivAvil.AddItem "SHOW VIEW"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 14

lstPrivAvil.AddItem "CREATE ROUTINE"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 15

lstPrivAvil.AddItem "ALTER ROUTINE"
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = 16

End Sub

Now you can check the ItemData of the listbox to see what permisions is selected using a loop eg: if the itemData =1 then the permision is SELECT,...


I get error
invalid property array index

it works fine if i comment these two lines

lstPrivApplied.ItemData(lstPrivAvil.NewIndex) = lstPrivAvil.ItemData(tmpIndex)
lstPrivAvil.ItemData(lstPrivAvil.NewIndex) = lstPrivApplied.ItemData(tmpIndex)
This one is on us!
(Get your first solution completely free - no credit card required)


that did the trick thx
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.