Results 1 to 15 of 15

Thread: list boxes

  1. #1
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HI Folks
    Office 2003 Word
    I have a userform with two list boxes on it. The general idea is that as the user selects an item from listbox1 it is deleted from listbox1 and placed in listbox2
    My problem is this listbox1 will only allow me to selexct the first item on the list. I really want to be able to select any item from the list and place it in listbox2 .
    Is this a property of the listbox control or does some code need to be written. I have no problem in the code to remove and put the item in listbox2. that is working fine. Its just this not being able to select any item in the listbox.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Make sure that the Enabled property of the list box is set to True.

  3. #3
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='771690' date='22-Apr-2009 07:23']Make sure that the Enabled property of the list box is set to True.[/quote]

    Hi Hans
    Thats the problem it is. I think it might be the code I am using on the command button to select the item
    Do you have a snippet of code that will select the sected item off the list?
    This is what I have

    Dim i As Integer
    If Me.ListHazard.Selected(i) = False Then
    Exit Sub
    ListHazard.SetFocus
    End If

    For i = 0 To Me.ListHazard.ListCount - 1
    If Me.ListHazard.Selected(i) Then
    hazard = Me.ListHazard.List(i)

    End If
    Next i
    For i = 0 To ListHazard.ListCount - 1
    If ListHazard.Selected(i) = True Then
    ListHazardsscored.AddItem ListHazard.List(i)
    ListHazard.RemoveItem (i)
    ListHazard.Selected(i) = False
    End If
    Next i

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You don't have to deselect a removed item, and you need to loop backwards to avoid problems.
    You can use code like this:

    Code:
    Private Sub cmdRight_Click()
      Dim i As Integer
      For i = Me.ListHazard.ListCount - 1 To 0 Step -1
    	If Me.ListHazard.Selected(i) Then
    	  Me.ListHazardsScored.AddItem Me.ListHazard.List(i)
    	  Me.ListHazard.RemoveItem i
    	End If
      Next i
    End Sub
    See the attached sample document.
    Attached Files Attached Files

  5. #5
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='771930' date='23-Apr-2009 10:50']You don't have to deselect a removed item, and you need to loop backwards to avoid problems.
    You can use code like this:

    Code:
    Private Sub cmdRight_Click()
      Dim i As Integer
      For i = Me.ListHazard.ListCount - 1 To 0 Step -1
    	If Me.ListHazard.Selected(i) Then
    	  Me.ListHazardsScored.AddItem Me.ListHazard.List(i)
    	  Me.ListHazard.RemoveItem i
    	End If
      Next i
    End Sub
    See the attached sample document.[/quote]
    Thanks Hans Will give it a go

  6. #6
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='jonrpullen' post='771933' date='23-Apr-2009 11:44']Thanks Hans Will give it a go[/quote]

    Hi Hans
    That worked fine thanks but it threw up naother problem that I have been unable to resolve
    If nothing is selected in listhazard and the user clicks the next button I want the form to be reshown with the list for selection
    I have tried to say

    if listhazard.selected(i) = false then exit sub
    frmlisthazrd.listhazard.setfocus
    end if

    but this approach simply stops the next button working probably because the form has not been reset

    Ideas please

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    I don't understand your description. Could you try to explain more clearly?

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    [quote name='jonrpullen' post='771956' date='23-Apr-2009 23:28']It threw up naother problem that I have been unable to resolve
    If nothing is selected in listhazard and the user clicks the next button I want the form to be reshown with the list for selection[/quote]
    Try
    If listhazard.listindex = -1 then
    msgbox "you have to pick a hazard"
    me.listhazard.setfocus
    Else
    'do something
    End if
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #9
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the code I have at present. The problem now is this it copies all the items in listhazard to listhazardscored and does not set hazard to the selected item

    Dim i As Integer
    If Me.ListHazard.ListIndex = -1 Then
    MsgBox "You must select one hazard"
    Me.ListHazard.SetFocus
    Else
    For i = Me.ListHazard.ListCount - 1 To 0 'Step -1
    If Me.ListHazard.Selected(i) Then
    Me.ListHazardsscored.AddItem Me.ListHazard.List(i)
    hazard = Me.ListHazard.Text
    Me.ListHazard.RemoveItem i
    End If
    Next i
    End If

    This is what is supposed to happen
    Up to 30 items caould be listed in listhazrd
    The user selects one item from the list
    The variable hazard is set to the text of the item selected
    the item is removed from listhazard
    the item is placed in listhazardscored (it is never reused or returned to listhazard)

    this all happens when the user clicks the NEXT commandbutton.

    At present the code does not set the variable and copies all the listhazard items to listhazardscored

    The from will be reloaded to after the selected hazard is scored and a document is created
    so it needs to remain in the same state as it was left with the listhazard showing the items not yet scored and the listhazardscored showing those that have been scored.
    My code still only selects the first in the list

    Any ideas as to how to make this work would be appreciated

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You shouldn't have commented out "Step -1" - it is necessary for the code to run.

    I don't understand how this code could copy ALL items unless the user selected ALL items.

    Could you attach a sample document, with sensitive information altered or removed? You can zip the document if it is large.

  11. #11
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='772458' date='27-Apr-2009 08:50']You shouldn't have commented out "Step -1" - it is necessary for the code to run.

    I don't understand how this code could copy ALL items unless the user selected ALL items.

    Could you attach a sample document, with sensitive information altered or removed? You can zip the document if it is large.[/quote]

  12. #12
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HERE IS THE DOCUMENT REQUESTED

    Thanks Hans
    Attached Files Attached Files

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Because your list box does not permit multiple selection, the code can be simplified:

    Code:
    Private Sub Cmdnext_Click()
      Dim i As Integer
      i = Me.ListHazard.ListIndex
      If i = -1 Then
    	Me.ListHazard.SetFocus
    	MsgBox "Please select a hazard"
    	Exit Sub
      End If
      hazard = Me.ListHazard
      Me.ListHazardsscored.AddItem Me.ListHazard
      Me.ListHazard.RemoveItem i
      ...
      ...
    End Sub

  14. #14
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='772476' date='27-Apr-2009 11:07']Because your list box does not permit multiple selection, the code can be simplified:

    Code:
    Private Sub Cmdnext_Click()
      Dim i As Integer
      i = Me.ListHazard.ListIndex
      If i = -1 Then
    	Me.ListHazard.SetFocus
    	MsgBox "Please select a hazard"
    	Exit Sub
      End If
      hazard = Me.ListHazard
      Me.ListHazardsscored.AddItem Me.ListHazard
      Me.ListHazard.RemoveItem i
      ...
      ...
    End Sub
    [/quote]

    Thanks Hans

  15. #15
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    The usual interface conventions would let you either double click an item to move it or click on an arrow located between the two lists. I would normally expect the Next button to take me to another userform or finish.
    I would try putting in code on the double click action of the hazard list
    Code:
    Private Sub ListHazard_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
      Me.ListHazardsscored.AddItem Me.ListHazard
      Me.ListHazard.RemoveItem Me.ListHazard.ListIndex
    End Sub
    This greatly simplifies your code and avoids the need to deal with multiple selections - your code looks like it came from a source where multiple selections were possible but you have multiple selections disabled on the list.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •