Solved

Print listbox contents

Posted on 2006-11-01
14
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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