Solved

Print listbox contents

Posted on 2006-11-01
14
267 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now