Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Printing with List Boxes

Posted on 1998-12-15
9
146 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450221
call PrintListbox(list1, false) to print only selected
0
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

839 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