Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with printing in VB .net

Status
Not open for further replies.

leetech

Automotive
Jan 29, 2003
2
hi need some help
i'm new to vb .net and want to print from a form but cant seem to get the print dialog to display. i can get the print preview up but wont put an image in it,

thanks for any help with this guys
 
Replies continue below

Recommended for you

You may get better results by posting this at tek-tips.com
 
Sorry to bring bad news, but we struggled with printing from vb.net also. There are utilities availble out there to help (so I am told), but when I contacted a colleague w/ a lot of VB experience, he said "It's not what VB is for. Get the data out to another program, which VB is good at doing, and print it from there."

Some manage to print from VB, but I've seen it and would rather take the easy way and transfer data to a print- friendly app.

Good Luck!
 
Hi Leetech,
If you are wanting to print the form, this is the method I use. For the form, Met3Grid, the code for the Print Form Button (Command4) is as shown below. Most of this code simply turns off stuff not to be printed and then after printing, turning the stuff back on.

Private Sub Command4_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command4.Click
Dim J As Integer
Command1.Visible = False
Command2.Visible = False
Command3.Visible = False
Command4.Visible = False
Command5.Visible = False
Command6.Visible = False
Label5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label8.Visible = False
Met3Grid.DefInstance.BackColor = System.Drawing.ColorTranslator.FromOle(&HFFFFFF)
'We make the form look pretty before its picture
Application.DoEvents()
Me.Refresh()
Application.DoEvents()
'Get a Graphics Object from the form
Dim FormG As Graphics = Me.CreateGraphics
'Create a bitmap from that graphics
Dim i As New Bitmap(Me.Width, Me.Height, FormG)
'Create a Graphics object in memory from that bitmap
Dim memG As Graphics = Graphics.FromImage(i)
'get the IntPtr's of the graphics
Dim HDC1 As IntPtr = FormG.GetHdc
Dim HDC2 As IntPtr = memG.GetHdc
'get the picture
BitBlt(HDC2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, HDC1, 0, 0, 13369376)
'Clone the bitmap so we can dispose this one
Me.Print_Image = i.Clone()
'Clean Up
FormG.ReleaseHdc(HDC1)
memG.ReleaseHdc(HDC2)
FormG.Dispose()
memG.Dispose()
i.Dispose()
'Show the PrintDialog
PrintDialog1 = New PrintDialog
PrintDialog1.Document = PrintDocument1
Dim r As DialogResult = PrintDialog1.ShowDialog
If r = DialogResult.OK Then
'Print the document
PrintDocument1.Print()
End If
Met3Grid.DefInstance.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
Command1.Visible = True
Command2.Visible = True
Command3.Visible = True
Command4.Visible = True
Command5.Visible = True
Command6.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
Label8.Visible = True
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor