Solved

Print listbox contents

Posted on 2006-11-01
14
270 Views
Last Modified: 2008-02-01
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
Comment
Question by:COASTCOMP
  • 7
  • 7
14 Comments
 

Author Comment

by:COASTCOMP
ID: 17855638
I am using VB.net 2003
0
 
LVL 3

Expert Comment

by:vsvb
ID: 17858326
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
 

Author Comment

by:COASTCOMP
ID: 17859025
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 3

Expert Comment

by:vsvb
ID: 17875986
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
 

Author Comment

by:COASTCOMP
ID: 17884956
vsvb:

Thank you so much for trying to help. The link is a big help but I still don't get it yet.
0
 
LVL 3

Expert Comment

by:vsvb
ID: 17907520
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
 

Author Comment

by:COASTCOMP
ID: 17908582
vsvb:

I really appreciate your help. What do you mean by formating my list view contain TextBox?
0
 
LVL 3

Expert Comment

by:vsvb
ID: 17914034
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
 
LVL 3

Expert Comment

by:vsvb
ID: 17914046
make sure textbox1 property multiline = true
-cheers
0
 
LVL 3

Expert Comment

by:vsvb
ID: 17916504
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
 
LVL 3

Accepted Solution

by:
vsvb earned 150 total points
ID: 17916532
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
 

Author Comment

by:COASTCOMP
ID: 17926508
vsvb:

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

-coastcomp
0
 

Author Comment

by:COASTCOMP
ID: 17927908
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
 

Author Comment

by:COASTCOMP
ID: 17927994
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question