Solved

Selected property and printing

Posted on 2004-10-08
10
175 Views
Last Modified: 2010-05-02
Hi:
    Can someone give examples of how to use the selected property to print multiple selection from a List box?
0
Comment
Question by:LearningJava
10 Comments
 
LVL 7

Expert Comment

by:HoweverComma
ID: 12264547
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
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264554
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
0
 
LVL 10

Expert Comment

by:anv
ID: 12265387
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..


0
 

Author Comment

by:LearningJava
ID: 12266236
Where does ListCount come from? I don't see it in the ListBox properties?

Also, can someone explain to me what ItemData means?
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12266262
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:LearningJava
ID: 12266406
I am using Visual basic 6.0.

Ithought that the Listbox entries could be accessed by index number??
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12266429
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

0
 

Author Comment

by:LearningJava
ID: 12266598
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.

0
 
LVL 19

Expert Comment

by:Shauli
ID: 12266662
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
0
 
LVL 19

Accepted Solution

by:
Shauli earned 500 total points
ID: 12266691
ListCount must be in the property list. Type List1. (dot) and a list of properties will open. Scroll down (it is sorted alphabetically) and you'l find it there.
ListCount returns the total of items in a listbox. Lets say your listbox list is:

John Doe
Jane Doe
GW Bush
JF Kerry

Then ListCount will return 4. However, as the first index number for the list in a listbox is 0, then when you loop among the index of the list, you loop from 0 to ListCount -1, which means from 0 to 3 (in this case, where you have 4 items in the list)

S
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now