Results 1 to 7 of 7

Thread: Writing selected items from listbox to textbox

  1. #1

    Thread Starter
    Junior Member lotus18's Avatar
    Join Date
    Dec 2007
    Location
    Zamboanga City, Philippines
    Posts
    20

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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

  5. #5

    Thread Starter
    Junior Member lotus18's Avatar
    Join Date
    Dec 2007
    Location
    Zamboanga City, Philippines
    Posts
    20

    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

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Writing selected items from listbox to textbox

    Quote 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!
    Doctor Ed

  7. #7
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    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
  •  



Click Here to Expand Forum to Full Width