Solved

code for printing entire form

Posted on 2006-06-16
7
370 Views
Last Modified: 2010-05-18
Hello,

I want to print an entire form. I have looked at the other examples on here but i dont have the PrintForm function when i type Me.

Do i have to import anything?
Bryan
0
Comment
Question by:bryanford
  • 3
  • 3
7 Comments
 
LVL 20

Expert Comment

by:alainbryden
ID: 16925458
IF you want to display it, then just loop through each of the form contents with a msgbox, but I'm sure this isn't what you wanted to do or you wouldn't have asked.

IF by print form you mean to use an actual printer, than you need to import the common dialogue box in your components. This is very simple to do, and then you just create a new common dialogue, set the type as PrintDialoge, and then you're off.

If all you want to do is to take a snapshot of a form, like a screenshot type thing, then you need to simulate the pressing of Alt+PrintScreen on the keyboard.  To do this you do the following:

Declare Function keybd_event Lib "user32" Alias "keybd_event" _
     (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, _
     ByVal dwExtraInfo As Long) As Long

so that you can do a keyboard event. THen when you want the screenshot:

keybd_event(VK_Alt, 0, 0, 0) 'send a key
keybd_event(VK_PrintScreen, 0, 0)
keybd_event(VK_Alt, 0, KEYEVENTF_KEYUP, 0) 'release key

Then to store it something like:

frmcontrols.snapImage1.Picture = Clipboard.GetData(vbCFBitmap)
0
 
LVL 1

Author Comment

by:bryanford
ID: 16925463
sorry i should have been more specific.

I want to print it to a printer, but dont want the title bar or border, and sometimes its scrollable so I want it to also print out what you cant see without scrolling.

I am using visual studio 2005 in vb.net language.

Thanks
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 16925560
Ah for starters you'll need to put everything you want into a listbox or a textbox. You need to defined exactly what format to print in. Probably the easiest way to do this would be to export data to a file, or spreadsheet, depending on the format, and then call the command for a print dialogue box for those. It's as easy as ExcelApp.Print
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:bryanford
ID: 16926350
all that is on my form is a bunch of labels. and this program is to be run on a pc without any software installed.
0
 
LVL 20

Accepted Solution

by:
alainbryden earned 75 total points
ID: 16926881
well, no softoware other than your own ;)

Okay, so looks like you need the printer dialogue box.

For example you could Create a rich text box and add all the label's to it in the order and format you want then: use  RichTextBox1.SelPrint (Printer.hDC) to print everything

http://www.samspublishing.com/library/content.asp?b=STY_VB6_24hours&seqNum=124
this will tell you how to get a dialogue box on your form. You need to include the control "Microsoft Common Dialog Control 6.0) or a later version. And this:

cdbDialog.DialogTitle = "Select a Printer"   '(for example)
cdbDialog.ShowPrinter

Will display the printer dialogue box.

There are properties you can set such as:

CommonDialog1.Copies = 2  
CommonDialog1.PrinterDefault = True
a summary of such properties are under the print dialogue heading at the bottom of this site: http://www.raritanval.edu/departments/CIS/full-time/Schwarz/avb/Lesson10.htm

CommonDialog1.flags = ....
here's a list of available flags: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cmdlg98/html/vbproFlagsPrint.asp

Really though you should just give the user as much freedom as he wants.

Once they submit you have tons of data from the form to process.
you can add stuff to the 'to print' list using a Printer object.
To set the printer object up based on the dialogue, you set properties such as Printer.DeviceName = CommonDialog1.name

Printer.Print form2.textbox1
Printer.Print form2.label
Printer.Print "ANd then..."   etc

Nothing will actually print until you have a lone statment.

Printer.Print.

Make sure to have lots of error handling incase the user disconnects from the printer.

This is a taste and there's a more in depth walkthrough of what you can do with a printer here: http://www.samspublishing.com/library/content.asp?b=STY_VB6_24hours&seqNum=166&rl=1

Hope I've been helpful. Let me know if there's any holes to fill. There's a lot of different approaches here.
0
 
LVL 4

Assisted Solution

by:programmer1024
programmer1024 earned 50 total points
ID: 16927990
0
 
LVL 1

Author Comment

by:bryanford
ID: 17046795
A combination of both your posts assisted me with the end result so thankyou very much

sorry for the delay.

Bryan
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

816 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

10 Experts available now in Live!

Get 1:1 Help Now