Results 1 to 19 of 19

Thread: I need some help on printing reports

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Location
    BC,Canada
    Posts
    15

    I need some help on printing reports

    Can someone help me to create a basic print form a database without using crystal report(i don't have it)
    i'm using an access database.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Does only the Visual Studio version come with Crystal Reports? I thought they all did.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Location
    BC,Canada
    Posts
    15
    I got vb .net standard edition and i can't find it any where

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That sucks you've been robbed. I've never done a report like that in .NET but there is an article in the new Visual Studio Magazine about it. You have to use the System.Drawing.Printing namespace. What part are you stuck on?

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Location
    BC,Canada
    Posts
    15
    I just need to ge started and then i will go from there.

    ps is there any way to get crystal reports

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can buy it they just came out with version 9, but I think it is rather expensive.

  7. #7
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    The basic printing functions built into Visual Studio are very primitive. While I've managed to do things like printing two columns of plain text (example at http://www.vbforums.com/showthread.p...hreadid=204202) to do very much more than that requires extending the builtin print handler, which is a little bit beyond me at the moment. I found a DLL for a custom control that extends the RichTextBox and builds in a more functional print handler, but it only prints in one column and is probably not all that useful for doing tabular reports. If you want to try it out it's attached with source.
    Attached Files Attached Files

  8. #8
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Actually it seems there's an API function that prints formatted text but I've never messed with API calls from VB (guess now is as good a time as any to learn how heh)

    ms-help://MS.VSCC/MS.MSDNVS/gdi/fontext_0odw.htm

    The Graphics.DrawString method that's part of the .NET framework doesn't seem to support embedded/mixed formatting:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDrawingGraphicsClassDrawStringTopic.htm

  9. #9
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Actually it seems that DrawText does the same thing as the .NET method DrawString (omits formatting). As the guy who wrote the above DLL says, it looks like EM_FORMATRANGE message is what you need to invoke in order to render mixed format text?

    Why is this so hard? Why isn't the print handler a little less dinky? Friggin Wordpad has this built into it. *grumble*

    Ah crikey this explains a LOT:
    http://support.microsoft.com/default...EN-US;Q146022&
    Last edited by Slow_Learner; Oct 10th, 2002 at 06:15 AM.

  10. #10
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I know this is not ideal but it should work.

    If you have Access then why not create the report in Access.
    Then create a Macro which opens access at that report and call access using the shell.

    Parksie

  11. #11
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Well for me that won't do, as some of my users won't have Access. Maybe the original poster will be happy though

  12. #12
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Why not try saving your info in a text file and then opening it up in a rich text box?

    I have not tried it myself but it certainly seems feasable.

    Parksie

  13. #13
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Getting it into a richtext and formatting it there isn't the problem, I'm already able to do that - it's getting it to the printer with the formatting intact. All the Graphics.DrawString method seems to be able to do is draw text in one font, one style, one point size. You're allowed to use tabs and returns but that's pretty much the only formatting it will interpret. Font changes, point size changes and underline/bold/italic state changes are ignored.

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Can't you pass a font object into the Drawstring method?

    I was just reading this in 'Programming in VB.NET.:
    VB Code:
    1. dim font1 as New Font("Arial",12)
    2. dim font2 as New Font("Arial",14,FontStyle.Bold)
    3. dim font3 as New Font("Arial",16,FontStyle.Italic Or FontStyle.Underline)
    4.  
    5. dim gr as Graphics=Me.CreateGraphics
    6. gr.DrawString("Arial 12 Regular",font1,Brushes.Black,20,20)
    7. gr.DrawString("Arial 14 Bold",font2,Brushes.Black,20,60)
    8. gr.DrawString("Arial 16 Italic and Underline",font3,Brushes.Black,20,100)
    9.  
    10. font1.Dispose()
    11. font2.Dispose()
    12. font3.Dispose()

  15. #15
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Yes you can - but I haven't seen a way to print mixed-format text in a rectangle with font changes, point size changes etc. *reads pages 919-926 a little closer than last time* maybe I just didn't understand it before.

    ...

    Yeah my problem with that way of doing things is that in order to go to the printer without scrutinizing the hell out of each and every character, I'd like to use the string measurement method Graphics.MeasureString - where I'm out of ideas is getting the text, with embedded formatting information like size change, underline/bold/font changes etc., from a RichTextBox into MeasureString and DrawString. The Programming VB .NET book examples all deal with just drawing text on a form, and there's no concern for how the text will wrap or clip (what MeasureString handles). I dunno, maybe there's a simple way to get text out of the RichTextBox and into DrawString with formatting intact, but I just don't see it.
    Last edited by Slow_Learner; Oct 11th, 2002 at 02:02 AM.

  16. #16
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Actually I think it may be a mistake to try to shoehorn RichTextBoxes into my printing scheme. Here's what I'm using right now:

    Code:
        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim iCharsLeftColumn, iCharsRightColumn As Integer
            Dim iLinesLeftColumn, iLinesRightColumn As Integer
            Dim strForPageLeftColumn, strForPageRightColumn As String
            Dim fmtStringFormatLeft As New StringFormat()
            Dim fmtStringFormatRight As New StringFormat()
    
            Dim iTabStopsLeft() As Single = {50, 50, 50, 50, 50, 50}
            fmtStringFormatLeft.SetTabStops(0, iTabStopsLeft)
            Dim iTabStopsRight() As Single = {220, 50, 50}
            fmtStringFormatRight.SetTabStops(0, iTabStopsRight)
    
    
            Dim rectLeftColumn As New RectangleF( _
                e.MarginBounds.Left, e.MarginBounds.Top, _
                (e.MarginBounds.Width / 2) - 10, e.MarginBounds.Height)
            Dim rectRightColumn As New RectangleF( _
                (e.MarginBounds.Width / 2) + 90, e.MarginBounds.Top, _
                (e.MarginBounds.Width / 2) - 10, e.MarginBounds.Height)
    
            Dim sizeMeasure As New SizeF((e.MarginBounds.Width / 2) - 10, _
                e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
    
            fmtStringFormatLeft.Trimming = StringTrimming.Word
            fmtStringFormatRight.Trimming = StringTrimming.Word
    
            e.Graphics.MeasureString(strPrintLeftColumn, PrintFont, sizeMeasure, _
                fmtStringFormatLeft, iCharsLeftColumn, iLinesLeftColumn)
            e.Graphics.MeasureString(strPrintRightColumn, PrintFont, sizeMeasure, _
                fmtStringFormatRight, iCharsRightColumn, iLinesRightColumn)
    
            strForPageLeftColumn = strPrintLeftColumn.Substring(0, iCharsLeftColumn)
            strForPageRightColumn = strPrintRightColumn.Substring(0, iCharsRightColumn)
    
    
            e.Graphics.DrawString(strForPageLeftColumn, PrintFont, Brushes.Black, rectLeftColumn, _
                fmtStringFormatLeft)
            e.Graphics.DrawString(strForPageRightColumn, PrintFont, Brushes.Black, rectRightColumn, _
                fmtStringFormatRight)
    
            If iCharsLeftColumn < strPrintLeftColumn.Length Then
                strPrintLeftColumn = strPrintLeftColumn.Substring(iCharsLeftColumn)
            Else
                strPrintLeftColumn = rtxtPrintLeftColumn.Text
            End If
    
            If iCharsRightColumn < strPrintRightColumn.Length Then
                strPrintRightColumn = strPrintRightColumn.Substring(iCharsRightColumn)
            Else
                strPrintRightColumn = rtxtPrintRightColumn.Text
            End If
    
            If iCharsLeftColumn < strPrintLeftColumn.Length Or _
            iCharsRightColumn < strPrintRightColumn.Length Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
        End Sub
    Before the print event is fired I've been able to get text into the two richtextboxes and get it formatted the way I like it (with underlines and bolds and whatnot). The downside is that the formatting is lost (which makes sense as I'm looking at the RichTextBox.Text property to get the text to work with, and that appears to just give a string as the docs say it does.)

    While this doesn't look super clean (at least it doesn't to me) it's a lot cleaner than anything I can envision if I didn't render a whole page's worth of column at a time - I think I might be able to work out a way to keep track of how much of a column I've used and draw the text line-by-line to the graphics object of the printdocument, but that seems MESSY AS HELL and I'm not very eager to do it like that if there's a more elegant solution.

  17. #17
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Actually I think I've found an example of printing line-by-line that I can adapt to printing two columns:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDrawingPrintingPrintDocumentClassPrintTopic.htm

    Doesn't look all that terribly threatening...

    ...

    Nope this way is bloody awful compared to figuring out how EM_FORMATRANGE works I think. Oh well, at least I have that C# example to try to adapt.
    Last edited by Slow_Learner; Oct 11th, 2002 at 03:24 AM.

  18. #18
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    In the hope that someone is helped (I feel sorry for the original poster for hijacking his thread so badly) please feel free to use this extended control:

    http://www.vbforums.com/showthread.p...hreadid=205078

  19. #19
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Here is an example from microsoft about printing in .NET.

    http://msdn.microsoft.com/code/default.asp?url=/code/.../msdncompositedoc.xml

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