Results 1 to 23 of 23

Thread: Print Listview Icons?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Print Listview Icons?

    Hello,

    someone who can help me please
    I have a code for a listview printing
    but the problem is that he don't print the icons.
    And this I never have to test until now
    I can add anything like a color and other stuff but I do not know how to solved this.

    please anyone?

    Code:
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            'Headings   
            currentY = 100
            For Each c In lstView.Columns
                maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
                colLefts(idx) = l
                colWidths(idx) = c.Width
                lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
               
                If lr.Width > 0 Then g.DrawString(c.Text, f, b, lr)
                l += c.Width
                idx += 1
            Next
            currentY += maxY + gap
            g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
            currentY += gap
            'Rows   
            iCount = lstView.Items.Count - 1
    
                l = 0
                maxY = 0
                idx = 0
                For Each lvsi In lstView.Items(ii).SubItems
                    maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
                    lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
                    If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
                    idx += 1
                Next
                currentY += maxY + gap
            Next
    Dummy1912
    Attached Images Attached Images  

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    If you want to draw an Icon you will call the DrawIcon method of the Graphics object. You call it right before calling DrawString to draw the text, then move the text the appropriate amount to the right.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    hello,

    can you give me a example please
    i never used drawicon.

    thanks

    dummy1912

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    Quote Originally Posted by Dummy1912 View Post
    i never used drawicon.
    Then the first thing you should do is read the documentation for that method. As is more often the case than not, the documentation contains a code example. If you can't get it to work after that then post back but you should always look first, ask questions later.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    don't worry
    i always look first before i ask my question

    im now looking for drawicon.

    i let you know.

    thanks for your answers

    dummy1912

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    hello

    i found something

    is this what you talk about?

    http://msdn.microsoft.com/en-us/library/d6s01xzx.aspx

    but i don't see how i can use it in my listview
    because i use a imagelist
    and he load a function and read it from the database
    if the value is true

    it will set the first icon from the imagelist
    if false
    the second one from the imagelist.

    dummy1912

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    Actually, given that the ImageList contains Image objects rather than icon objects, you'd call DrawImage rather than DrawIcon. Not the same but very similar. Sorry about the bum steer. You will use the item's ImageIndex to get the appropriate Image from the ImageList and pass it to DrawImage.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    no problem

    okay ill find and search somethings.

    i don't know if it gonna be easy

    dummy1912

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    I have found something again.

    is this what you mean?

    Code:
    AddHandler listView1.DrawSubItem, AddressOf Me.listView1_DrawSubItem
    Dim DrawSubItem As The
    
        
        Private Event : As handler
        
        Private Sub listView1_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs)
            'specify the lisview column
            If (e.ColumnIndex = 1) Then
                'draw the subitem with the same background color.
                e.DrawBackground
                'set the image from the imagelist associated with the ListViewItem
                e.Graphics.DrawImage(e.Item.ImageList.Images(e.Item.ImageIndex), e.SubItem.Bounds.Location)
            End If
        End Sub
    dummy1912

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    Quote Originally Posted by Dummy1912 View Post
    no problem

    okay ill find and search somethings.

    i don't know if it gonna be easy

    dummy1912
    It is going to be easy. It's a simple If statement to determine which index to use, get the Image and pass it to DrawImage, which will be much like DrawString. Give it a go and post back what you've done if you get stuck, so we can help you fix it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    it didn't work

    what is wrong?

    Code:
    Private Sub listView1_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs)
            'specify the lisview column
            If (e.ColumnIndex = 1) Then
                'draw the subitem with the same background color.
                e.DrawBackground()
                'set the image from the imagelist associated with the ListViewItem
                e.Graphics.DrawImage(e.Item.ImageList.Images(e.Item.ImageIndex), e.SubItem.Bounds.Location)
            End If
        End Sub

    code for printing listview:

    Code:
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            AddHandler LstView.DrawSubItem, AddressOf Me.listView1_DrawSubItem
    
    
            'Headings   
            currentY = 100
            For Each c In lstView.Columns
                maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
                colLefts(idx) = l
                colWidths(idx) = c.Width
                lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
               
                If lr.Width > 0 Then g.DrawString(c.Text, f, b, lr)
                l += c.Width
                idx += 1
            Next
            currentY += maxY + gap
            g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
            currentY += gap
            'Rows   
            iCount = lstView.Items.Count - 1
    
                l = 0
                maxY = 0
                idx = 0
                For Each lvsi In lstView.Items(ii).SubItems
                    maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
                    lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
                    If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
                    idx += 1
                Next
                currentY += maxY + gap
            Next

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    What's the point here? It's to print the icon with each item, correct? So shouldn't the code to draw the images be in the PrintPage event handler? They aren't going to get printed otherwise. I said back in post #2:
    You call it right before calling DrawString to draw the text, then move the text the appropriate amount to the right.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    yes correct only there are 2 icons to use in the listview.

    i have relocate the code
    and add it in the printpage

    but i get a error

    'Object reference not set to an instance of a object.

    I don't get it.

    i did:

    Code:
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
                'Headings   
            currentY = 100
            For Each c In lstView.Columns
                maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
                colLefts(idx) = l
                colWidths(idx) = c.Width
                lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
               
                If lr.Width > 0 Then g.DrawString(c.Text, f, b, lr)
                l += c.Width
                idx += 1
            Next
            currentY += maxY + gap
            g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
            currentY += gap
            'Rows   
            iCount = lstView.Items.Count - 1
    
    If (re.ColumnIndex = 0) Then
                    'draw the subitem with the same background color.
                    re.DrawBackground()
                    'set the image from the imagelist associated with the ListViewItem
                    re.Graphics.DrawImage(re.Item.ImageList.Images(re.Item.ImageIndex), re.SubItem.Bounds.Location)
                End If
    
                l = 0
                maxY = 0
                idx = 0
                For Each lvsi In lstView.Items(ii).SubItems
                    maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
                    lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
                    If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
                    idx += 1
                Next
                currentY += maxY + gap
            Next

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    I don't know what i do wrong
    i have add it on every line where drawstring is and no place is good.

    and still not working

    didn't know is was so difficult.

    Dummy1912

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    If there are two icons to draw then you'll need to call DrawImage twice, but I only see it once in that code. Basically you need to call DrawString for each subitem you want to draw, and call DrawImage before each of those you want an image in front of.

    When something doesn't work, take a step back and start simple, then build it up. Start by printing just the first image. Once that works, add the first subitem text. Once that works, add the second image. Once that works... etc.

    As for your error, please always point out what line it occurs on. Otherwise we have to spend time trying to work out where the error is before we can even think about working what caused it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    this is the error:

    System.NullReferenceException was unhandled by user code
    Message="Object reference not set to an instance of an object."
    Source="Bill_Payments_System"
    StackTrace:
    at Bill_Payments_System.Form1.PrintDocument11_PrintPage(Object sender, PrintPageEventArgs e) in G:\Drive D\Bill1\Bill1\Form1.vb:line 85
    at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)
    at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
    at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
    at System.Drawing.Printing.PrintController.Print(PrintDocument document)
    at System.Drawing.Printing.PrintDocument.Print()
    at System.Windows.Forms.PrintPreviewControl.ComputePreview()
    at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()
    InnerException:

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    every time i set this

    Code:
            If (re.ColumnIndex = 0) Then
                'draw the subitem with the same background color.
                re.DrawBackground()
                'set the image from the imagelist associated with the ListViewItem
                e.Graphics.DrawImage(re.Item.ImageList.Images(re.Item.ImageIndex), re.SubItem.Bounds.Location)
            End If
    i get that error?

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    That doesn't tell us which line of your code throws the exception. We don't know which is line 85. The IDE will highlight the current line when an exception is thrown. You need to let us know which line that is.

    That said, I still say you should just comment that code out and start building it up one part at a time. If you try to do too much in one go and something goes wrong, it's difficult to determine what part the issue is in. If you only implement small parts at a time then it's easy to determine where the issue is when one occurs because you know that what went before works.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    the error stuck here

    re.ColumnIndex = 0

    i get a yellow arrow.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    Then you haven't assigned anything to 're'. You can't get the ColumnIndex of Nothing. Did you actually mean to use 'e' rather than 're'?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Re: Print Listview Icons?

    i have this

    Dim re As DrawListViewSubItemEventArgs

    because

    i can't use

    If (e.ColumnIndex = 1) Then
    'draw the subitem with the same background color.
    e.DrawBackground()
    'set the image from the imagelist associated with the ListViewItem
    e.Graphics.DrawImage(e.Item.ImageList.Images(e.Item.ImageIndex), e.SubItem.Bounds.Location)

    End If

    because the 'e' is already exist in print_page

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Print Listview Icons?

    You don't conjure a DrawListViewSubItemEventArgs yourself. One gets passed to you in the DrawListViewSubItem event handler, but you aren't handling that event because you aren't drawing a ListViewSubItem. You're handling the PrintPage event because you're printing a page.

    I'll say this one last time: you need to stop, take a step back and start simple because you've wandered down a road that doesn't work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    31

    Red face Re: Print Listview Icons?

    i don't know it anymore

    i even found this:

    Code:
    e.Graphics.DrawImage(yourimage.Image, posX,posY,width,height)
    but this is not working.

    i need to load the current icons from the imagelist
    and get them right in the listview like before you print it.
    it just i can't find anything about this
    to print the icon's

    i only found the string to print but nothing about the icons

    please nobody that know the answer?



    Dummy1912

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width