How do you print the contents of a list box?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: How do you print the contents of a list box?

  1. #1
    Join Date
    Apr 2009
    Posts
    6

    Question How do you print the contents of a list box?

    I would be very grateful if someone could recommend some code which would allow me to print the contents of a listbox in VB6. I would ideally like to call the standard ‘print dialog box’, however this is not vital.

    One big complication could involve the fact that I am populating my listbox ‘lst_Bookings’ using the following subroutine:

    [I]Sub DisplayBookings(e)
    'The value 'b' is defined as an integer
    Dim b As Integer
    'BookingData is defined as a string
    Dim BookingData As String
    'The bookings list is cleared so that only the records for the appropriate event are displayed
    lst_Bookings.Clear
    Code:
    For b = 1 To BookingCount
      With Booking(b)
        'If the 'Event_ID' value = to 'e' the booking data is displayed in the list box
        If .Event_ID = e Then
            BookingData = .Ticket_No & "  " & .Student_ID & " " & .Booking_Date
            'The booking data is added to the list box
            lst_Bookings.AddItem BookingData
       End If
       
      End With
    Next b
    This is due to the fact that my data is being stored in a file, not an array, and also because a combo box selection is determining what information is displayed. All my variables are defined in a module, as they need to be used by more than one program.

    Thanks!

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    It really doesn't matter how the listbox is populated.

    After it is, this will print it
    Code:
    Private Sub Command1Click()
    Dim i As Long
        
        For i = 0 To List1.ListCount - 1
            Printer.Print List1.List(i)
        Next
        
        Printer.EndDoc
    
    End Sub
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Apr 2009
    Posts
    6

    Thumbs up

    Thanks for the code, it works a treat!

    Is there any way I could combine it with the print dialog box so that the user could choose a printer?

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Put a common dialog control on the form.
    Code:
    Private Sub Command1_Click()
    On Error GoTo ErrTrap
    Dim i As Long
       With CommonDialog1
            .DialogTitle = "Page Setup"
            .CancelError = True
            .ShowPrinter
        End With
        For i = 0 To List1.ListCount - 1
            Printer.Print List1.List(i)
        Next
        Printer.EndDoc
    Exit Sub
    ErrTrap:
    If Err.Number = 32755 Then
       MsgBox "Print Job Cancelled."
    End If
    End Sub
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    Apr 2009
    Posts
    6
    VB crashes when I try to load the 'common dialog' control onto my toolbar.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Well, thats a first for me.

    What happens? What does "crashes" mean?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    Join Date
    Apr 2009
    Posts
    6
    I don’t currently have the ‘Common Dialog’ Control loaded on my toolbox. In order to add it, I am selecting my toolbox by right clicking, navigating to ‘components’, before selecting the ‘Microsoft Common Dialog Control 6.0’ on the list of controls. When I then click ‘Apply’, VB comes up with an error which reads: ‘Visual Basic has encountered a problem and needs to close. We are sorry for the inconvenience.’ The program then crashes.

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What version of VB6 are you using?

    Are you up to date on the service packs?

    Did your VB6 get installed from a valid, factory, Visual Basic CD?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  9. #9
    Join Date
    Apr 2009
    Posts
    6
    VB6 Enterprise Edition, version 8176; I’m using a college library machine, so I haven’t got the access rights to install anything.


    (Actually, the library is closing now, so I’ll be back on the forum again tomorrow.)

    Many thanks for your time and help!

  10. #10
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Then you need to report this issue to the folks in charge of maintaining the Library computers.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  11. #11
    Join Date
    Apr 2009
    Posts
    6
    Ok, will do that. Thanks anyway!

  12. #12
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Try a different computer in the Library...it may be just a problem with the VB on that one individual machine.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. SelectedItem in a List box
    By admol in forum ASP.NET
    Replies: 1
    Last Post: 03-09-2007, 09:44 AM
  2. List Box
    By Scott in forum ASP.NET
    Replies: 1
    Last Post: 12-19-2001, 02:12 AM
  3. Replies: 0
    Last Post: 08-02-2001, 09:37 PM
  4. data-bound list box
    By John in forum VB Classic
    Replies: 0
    Last Post: 03-19-2001, 09:53 PM
  5. Replies: 2
    Last Post: 03-24-2000, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center