Results 1 to 13 of 13

Thread: Urgent ListView problem - Unresolved

  1. #1

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718

    Urgent ListView problem - Unresolved

    Hi all...

    Here is some code which I use to print and align listviews in the package...

    VB Code:
    1. Option Explicit
    2.  
    3. Public Event MaxYReached(ByVal CurrentY As Double, ByRef oPrinter As Object, ByRef bRePrintHeader As Boolean)
    4. Public Event Progress(ByVal oNextListItem As ListItem, ByRef oPrinter As Object, ByRef bRePrintHeader As Boolean, ByVal lTotalLines As Long, ByRef bCancel As Boolean)
    5.  
    6. Private mlFromX                         As Long
    7.  
    8. Public Sub PrintListview(lvListview As ListView, Optional ByVal oPrinter As Object, Optional ByVal bLines As Boolean = False, Optional ByVal bBlackAndWhite As Boolean = True, Optional ByVal MaxY As Double, Optional ByVal lFromX As Long)
    9.     Dim oListItem                       As ListItem
    10.     Dim oSubItem                        As ListSubItem
    11.     Dim oColumnHeader                   As ColumnHeader
    12.     Dim iCounter                        As Integer
    13.     Dim lPrevY                          As Long
    14.     Dim sCorrectPrintText               As String
    15.     Dim bRePrintHeader                  As Boolean
    16.     Dim bCancel                         As Boolean
    17.    
    18.     mlFromX = lFromX
    19.    
    20.     If oPrinter Is Nothing Then
    21.         Set oPrinter = Printer
    22.     End If
    23.    
    24.     With oPrinter
    25.         .Font = lvListview.Font
    26.         Call PrintHeader(lvListview, oPrinter)
    27.    
    28.         .CurrentY = .CurrentY + .TextHeight(" ") + (.TextHeight(" ") / 2)
    29.         For Each oListItem In lvListview.ListItems
    30.             bRePrintHeader = False
    31.             RaiseEvent Progress(oListItem, oPrinter, bRePrintHeader, lvListview.ListItems.Count, bCancel)
    32.            
    33.             If bCancel Then
    34.                 Exit For
    35.             End If
    36.            
    37.             If bRePrintHeader Then
    38.                 Call PrintHeader(lvListview, oPrinter)
    39.             End If
    40.            
    41.             Set oColumnHeader = lvListview.ColumnHeaders(1)
    42.             lPrevY = .CurrentY
    43.             'oPrinter.Line (.CurrentX, .CurrentY)-(.CurrentX + oListItem.Width, .CurrentY)
    44.             .CurrentY = lPrevY
    45.             .CurrentX = .CurrentX - oListItem.Width
    46.            
    47.             Select Case oColumnHeader.Alignment
    48.                 Case lvwColumnLeft
    49.                     .CurrentX = mlFromX + oListItem.Left + 60
    50.                 Case lvwColumnRight
    51.                     .CurrentX = mlFromX + oListItem.Left + oListItem.Width - .TextWidth(oColumnHeader.Text) - 120
    52.                 Case lvwColumnCenter
    53.                     .CurrentX = mlFromX + oListItem.Left + ((oListItem.Width - .TextWidth(oListItem.Text)) / 2)
    54.             End Select
    55.            
    56.             .ForeColor = IIf(bBlackAndWhite, vbBlack, oListItem.ForeColor)
    57.            
    58.             If oListItem.Width > 15 Then
    59.                 sCorrectPrintText = Trim$(oListItem.Text)
    60.                
    61.                 If .TextWidth(sCorrectPrintText) > oColumnHeader.Width - 60 Then
    62.                     Do Until Not .TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120)
    63.                         sCorrectPrintText = Mid$(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
    64.                     Loop
    65.                    
    66.                     sCorrectPrintText = sCorrectPrintText & "..."
    67.                 End If
    68.                
    69.                 Debug.Assert sCorrectPrintText = Trim$(oListItem.Text)
    70.                 oPrinter.Print sCorrectPrintText;       'Because of some weird bug there has to be oPrinter before this
    71.                                                         ' If i dont add the oPrinter i get a syntax error
    72.             End If
    73.            
    74.             .CurrentY = lPrevY
    75.            
    76.             For Each oSubItem In oListItem.ListSubItems
    77.                 Set oColumnHeader = lvListview.ColumnHeaders(oSubItem.Index + 1)
    78.                
    79.                 Select Case oColumnHeader.Alignment
    80.                     Case lvwColumnLeft
    81.                         .CurrentX = mlFromX + oColumnHeader.Left + 60
    82.                     Case lvwColumnRight
    83.                         .CurrentX = mlFromX + oColumnHeader.Left + oColumnHeader.Width - .TextWidth(oSubItem.Text) - 120
    84.                     Case lvwColumnCenter
    85.                         .CurrentX = mlFromX + oColumnHeader.Left + ((oColumnHeader.Width - .TextWidth(oSubItem.Text)) / 2)
    86.                 End Select
    87.                
    88.                 .ForeColor = IIf(bBlackAndWhite, vbBlack, oSubItem.ForeColor)
    89.                
    90.                 If oColumnHeader.Width > 15 Then
    91.                     sCorrectPrintText = Trim$(oSubItem.Text)
    92.                    
    93.                     If .TextWidth(sCorrectPrintText) > oColumnHeader.Width - 120 Then
    94.                         Do Until Not .TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120) Or (sCorrectPrintText = "")
    95.                             sCorrectPrintText = Mid$(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
    96.                         Loop
    97.                         sCorrectPrintText = sCorrectPrintText & "..."
    98.                     End If
    99.                    
    100.                     oPrinter.Print sCorrectPrintText;
    101.                 End If
    102.                
    103.                 .CurrentY = lPrevY
    104.             Next
    105.            
    106.             .CurrentY = .CurrentY + oListItem.Height
    107.            
    108.             If .CurrentY > MaxY Then
    109.                 RaiseEvent MaxYReached(.CurrentY, oPrinter, bRePrintHeader)
    110.                 If bRePrintHeader Then
    111.                     Call PrintHeader(lvListview, oPrinter)
    112.                     .CurrentY = .CurrentY + .TextHeight(" ") + (.TextHeight(" ") / 2)
    113.                 End If
    114.             End If
    115.         Next
    116.     End With
    117. End Sub
    118.  
    119.  
    120. Public Sub PrintHeader(lvListview As ListView, oPrinter As Object)
    121.     Dim lPrevY                          As Long
    122.     Dim oColumnHeader                   As ColumnHeader
    123.     Dim sCorrectPrintText               As String
    124.    
    125.     With oPrinter
    126.         For Each oColumnHeader In lvListview.ColumnHeaders
    127.             If oColumnHeader.Width > 60 Then
    128.                 lPrevY = .CurrentY
    129.                
    130.                 Select Case oColumnHeader.Alignment
    131.                     Case lvwColumnLeft
    132.                         .CurrentX = mlFromX + oColumnHeader.Left + 60
    133.                     Case lvwColumnRight
    134.                         .CurrentX = mlFromX + oColumnHeader.Left + oColumnHeader.Width - .TextWidth(oColumnHeader.Text) - 120
    135.                     Case lvwColumnCenter
    136.                         .CurrentX = mlFromX + oColumnHeader.Left + ((oColumnHeader.Width - .TextWidth(oColumnHeader.Text)) / 2)
    137.                 End Select
    138.                
    139.                 .FontUnderline = True
    140.            
    141.                 If oColumnHeader.Width > 15 Then
    142.                     sCorrectPrintText = Trim$(oColumnHeader.Text)
    143.                    
    144.                     If sCorrectPrintText <> "" Then
    145.                         If .TextWidth(sCorrectPrintText) > oColumnHeader.Width - 120 Then
    146.                             Do Until Not .TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120) Or (sCorrectPrintText = "")
    147.                                 sCorrectPrintText = Mid$(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
    148.                             Loop
    149.                            
    150.                             sCorrectPrintText = sCorrectPrintText & "..."
    151.                         End If
    152.                        
    153.                         oPrinter.Print sCorrectPrintText;
    154.                     End If
    155.                 End If
    156.                    
    157.                 .FontUnderline = False
    158.                 .CurrentY = lPrevY
    159.             End If
    160.         Next
    161.     End With
    162. End Sub

    The code works fine, but only for the first page! Once all the data has filled the first page... it goes mad. All other remaining fields print on seperate pages! It needs to stop after a certain amount of data, in order to go over to another page.

    Any quick idea's please, PLEASE?

    Regards,

    Paul.
    Last edited by VisionIT; Feb 13th, 2003 at 05:31 AM.

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    You need a varible, say lngLine, which tracks what line it's printing on, then when this gets to 50 (Max line per page), it resets it'self to one and you then need to call Printer.newPage...does that make sense?

    Woka

  3. #3

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    Perfect sense M8y...

    But how i'm supposed to include it into that code... i still need to find. Damn good idea though M8, cheers.

    Regards,

    Paul.

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Here's my print code for the SGrid from vbAccelerator...
    VB Code:
    1. Private Function Print_Results(grdPrint As vbalGrid)
    2. Dim intHeader           As Integer
    3. Dim intMargin           As Integer
    4. Dim intPageNo           As Integer
    5. Dim intPrintRow         As Integer
    6. Dim intCounter          As Integer
    7. Dim intColumns          As Integer
    8. Dim intColumnWidth()    As Long
    9. Dim intColumnID()       As Integer
    10. Dim strOutput           As String
    11. Dim intGridWidth        As Integer
    12. Dim sngRatio            As Single
    13. Dim intXpos             As Integer
    14. Dim intPosition         As Integer
    15. Dim intRow              As Integer
    16. Dim intLinesPerPage     As Integer
    17. Dim blnIcon             As Boolean
    18. On Error GoTo ErrorHandler
    19.     Printer.ScaleMode = vbTwips
    20.     Printer.PaperSize = vbPRPSA4
    21.     Printer.Orientation = vbPRORLandscape
    22.     Printer.FontName = "Arial"
    23.     intMargin = 500
    24.     intHeader = 1500
    25.     intLinesPerPage = 45
    26.     With grdPrint
    27.         For intCounter = 1 To .Columns
    28.             If .ColumnVisible(intCounter) And Trim(.ColumnHeader(intCounter) & "") > "" Then
    29.                 intGridWidth = intGridWidth + (.ColumnWidth(intCounter) * Screen.TwipsPerPixelX)
    30.             End If
    31.         Next intCounter
    32.         sngRatio = ((Printer.Width - 2000) / intGridWidth)
    33.         For intCounter = 1 To .Columns
    34.             If .ColumnVisible(intCounter) And Trim(.ColumnHeader(intCounter) & "") > "" Then
    35.                 intColumns = intColumns + 1
    36.                 ReDim Preserve intColumnWidth(1 To intColumns)
    37.                 ReDim Preserve intColumnID(1 To intColumns)
    38.                 intColumnWidth(intColumns) = (.ColumnWidth(intCounter) * Screen.TwipsPerPixelX) * sngRatio
    39.                 intColumnID(intColumns) = intCounter
    40.             End If
    41.         Next intCounter
    42.     End With
    43.     intPrintRow = 0
    44.     intPageNo = 0
    45.     For intRow = 1 To grdPrint.Rows
    46.         intPrintRow = intPrintRow + 1
    47.         If intPrintRow = 1 Then
    48.             intPageNo = intPageNo + 1
    49.             Printer.FontSize = 12
    50.             Printer.FontBold = True
    51.             Printer.FontUnderline = False
    52.             Printer.CurrentX = intMargin - 300
    53.             Printer.CurrentY = intHeader - 1000
    54.             Printer.Print Me.Caption
    55.             Printer.FontSize = 7
    56.             Printer.FontBold = False
    57.             Printer.CurrentY = intHeader - 600
    58.             Printer.CurrentX = intMargin - 300
    59.             Printer.Print "Page " & intPageNo & " of " & ((grdPrint.Rows - (grdPrint.Rows Mod intLinesPerPage)) / intLinesPerPage) + IIf((grdPrint.Rows Mod intLinesPerPage) > 0, 1, 0)
    60.             intXpos = 0
    61.             Printer.FontBold = True
    62.             Printer.FontUnderline = True
    63.             For intCounter = 1 To intColumns
    64.                 Printer.CurrentY = intHeader
    65.                 If intCounter > 1 Then
    66.                     intXpos = intXpos + intColumnWidth(intCounter - 1)
    67.                 End If
    68.                 Printer.CurrentX = intXpos + intMargin
    69.                 strOutput = grdPrint.ColumnHeader(intColumnID(intCounter))
    70.                 intPosition = Len(strOutput)
    71.                 Do While Printer.TextWidth(Left(strOutput, intPosition)) > intColumnWidth(intCounter)
    72.                     intPosition = intPosition - 1
    73.                 Loop
    74.                 If intPosition < Len(strOutput) Then
    75.                     strOutput = Left(strOutput, intPosition - 2) & "..."
    76.                 End If
    77.                 If grdPrint.ColumnAlign(intColumnID(intCounter)) = ecgHdrTextALignCentre Then
    78.                     Printer.CurrentX = Printer.CurrentX + (intColumnWidth(intCounter) - Printer.TextWidth(strOutput)) / 2
    79.                 End If
    80.                 Printer.Print strOutput
    81.             Next intCounter
    82.             Printer.Print
    83.         End If
    84.        
    85.         Printer.FontUnderline = False
    86.         Printer.FontBold = False
    87.         intXpos = 0
    88.         For intCounter = 1 To intColumns
    89.             Printer.CurrentY = intHeader + 250 + (Printer.TextHeight("H") * 1.1 * intPrintRow)
    90.             If intCounter > 1 Then
    91.                 intXpos = intXpos + intColumnWidth(intCounter - 1)
    92.             End If
    93.             Printer.CurrentX = intXpos + intMargin
    94.             If grdPrint.Cell(intRow, intColumnID(intCounter)).IconIndex > 0 Then
    95.                 blnIcon = True
    96.                 strOutput = ""
    97.             Else
    98.                 blnIcon = False
    99.                 strOutput = grdPrint.CellFormattedText(intRow, intColumnID(intCounter))
    100.             End If
    101.             intPosition = Len(strOutput)
    102.             Do While Printer.TextWidth(Left(strOutput, intPosition)) > intColumnWidth(intCounter)
    103.                 intPosition = intPosition - 1
    104.             Loop
    105.             If intPosition < Len(strOutput) Then
    106.                 strOutput = Left(strOutput, intPosition - 3) & "..."
    107.             End If
    108.            
    109.             If blnIcon Then
    110.                 If grdPrint.ColumnAlign(intColumnID(intCounter)) = ecgHdrTextALignCentre Then
    111.                     Printer.CurrentX = Printer.CurrentX + (intColumnWidth(intCounter) - Printer.TextHeight("H")) / 2
    112.                 End If
    113.                 Printer.PaintPicture ilsImages.ItemPicture(ilsImages.ItemKey(grdPrint.Cell(intRow, intColumnID(intCounter)).IconIndex + 1)), Printer.CurrentX, Printer.CurrentY, Printer.TextHeight("H"), Printer.TextHeight("H")
    114.             Else
    115.                 If grdPrint.ColumnAlign(intColumnID(intCounter)) = ecgHdrTextALignCentre Then
    116.                     Printer.CurrentX = Printer.CurrentX + (intColumnWidth(intCounter) - Printer.TextWidth(strOutput)) / 2
    117.                 End If
    118.                 Printer.Print strOutput
    119.             End If
    120.         Next intCounter
    121.        
    122.         If intPrintRow = intLinesPerPage And intRow < grdPrint.Rows Then
    123.             intPrintRow = 0
    124.             Printer.NewPage
    125.         End If
    126.     Next intRow
    127.     Printer.FontBold = True
    128.     Printer.CurrentY = Printer.CurrentY + 400
    129.     strOutput = grdPrint.Rows & " Helpdesk Requests"
    130.     Printer.CurrentX = (Printer.Width - Printer.TextWidth(strOutput) - intMargin) / 2
    131.     Printer.Print strOutput
    132.     Printer.EndDoc
    133.     Exit Function
    134. ErrorHandler:
    135.     Call MsgBox("Failed to print." & vbCr & Err.Number & " : " & Err.Description, vbCritical, "Printer Error")
    136.     Exit Function
    137. End Function
    The varibles are intLinePerPage and intPrintRow...think you can figure how it works

    if you need help give me a shout...

    Woka

  5. #5

  6. #6

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    User-defined type not defined on this line...

    Private Function Print_Results(grdPrint As vbalGrid)

    Damnit

    Any idea's, or is there just a line I could add to the existing code perhaps?

    Regards,

    Paul.

  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Woka's code is not using a listview, but vbAccelerator's SGrid.

    I think your problem is caused by one of the
    .CurrentY = lPrevY
    lines. If the first page is full, the printer switches to the next page, and cannot go back. The line I mentioned causes the printer to go to the last Y position on the new page instead.

    You need to add some code that compares the Y position to the paper's height, and if needed inserts a Printer.NewPage. Don't rely on the printer to go to a new page, you should do it yourself.

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    p, my code is for SGrid, not a ListView, however, the functionality is still exactally the same. Just look at how lngPrintRow is used to set a new page and work out what line you are printing on...

  9. #9

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    Right...

    I understand exactly how the code is working... but the currentY variable you mentioned doesn't monitor line numbers.

    Even if I add my own code to search through it, it keeps doing it every damn time!

    Getting a bit sick of VB now

    Regards,

    Paul.

  10. #10
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    CurretY is the the Y co-ordinate on the paper where I am going to print text...
    VB Code:
    1. If intPrintRow = intLinesPerPage And intRow < grdPrint.Rows Then
    2.             intPrintRow = 0
    3.             Printer.NewPage
    4.         End If
    intPrintRow is the one that records what line I am up to for that page...so if intprintRow = intLinePerPage (max Lines) then it resets and Printer.NewPage is called...
    You should be merely looking for how the intprintRow varible is used, and nothing more...

    Woka

  11. #11

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    Thanks for all the help M8....

    My heads killing me, and i'm far from programming mood! I'll perhaps look at it again 2moro. It seriously looks like crap to me at the moment, and I coded it!

    If i remember correctly... it looked that way anyway!

    If anyone invents a quick-fix button, please inform me!

    Annoying part... is I can perfectly understand the code you've placed, but can't physically place it into the format! I need a headache pill or 5!

    Regards,

    Paul.

  12. #12

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    Ok... headache's gone, back to work!

    Anyone else got any ideas where to stick the printer.newpage line in all my code?

    Regards,

    Paul.

  13. #13
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    It's not hard to transgfer those few varibles across.
    I Have...
    VB Code:
    1. For intRow = 1 To grgGrid.Rows
    2.  
    3. Next intRow
    You have...
    VB Code:
    1. For Each oListItem In lvListview.ListItems
    2.  
    3. Next oListItem
    SO...add the following...
    VB Code:
    1. Dim lngPrintRow   As Long
    2. Dim lngRow   As Long
    3. Dim lngMaxRows   As Long
    4.    lngMaxRows = 40 'This varible sets how many listitems to display per page...setting this too large may cause a few problems with page positioning :(
    5.  
    6. For Each oListItem In lvListview.ListItems
    7.    If lngPrintRow = 0 Then
    8.       PrintHeader lvListview, oPrinter 'I am assuming this prints the column headers on the page...
    9.    End If
    10.    lngPrintRow = lngPrintRow + 1
    11.    lngRow = lngRow + 1
    12.    'All the rest of your code...
    13.  
    14.    If lngPrintRow = lngMaxRows And lngRow <  lvListItem.ListItems.Count Then
    15.       Printer.NewPage
    16.       lngPrintRow = 0
    17.    End If
    18. Next oListItem
    That will print on multiple pages, and add the columns to the top of each page. The code I added includes special text and font formatting, but I stripped that out to make it slightly simpler...

    Hope this helps.

    Woka

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width