Solved

Printing with List Boxes

Posted on 1998-12-15
9
143 Views
Last Modified: 2010-05-03
I have a large List Box on a Form full of items added using the "Additem" method. How do I print:
 
i)everything (all the items) into a report?
ii) only the selected (checked) items into a report?

NB I'm using VB5 Enterprise Edition.
0
Comment
Question by:its_ajt
9 Comments
 

Author Comment

by:its_ajt
ID: 1450218
Edited text of question
0
 

Author Comment

by:its_ajt
ID: 1450219
Edited text of question
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450220
call PrintListbox(list1, true) to print all
call PrintListbox(list1, true) to print only selected

Private Sub PrintListBox(ByVal list As listbox, Optional ByVal fAll As Boolean = True)
    Dim i As Integer
    For i = 0 To list.ListCount - 1
        If (list.Selected(i) Or fAll) Then
            Printer.Print list.list(i)
        End If
    Next
    Printer.EndDoc
End Sub

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450221
call PrintListbox(list1, false) to print only selected
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Expert Comment

by:mark2150
ID: 1450222
I was wondering how two *identical* calls were producing different results! (grin) For a moment there I thought you'ld invented the "do what I meant" function!

M
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450223
It's magic..
0
 

Author Comment

by:its_ajt
ID: 1450224
Can't get the code to work properly - something about list.Selected(i) being an invalid array but the programs at home and I'm at work! - and also it doesn't cater for multiple pages...........else I need more help getting it to work with the Printer object/Common dialog Control.

Thanks - Merry Xmas
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450225
Good Luck
0
 
LVL 1

Accepted Solution

by:
twolff earned 200 total points
ID: 1450226
Mirkwoods code is not bad but I would handle it a different way. It will require 3 functions that can be reused in other applications.

I would write one function that would print the line on the page. This function would be passed the text to be printed. It would then compare the height of the page minus the currenty location to the amount of space that the text to be printed will require using the textheight method of printer object. If the text will fit on the page then print the line else start a new page and then print the line.

The next function will be used to loop through all of the items in the listbox and send the text to the first function to be printed.

The final function will be used to print only the selected items in the list. This is a little harder(or not depending on your api knowledge) than presented by mirkwood. You can use the SendMessage API to retreive an array of indexs of only the selected items in a listbox or filebox. You would then cycle through the array and only print the items that are chosen. This will eliminate the need to loop through the whole list when deciding what is selected. This technique will also work if the listbox's style is changed to checkbox in VB5 and VB6.

Here is a sample that I wrote:

> Add a filebox(File1) and a commandbutton(Command1) to a form and paste the following code.

Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_GETSELCOUNT = &H190
Const LB_GETSELITEMS = &H191

Private Sub Command1_Click()
   Dim lngNumSel As Long
   Dim lngSelItems() As Long
   Dim lngStatus As Long
   Dim strMsg As String
   Dim x As Long
   
   '--- get the number of selected items
   lngNumSel = SendMessage(File1.hwnd, LB_GETSELCOUNT, 0, 0)
   
   '--- redim the array to hold the selected items
   ReDim lngSelItems(lngNumSel)
   
   '--- get the selected items
   lngStatus = SendMessage(File1.hwnd, LB_GETSELITEMS, lngNumSel, lngSelItems(0))
   
   Select Case lngStatus
      Case -1
         strMsg = "Error Occurred"
      Case 0
         strMsg = "None Selected"
      Case Else
         For x = 0 To lngStatus - 1
            strMsg = strMsg & File1.List(lngSelItems(x)) & vbCrLf
         Next x
   End Select
   MsgBox strMsg
End Sub

I hope this helps. Enjoy.
0

Featured Post

Highfive Gives IT Their Time Back

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!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 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

19 Experts available now in Live!

Get 1:1 Help Now