Link to home
Start Free TrialLog in
Avatar of LearningJava
LearningJava

asked on

Selected property and printing

Hi:
    Can someone give examples of how to use the selected property to print multiple selection from a List box?
Avatar of HoweverComma
HoweverComma

Dim Inum as Integer
Inum=0

While Inum< ListBox.ItemCount-1      ' -1 because it is Zero based
If ListBox.Selected(Inum)=True Then
    'It is selected we need to print
    ' If it is not numeric data the use Str(ListBox.ItemData(Inum))
    Printer.Print ListBox.ItemData(Inum)
End If
Printer.EndDoc
Wend
Private Sub Command1_Click()
Dim c As Integer
For c = 0 To List1.ListCount - 1
    If List1.Selected(c) = True Then
        Debug.Print List1.List(c)
    End If
Next c
End Sub

S
Set List1 MultiSelect to true in design mode..

and then do following

dim ar() as string

Private Sub Command1_Click()
      Redim ar(List1.ListCount-1)
      dim i!,j!
      j=0

      for i = 0 to list1.ListCount - 1
         if List1.Selected(i) = True then
           printer.print List1.List(i)
           ar(j) = List1.List(i)''for storing into an array for later use.
           j=j+1
         end if
      next

instead of storing it into an array u can store the list into a text file...

which u can print later after the loop..
reasn being..

above code will print each line into a separate page (i suppose)

to avoid that ..u can append the list to a file...which u can print later..


Avatar of LearningJava

ASKER

Where does ListCount come from? I don't see it in the ListBox properties?

Also, can someone explain to me what ItemData means?
Are you with VB or VBA?
ListCount is a property for listbox in VB.  ItemData is another property that allows you to set a specific NUMBER to each item in the list, so later on you can identity this item using the ItemData, like if you have a job number, or employee number, or part number, so you can display the item's name and store in ItemData the corresponding number for later use.

S
I am using Visual basic 6.0.

Ithought that the Listbox entries could be accessed by index number??
Yes, they are.

Dim c As Integer
For c = 0 To List1.ListCount - 1    'listcount is the number of items in the list
    If List1.Selected(c) = True Then    'c is looping among the index numbers.
        Debug.Print List1.List(c)    'doing your stuff with a specific item in the list. In this case - a selected item
        'do your stuff here
    End If
Next c

S

What is the meaning of Debug.Print?

Why not Printer.Print List1.List(c)?

Also, could you elaborate on ListCount? I don't see it in the  properties for Listbox.

Debug.Print is a convenient way to see the result in the immediate window (the bottom window on the VB) to check that your code does what you really expect it to do. If you want to print to a printer then Printer.Print List1.List(c) is just fine.

S
ASKER CERTIFIED SOLUTION
Avatar of Shauli
Shauli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial