|
-
Feb 13th, 2008, 10:28 AM
#1
Thread Starter
Junior Member
Writing selected items from listbox to textbox
Hello world!!!
How can I write all the selected item of the listbox to a textbox? For e.g. I have these items from a listbox:
-rey
-john
-jane
-mike
-ken
-blen
If I selected rey and jane, the output of the textbox should be rey, jane else the output is All
By the way, my listbox's style is 1-Checkbox.
This is my sample code
Code:
Private Sub cmdOK_Click()
If chkAll.Value = 1 Then
txtName.Text = "All"
Else
txtName.Text = lstNames.Text
End If
Please guide me. thanks
-
Feb 13th, 2008, 10:37 AM
#2
Re: Writing selected items from listbox to textbox
You need something like
Code:
Private Sub cmdOK_Click()
Dim i As Long
If chkAll.Value = 1 Then
txtName.Text = "All"
Else
For i = 0 To lstNames.ListCount - 1
If lstNames.Selected(i) = True Then
txtName.Text = txtName.Text & lstNames.List(i) & vbCrLf
End If
Next
End If
-
Feb 13th, 2008, 01:40 PM
#3
Re: Writing selected items from listbox to textbox
An extension of Hack's code:
Code:
Dim MyNames() As String
Const ListSize = 5
Private Sub Command1_Click()
Text1.Text = vbNullString
For I = 0 To List1.ListCount - 1
If List1.Selected(I) = True Then Text1.Text = Text1.Text & List1.List(I) & vbCrLf
Next
If Text1.Text = vbNullString Then ' Nothing selected, so take them all
For I = 0 To List1.ListCount - 1
Text1.Text = Text1.Text & List1.List(I) & vbCrLf
Next
End If
End Sub
Private Sub Form_Load()
ReDim MyNames(ListSize)
MyNames(0) = "-rey"
MyNames(1) = "-john"
MyNames(2) = "-jane"
MyNames(3) = "-mike"
MyNames(4) = "-ken"
MyNames(5) = "-blen"
For I = 0 To ListSize
List1.AddItem MyNames(I)
Next
Text1.Text = vbNullString
End Sub
Private Sub List1_Click()
List1.Selected(List1.ListIndex) = True
End Sub
I select an item with each click on the list. Then I clear the text box each time the command button is pressed so that you can add to the list of selections.
The selection could also be done with a "Select" command button press. If so, then you may want to add an "Unselect" command button so that if the name in the list is selected, it can be deselected with a button press if the index is at that location. The Unselect button could be enabled only if the ListIndex is located on an item that has already been selected and disabled if the index is located on an item that has not been selected.
-
Feb 13th, 2008, 04:30 PM
#4
Re: Writing selected items from listbox to textbox
@Code Doc: in case there's no selected items your first loop is irrelevant (and can take quite some time). Use the .SelCount property:
Code:
Private Sub Command1_Click()
Text1.Text = vbNullString
If List1.SelCount Then
For I = 0 To List1.ListCount - 1
If List1.Selected(I) = True Then Text1.Text = Text1.Text & List1.List(I) & vbCrLf
Next
Else
For I = 0 To List1.ListCount - 1
Text1.Text = Text1.Text & List1.List(I) & vbCrLf
Next
End If
End Sub
-
Feb 14th, 2008, 05:30 AM
#5
Thread Starter
Junior Member
Re: Writing selected items from listbox to textbox
Thanks a lot guys!!!
Now I'll try it a little bit later. 
BTW, Happy Valentines Day!
Rey Sean
-
Feb 14th, 2008, 11:43 AM
#6
Re: Writing selected items from listbox to textbox
 Originally Posted by gavio
@Code Doc: in case there's no selected items your first loop is irrelevant (and can take quite some time). Use the .SelCount property:
Code:
Private Sub Command1_Click()
Text1.Text = vbNullString
If List1.SelCount Then
For I = 0 To List1.ListCount - 1
If List1.Selected(I) = True Then Text1.Text = Text1.Text & List1.List(I) & vbCrLf
Next
Else
For I = 0 To List1.ListCount - 1
Text1.Text = Text1.Text & List1.List(I) & vbCrLf
Next
End If
End Sub
Terrific idea! I forgot about the .SelCount property. Thanks, Gavio!
-
Feb 14th, 2008, 08:34 PM
#7
Re: Writing selected items from listbox to textbox
You might want to throw in a if
If list1.lisIndex = -1 then
msgbox "No item selected"
end if
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
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
|