Link to home
Start Free TrialLog in
Avatar of beneke
beneke

asked on

Print Listview

I need to print the contents of a listview control to a printer, with headers and the columns right-justified.(currency).
Avatar of caraf_g
caraf_g

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
Avatar of beneke

ASKER

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.
Fair enough!
I Will search for it
Avatar of beneke

ASKER

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
Avatar of beneke

ASKER

Adjusted points to 100
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



Yes, gajendra, that is exactly what I was saying.
Avatar of beneke

ASKER

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


Avatar of beneke

ASKER

Problem solved!!!!!!!!!!!!!!!!!!!
What about right-adjusted columns?
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 ....
ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beneke

ASKER

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?.