CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    2

    C# Noob Question about printing

    I have created a form, added 3 or 4 text boxes, and all i want to do is print the text that shows up in the form textboxes. I can find samples of printing the entire form "graphically" but that is not what i need. I just need the most basic method of printing the text out of a basic form.

    Thank you

  2. #2
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: C# Noob Question about printing

    Hello Coder 13.

    Regrettably, I know of no "basic method of printing the text out of a basic form", printing remains a labor-intensive endeavor (I THINK! I am most assuredly not an expert). It involves, at the very least, instantiating and supporting a PrintDocument, and at least one event handler (of several available to the user)

    Some time ago I wrote a print DLL which, after seeing your note, I recovered from the dank recesses of the Code Dungeon, dusted it off and ran it through VS2010. I then created a new project and copied part of the DLL into the project code, fooled with it, and managed to get it running.

    Incidently, the DLL had only the most RUDIMENTARY testing, (and the code I extracted from the DLL and placed into this project suffered some careless editing during the transfer between DLL and project) so I'd avoid anything fancy, but I think it'll work OK for simple text. At the very least, it'll get ya started. It's workin' here.

    I am going to attempt to attach the two files: the first is the print-related code ripped from the DLL code, while the second is the code that drives that print-related code. I expect that you will be able to download the attached files, compile'em and let'er rip. It runs here on this lowly SONY Vaio running Windows XP and VS2010. I haven't tried it on any of the other machines.

    Now, a word of caution, the attached code is probably more than you asked for or wanted. It provides font-selection dialog and printer-selection dialog. If you wish, you can rip that code outta there. Also, it provides a header and footer on the printed page where the header provides the computer name and job name, date and time. The footer consists solely of the page number but you can modify that as you see fit - it's easily done.

    If you compile and execute, I expect that it will display 27 lines of text comprising the string "line number " and the line number. You wanted basic ? you GOT basic !

    Your job will be to go into function "userProvideLinesMethod" in file "MainTestPage" (the test driver code) and modify the code to read your text boxes, or accept a string[] or whatever you intend to do instead of just printing 27 similar lines.

    I expect that you will be able to drop these two files into a project, compile and run without any changes to establish a functional baseline. What you do after that is up to you.

    Also, you will almost certainly wanna whittle away at the print-related file, reducing it to something more manageable. There's more there than you asked for.

    OK, I'm gonna attempt to attach files. Hang on....

    OK, I think they're there: MainTestPage.cs is the test driver. prtAssy.cs is the print-related code.
    Give it a try. No guarantees, but it works here. And, if nothing else, it's worth exactly what you paid for it.


    bill
    Last edited by ThermoSight; January 8th, 2011 at 08:22 PM.

  3. #3
    Join Date
    Jan 2011
    Posts
    2

    Re: C# Noob Question about printing

    Thank you very much for the reply. I guess the whole printing thing is going to take some time to learn. Your assistance is very appreciated.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: C# Noob Question about printing

    Quote Originally Posted by ThermoSight View Post
    Hello Coder 13.

    Regrettably, I know of no "basic method of printing the text out of a basic form", printing remains a labor-intensive endeavor (I THINK! I am most assuredly not an expert). It involves, at the very least, instantiating and supporting a PrintDocument, and at least one event handler (of several available to the user)

    Some time ago I wrote a print DLL which, after seeing your note, I recovered from the dank recesses of the Code Dungeon, dusted it off and ran it through VS2010. I then created a new project and copied part of the DLL into the project code, fooled with it, and managed to get it running.

    Incidently, the DLL had only the most RUDIMENTARY testing, (and the code I extracted from the DLL and placed into this project suffered some careless editing during the transfer between DLL and project) so I'd avoid anything fancy, but I think it'll work OK for simple text. At the very least, it'll get ya started. It's workin' here.

    I am going to attempt to attach the two files: the first is the print-related code ripped from the DLL code, while the second is the code that drive s that print-related code. I expect that you will be able to download the attached files, compile'em and let'er rip. It runs here on this lowly SONY Vaio running Windows XP and VS2010. I haven't tried it on any of the other machines.

    Now, a word of caution, the attached code is probably more than you asked for or wanted. It provides font-selection dialog and printer-selection dialog. If you wish, you can rip that code outta there. Also, it provides a header and footer on the printed page where the header provides the computer name and job name, date and time. The footer consists solely of the page number but you can modify that as you see fit - it's easily done.

    If you compile and execute, I expect that it will display 27 lines of text comprising the string "line number " and the line number. You wanted basic ? you GOT basic !

    Your job will be to go into function "userProvideLinesMethod" in file "MainTestPage" (the test driver code) and modify the code to read your text boxes, or accept a string[] or whatever you intend to do instead of just printing 27 similar lines.

    I expect that you will be able to drop these two files into a project, compile and run without any changes to establish a functional baseline. What you do after that is up to you.

    Also, you will almost certainly wanna whittle away at the print-related file, reducing it to something more manageable. There's more there than you asked for.

    WHEN questions arise, you can reach me at lREMOVED

    OK, I'm gonna attempt to attach files. Hang on....

    OK, I think they're there: MainTestPage.cs is the test driver. prtAssy.cs is the print-related code.
    Give it a try. No guarantees, but it works here. And, if nothing else, it's worth exactly what you paid for it.


    bill
    REMOVE your email from the listing, to prevent SPAM
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: C# Noob Question about printing

    hello again Coder13.

    yeah, printing remains a labor-intensive endeavor, though apparently less so than it once was.

    Indeed, on page 496 of "Microsoft .NET Framework 3.5 - Windows Forms Application Development (MCTS exam 70-505)", one of the two authors (Matt Stoecker) writes:

    "The process of creating print documents can be frustrating and difficult. Before the .NET Framework was introduced, I found the process of actually getting a document to the printer to be so difficult that I avoided it whenever I could. With the introduction of the .NET Framework, printing is now easy to code and it is easier to create added value in the applications I create".


    There is quite a bit in the print-related file that is unnecessary for your modest requirements and may be stripped away .... I left that task to you. (for example, extra constructors, properties, unnecessary class members, font support, etc). The file I provided was just something to get you rollin'.

    In the project here at my desk, I have a form with a single control, the "GO" button, and the handler has a single line: "InitiatePrintJob();" that's what starts the whole chain of events.

    Best wishes.

    bill

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: C# Noob Question about printing

    I've loaded a copy of a RTB file, with [x]-[xxx] to substitute whatever I wanted. I numbered them, then looped thru, replacing each with the appropriate text. Then, giving a print preview. That was VB6!

    Program calculated data related to the construction industry, calculated prices, displayed an image, and then PRINTED a hard-copy receipt.

    You need to design the data before you can grab it, in most cases.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured