Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Print Listview

Posted on 1999-07-13
14
Medium Priority
?
427 Views
Last Modified: 2010-05-02
I need to print the contents of a listview control to a printer, with headers and the columns right-justified.(currency).
0
Comment
Question by:beneke
[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
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524226
I assume you need no explanation as to how you can read through a list view and how you can process all the list items and their sub items. Your problem is how you can get the printer to print neatly aligned columns.

It's not hard.

The printer object has a CurrentX and CurrentY property.

You can use the CurrentX to allow for printing in neat columns

Printer.Print "Hello";
Printer.CurrentX = <Value for second column>
Printer.Print "World";
...
Printer.CurrentX = <Value for last column>
Printer.Print "Last" 'Note: no semicolon!

Printing a right-adjusted column is not that hard, but you need a bit of trickery.

1 Determine where the column starts
2 Determine the maximum width of the column
3 Take the text you want to print. Now, the printer has a
  .TextWidth function that you can use with your text
  dblX = Printer.TextWidth("Text you want to print")
4 To print the column right-adjusted, do
  Printer.CurrentX = <Start of column> + <width of column> - <TextWidth of the text you want to print>

Good luck!!

Pino
0
 

Author Comment

by:beneke
ID: 1524227
I gather what you say, though I want to look through some more proposals. If your turn out the best, I'll ask you to lock the ? and reward you.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524228
Fair enough!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:waty
ID: 1524229
I Will search for it
0
 

Author Comment

by:beneke
ID: 1524230
If possible I would like it to be a module.

If its an OCX, I will be using the OCX in a freeware program and thus would like the OCX to be freeware also

PS. I cannot use Crystal, I'm not reading a database
0
 

Author Comment

by:beneke
ID: 1524231
Adjusted points to 100
0
 
LVL 3

Expert Comment

by:gajendra
ID: 1524232
Here is a function for printing Right Justified.

where P is the object you are printing to (Printer/Picture Box/Form)
X is the X Cordinate where to print
Y is the Y Cordinate where to print
W is the width within which to print
and txt is the Text that is to be printed.

I prefer txt to be formatted through the calling code.
PrnRight format(Amount,"format string"), XCord, YCord, 2 * 1370, Printer)
should do the thing.

Public Sub PrnRight(ByVal txt As String, ByVal X As Single, ByVal Y As Single, ByVal W As Single, P As Object)
Dim I, L As Integer
P.CurrentX = X
P.CurrentY = Y
L = Len(txt)
If P.TextWidth(txt) > W Then
    For I = 1 To L
        If P.CurrentX + P.TextWidth(Mid$(txt, I, 1)) < X + W Then
            P.Print Mid$(txt, I, 1);
        Else
            Exit For
        End If
    Next I
Else
    P.CurrentX = P.CurrentX + (W - P.TextWidth(txt))
    P.Print txt;
End If
End Sub



0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524233
Yes, gajendra, that is exactly what I was saying.
0
 

Author Comment

by:beneke
ID: 1524234
I found this code and still have to modify it to cater for page breaks, but boy does it work.


Public Sub PrintListView(LV As ListView, Lines As Integer)

'*********************************************************************
'   LV is the listview to print
'   Lines is the number of lines in the listview to print. 0 print all
'*********************************************************************

Dim LVWidth      As Integer
Dim NewTab       As Integer
Dim j            As Integer
Dim i            As Integer
Dim LineCount    As Integer
Dim iOldFontSize As Integer

Dim itmX         As ListItem
   
LVWidth = 0

For i = 1 To LV.ColumnHeaders.Count
    LVWidth = LVWidth + LV.ColumnHeaders(i).Width
Next i

NewTab = 0

'****************************************************************
'   Printing of the column headers
'****************************************************************
iOldFontSize = Printer.FontSize
Printer.FontSize = 8

Printer.FontUnderline = True
Printer.FontBold = True

For i = 1 To LV.ColumnHeaders.Count
    NewTab = NewTab + CInt(LV.ColumnHeaders(i).Width * Printer.ScaleWidth / LVWidth)
    Printer.Print LV.ColumnHeaders(i).Text;
    Printer.CurrentX = NewTab
Next i

Printer.FontUnderline = False
Printer.FontBold = False

Printer.Print

'****************************************************************
'   Printing of the Listview's Lines
'****************************************************************

If Lines < LV.ListItems.Count And Lines <> 0 Then
    LineCount = Lines
Else
    LineCount = LV.ListItems.Count
End If

For i = 1 To LineCount
    NewTab = 0
    Set itmX = LV.ListItems(i)
    Printer.Print itmX.Text;
    For j = 1 To LV.ColumnHeaders.Count - 1
        NewTab = NewTab + CInt(LV.ColumnHeaders(j).Width * Printer.ScaleWidth / LVWidth)
        Printer.CurrentX = NewTab
        Printer.Print itmX.SubItems(j);
    Next j
    Printer.Print
Next i
 
Printer.FontSize = iOldFontSize

End Sub


0
 

Author Comment

by:beneke
ID: 1524235
Problem solved!!!!!!!!!!!!!!!!!!!
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524236
What about right-adjusted columns?
0
 
LVL 1

Expert Comment

by:jcastr
ID: 1524237
why you can't use crystal??? crystal does not need a database to work... you can simply send your data like formulas or ASCII file ....
0
 
LVL 14

Accepted Solution

by:
waty earned 400 total points
ID: 1524238
' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 14/07/99
' * Time             : 16:52
' **********************************************************************
' * Comments         : Print a listview
' *
' *
' **********************************************************************

Public Sub PrintListView(LV As ListView)
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 14/07/99
   ' * Time             : 16:49
   ' * Module Name      : Listview_Module
   ' * Module Filename  : Listview.bas
   ' * Procedure Name   : PrintListView
   ' * Parameters       :
   ' *                    LV As ListView
   ' **********************************************************************
   ' * Comments         :
   ' *
   ' *
   ' **********************************************************************

   Dim i          As Integer
   Dim LVWidth    As Integer
   Dim NewTab     As Integer
   Dim j          As Integer
   Dim itmX       As ListItem

   Printer.Font.Size = 13

   LVWidth = 0
   For i = 1 To LV.ColumnHeaders.Count
      LVWidth = LVWidth + LV.ColumnHeaders(i).Width
   Next
   NewTab = 0
   For i = 1 To LV.ColumnHeaders.Count
      NewTab = NewTab + CInt(LV.ColumnHeaders(i).Width * Printer.ScaleWidth / LVWidth)
      Printer.Print LV.ColumnHeaders(i).Text;
      Printer.CurrentX = NewTab
   Next
   Printer.Print
   Printer.Print
   
   For i = 1 To LV.ListItems.Count
      NewTab = 0
      Set itmX = LV.ListItems(i)
     
      ' *** Skip a page if needed
      If Printer.CurrentY + Printer.TextHeight(itmX.Text) > Printer.ScaleHeight - 600 Then Printer.NewPage
     
      Printer.Print itmX.Text;
     
      For j = 1 To LV.ColumnHeaders.Count - 1
         NewTab = NewTab + CInt(LV.ColumnHeaders(j).Width * Printer.ScaleWidth / LVWidth)
         Printer.CurrentX = NewTab
         Printer.Print itmX.SubItems(j);
      Next
      Printer.Print
   Next
   Printer.EndDoc

End Sub


0
 

Author Comment

by:beneke
ID: 1524239
Thanks for the help!!!!!!!!!!.

By the way, I will update my code with yours. It work quite nice. If I do ge stuck, may I call on you?.
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

715 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