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) .
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
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
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
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
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.
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).W idth * 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).W idth * Printer.ScaleWidth / LVWidth)
Printer.CurrentX = NewTab
Printer.Print itmX.SubItems(j);
Next j
Printer.Print
Next i
Printer.FontSize = iOldFontSize
End Sub
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).W
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).W
Printer.CurrentX = NewTab
Printer.Print itmX.SubItems(j);
Next j
Printer.Print
Next i
Printer.FontSize = iOldFontSize
End Sub
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?.
By the way, I will update my code with yours. It work quite nice. If I do ge stuck, may I call on you?.
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