Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Printing with List Boxes

Posted on 1998-12-15
9
Medium Priority
?
150 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
[X]
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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 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