Hello,

I am trying to write a program to display system information. I have that part down. I am using a listbox, so that I can have different lines, for example:

ListBox1.Items.Add("Computer Name: " & My.User.Name)
        ListBox1.Items.Add("OS Version:" & My.Computer.Info.OSVersion)

I am running into problems when I try to print. I have added the following code, per an instruction manual:

Private printpagesettings As New PageSettings
    Private stringtoprint As String
    Private printfont As New Font("Arial", 10)

and

Private Sub PrintInformationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintInformationToolStripMenuItem.Click
        Try
            ListBox1.SelectedItems.ToString()
            PrintDocument1.DefaultPageSettings = printpagesettings
            stringtoprint = ListBox1.SelectedItems.ToString
            PrintDialog1.Document = PrintDocument1
            Dim result As DialogResult = PrintDialog1.ShowDialog
            If result = DialogResult.OK Then
                PrintDocument1.Print()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

However, when I go to print, and select both values from my list box, it will only print one line, not both. Any thoughts would be greatly appriciated. Thanks!

Hi

in the last sectio where you try to print, you set: stringtoprint = ListBox1.SelectedItems.ToString

It is going to return only the selected item. You have to add all the items first, something like this:

Dim listbox_items As Integer = ListBox1.Items.Count()
        Dim i As Integer

        For i = 0 To listbox_items - 1
            Dim stringtorpint As String
            stringtorpint &= ListBox1.Items.Item(i)
        Next

You might have to add sometyhing else in between (like a CR or something) so it will print every item in a different line.

Hope it helps.

Hey, sorry to bug you again, but it didn't work. I tried adding it to the PrintInformationToolStripMenuItem_Click sub, but it didn't work. All my printer did was send a blank page through. If I declair anything in your code inside of the try tags, then the rest of the print code won't recognize those declairations. Now even when I remove the new code, I'm getting a blank page. Totally confused right now! :) Thanks so much!!!

Ok, now I changed you line of code:

stringtoprint = ListBox1.Items.Item(i)

to:

stringtoprint = ListBox1.Items.Item(i) - 1

Now I don't get an error that says invalid value="3" for type 'index'. I instead get an error that says that conversion from string ="os name: windows x" to type 'double' is not valid. I don't have dobule declaired anywhere, so I don't know why it is using double. I am farther than I was, though. Thanks!

Hi,

Try using ListBox1.Items.Text to get all the listbox items ina single string separated by CRLF.

Loren Soth

This article has been dead for over six months. Start a new discussion instead.