By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Listboxes

P: 63
I am trying to select multiple items in a listbox and place the values into a textbox on another form in vb6. I just can't seem to get it right. If I select 3 items, it only returns the last item selected? Please help!
Apr 16 '07 #1
Share this Question
Share on Google+
7 Replies


P: 60
It should look something like this as far as I can tell. This is not my code, I found this information here:

http://www.thescripts.com/forum/thread399839.html

Expand|Select|Wrap|Line Numbers
  1. Dim objX as Object
  2. For Each objX in lstBox.SelectedItems
  3.     'Do Something
  4. Next
  5.  
Apr 16 '07 #2

P: 63
Thanks. But selectedItems is not a valid method or property for my listbox?
Apr 16 '07 #3

devonknows
100+
P: 137
It should look something like this as far as I can tell. This is not my code, I found this information here:

http://www.thescripts.com/forum/thread399839.html

Expand|Select|Wrap|Line Numbers
  1. Dim objX as Object
  2. For Each objX in lstBox.SelectedItems
  3.     'Do Something
  4. Next
  5.  
If you are using VB 6 or below the problem is there isnt a .SelectedItems Method, that is .NET and the link is for .NET, something like this could be of help to you

Going on the basis that your other form is called "MyFormName" which you can change lol, The text box you are transfering the information to is called "Text1" and the List

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim i As Integer
  3.  
  4.     MyFormName.Text1.Text = "" 'Clear The Text Box For Use
  5.     MyFormName.Text1.MultiLine = True 'Make Sure The Text Box Is Multiline
  6.  
  7.     'Loop through the items in the List Box (Named: List1)
  8.     For i = 0 To List1.ListCount - 1
  9.  
  10.         If List1.Selected(i) = True Then     'If The List Item Is Selected
  11.  
  12.             With MyFormName 'With The Other Form
  13.  
  14.                 .Text1.Text = .Text1.Text & List1.List(i) 'Add The Selected Text
  15.                 .Text1.Text = .Text1.Text & vbCrLf        'Next Line After The Added Text
  16.  
  17.             End With            'End With The Other Form
  18.         End If
  19.     Next
  20.     MyFormName.Show          'Show the other form.
  21. End Sub
Comments are included so that you can determine what each line of code does, (Turns green when in the code explorer screen), but i hope this helps you

Brief Rundown:
For i = 0 to List1.listcount - 1 : The listbox's list starts at 0, where as the count does not, so to stop it getting confused - 1 from the list count
vbCrLf: Next line
With MyFormName: means you dont have to type out MyFormName.Text1.Text everytime you want to do something with an object on that particular form.

Hope This Helps
Kind Regards
Devon.
Apr 16 '07 #4

P: 63
Thanks Devon. It works beautifully!
Apr 16 '07 #5

P: 60
Sorry, I missed the vb6 part. Thank you for the correction, devon.
Apr 16 '07 #6

devonknows
100+
P: 137
Its quite alright, glad to help i know when i struggle for things i want people to help me so i return the favour when i can, The comments should give you all the guidance you need with it, pretty self explanatory...

Sorry, I missed the vb6 part. Thank you for the correction, devon.
Sorry for the abruptness, was a bit hungover this afternoon :P but i looked and thought... Im sure it doesnt have SelectedItems, unless ive been going about it the wrong way all this time rofl.

Kind Regards
Devon
Apr 16 '07 #7

P: 60
Yesterday morning I had my right half of my body kinda jerk, and felt really bad all of a sudden. I was somewhat disoriented but tried to go about my daily routine. I eventually went to the hospital, but I missed many simple things yesterday due to being disorented, so I appologize for making mistakes yesterday by answering questions when I should have been going to a doctor.
Apr 17 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.