lp3535
asked on
Selected Items in Listbox
I have a multi-select listbox which puts a tick in the checkbox if the item is selected by the user.
Eg.
Apples
Oranges (selected)
Bananas (selected)
Strawberries
Grapes (selected)
What I want to be able to do is:
a) Send the selected items to a form field (Text1) in my document in the following format:
Oranges
Bananas
Grapes
b) When my dialog box is initialized, reflect which items were selected from the information that is now in my form field (Text1), ie. Oranges, Bananas, Grapes.
I figure this probably needs to be done using an array, which I'm not very familiar with.
Thanks in advance.
Eg.
Apples
Oranges (selected)
Bananas (selected)
Strawberries
Grapes (selected)
What I want to be able to do is:
a) Send the selected items to a form field (Text1) in my document in the following format:
Oranges
Bananas
Grapes
b) When my dialog box is initialized, reflect which items were selected from the information that is now in my form field (Text1), ie. Oranges, Bananas, Grapes.
I figure this probably needs to be done using an array, which I'm not very familiar with.
Thanks in advance.
ASKER
Sorry, that would have helped! VBA - form field is in a Word doc.
Dim lItem As Long
For lItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lItem) = True Then
....
End If
Next
For lItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lItem) = True Then
....
End If
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Dhaest,
I've changed your code slightly (see below) to send the result to my form field, but how do I then tick the selections when I open the dialog box again?
I've changed your code slightly (see below) to send the result to my form field, but how do I then tick the selections when I open the dialog box again?
Dim Msg As String, i As Integer
Msg = ""
With Me.lbRelationshipType
For i = 0 To .ListCount - 1
If .Selected(i) Then
Msg = Msg & .List(i) & Chr(13)
End If
Next i
End With
ActiveDocument.FormFields("Text1").Result = Msg
How is the string formatted to set the items ?
Suppose you have this:
Dim myString as string
Dim avarSplit As Variant
Dim intIndex As Integer
myString = "Oranges,Bananas"
avarSplit = Split( myString , ",")
For intIndex = LBound(avarSplit) To UBound(avarSplit)
With Me.lbRelationshipType
For i = 0 To .ListCount - 1
If .Item(i) = avarSplit(intIndex) Then
.Selected(i) = true
End If
Next i
End With
Next
Suppose you have this:
Dim myString as string
Dim avarSplit As Variant
Dim intIndex As Integer
myString = "Oranges,Bananas"
avarSplit = Split( myString , ",")
For intIndex = LBound(avarSplit) To UBound(avarSplit)
With Me.lbRelationshipType
For i = 0 To .ListCount - 1
If .Item(i) = avarSplit(intIndex) Then
.Selected(i) = true
End If
Next i
End With
Next
ASKER
The string is identical to what your earlier code was, so seperated by Chr(13).
ASKER
I forgot to add - that code generated an error at this line:
If .Item(i) = avarSplit(intIndex) Then
If .Item(i) = avarSplit(intIndex) Then
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Dhaest, that's perfect.
How to get multiple selected values and items from listbox ?
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strItem As [String]
For Each selecteditem As [Object] In listBox1.SelectedItems
strItem = TryCast(selecteditem, [String])
'Process(strItem);
System.Diagnostics.Debug.W
Next
End Sub