Solved

Print listbox contents

Posted on 2006-11-01
14
272 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
Industry Leaders: 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!

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
vb.net winforms sizing/resolution? 4 53
How to limit User Input 2 33
Convert Ctime to date time in textfile? 7 58
Asp.Net Session Question 2 33
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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