Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Print listbox contents

I have a listbox (called lstview), that contents SQL query results as below:

Materials            cost1                    cost2
-----------           ------                    ------
Invar -1              $5.00                  $5.20
Invar-2               $2.00                  $6.00


I want to have a button below this listbox, when this button is clicked, I should have the print preview, then option to print the above text out with the same format (3 columns) like that?

Thanks in advance!
0
COASTCOMP
Asked:
COASTCOMP
  • 7
  • 7
1 Solution
 
COASTCOMPAuthor Commented:
I am using VB.net 2003
0
 
vsvbCommented:
this is some way to do this

1) Creat one Form contain one listview on it ( eg. FormShow)
2) in one global variable store your listbox variable ( user datarow collection)
3) On FormShow load event fill the row in listview by using global variable
4) On your main form on printpreview button pass value to global variable and call FormShow
5) code for print preview and print the form  http://www.knowdotnet.com/articles/printform.html

cheers
0
 
COASTCOMPAuthor Commented:
vsvb:
Thank you for your comment. I am very new to vb.net. Would you please show me the code to do so?.
I just want to print out the content in the listbox not the whole form.

Also I already have a form that has the listbox (lstview) in it that shows the query results.

Thanks again.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vsvbCommented:
another way is format your text in Richtextbox ( or use Richtextbox insted)
and print the Richtextbox
follow this code
http://support.microsoft.com/kb/811401
0
 
COASTCOMPAuthor Commented:
vsvb:

Thank you so much for trying to help. The link is a big help but I still don't get it yet.
0
 
vsvbCommented:
This will helps you
first format your list view contain in Multiline Textbox  Then add PrintDocument object to a form
and try this

Private Sub Button1_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles Button1.Click

        Me.PrintDocument1.Print()
End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
        Handles PrintDocument1.PrintPage

        e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Blue,  100, 100)
    End Sub

i am sure you will get success
-cheers

0
 
COASTCOMPAuthor Commented:
vsvb:

I really appreciate your help. What do you mean by formating my list view contain TextBox?
0
 
vsvbCommented:
its mean that transfer your contain of Listbox to Textbox

     Dim i As Integer
        Const vbCrLf As String = Microsoft.VisualBasic.ControlChars.CrLf
        For i = 0 To ListBox1.Items.Count - 1
            TextBox1.Text = TextBox1.Text + vbCrLf + ListBox1.Items.Item(i).ToString
        Next

now use this code

Private Sub Button1_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles Button1.Click

        Me.PrintDocument1.Print()
End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
        Handles PrintDocument1.PrintPage

        e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Blue,  100, 100)
    End Sub
0
 
vsvbCommented:
make sure textbox1 property multiline = true
-cheers
0
 
vsvbCommented:
or try this
Private Sub Button1_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles Button1.Click

        Me.PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
        Handles PrintDocument1.PrintPage
   
       Dim i As Integer
       Dim strs as string
       Const vbCrLf As String = Microsoft.VisualBasic.ControlChars.CrLf
       For i = 0 To ListBox1.Items.Count - 1
              strs =ListBox1.Items.Item(i).ToString +  vbCrLf
       Next
       
      e.Graphics.DrawString(strs , ListBox1.Font, Brushes.Blue,  100, 100)

End Sub


0
 
vsvbCommented:
for 3 listview

 For i = 0 To ListBox1.Items.Count - 1
              strs =ListBox1.Items.Item(i).ToString +  Space(4)  + ListBox2.Items.Item(i).ToString +  Space(4) + ListBox3.Items.Item(i).ToString + vbCrLf
 Next
you have to write Space function accourding to length of your string for same distance between column
vsvb
0
 
COASTCOMPAuthor Commented:
vsvb:

Thank you very much for your help. I will try your code on Monday and keep you posted.

-coastcomp
0
 
COASTCOMPAuthor Commented:
vsvb:

I tried this:

Private Sub Button1_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles Button1.Click

        Me.PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
        Handles PrintDocument1.PrintPage
   
       Dim i As Integer
       Dim strs as string
       Const vbCrLf As String = Microsoft.VisualBasic.ControlChars.CrLf
       For i = 0 To ListBox1.Items.Count - 1
              strs =ListBox1.Items.Item(i).ToString +  vbCrLf
       Next
       
      e.Graphics.DrawString(strs , ListBox1.Font, Brushes.Blue,  100, 100)

End Sub


There is something wrong with the loop. It only printed out the last line in the listbox. Really appreciate if you could figure out.

Thank you so much,

coastcomp
0
 
COASTCOMPAuthor Commented:
vsvb:

I got it to work by changing strs+=ListBox1.Items.Item(i).ToString +  vbCrLf

There is another problem. That is the contents in the listbox is more than one page long. This code does not know how to add more pages.
Please help if you could.

Thanks,

Coastcomp.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now