Convert this VB to C#

D

Dan Holmes

I figured out most of it but i don't know what to do with the addhandler
in the constructor.


Public Class ScreenCapture
Dim thisImage As System.Drawing.Image
Dim d As System.Drawing.Printing.PrintDocument

Public Sub New()
d = New System.Drawing.Printing.PrintDocument
AddHandler d.PrintPage, AddressOf PrintPage
End Sub

Public Sub PrintFormClientArea(ByRef f As System.Windows.Forms.Form)
Dim sc As New ScreenCapture
thisImage = sc.CaptureWindow(f.Handle)
d.DocumentName = f.Text
If d.PrinterSettings.InstalledPrinters.Count > 1 Then
Dim p As System.Windows.Forms.PrintDialog = New
System.Windows.Forms.PrintDialog
p.Document = d
p.PrinterSettings.DefaultPageSettings.Landscape = True
If p.ShowDialog(f) = DialogResult.OK Then
d.Print()
End If
Else
d.PrinterSettings.DefaultPageSettings.Landscape = True
d.Print()
End If
End Sub

Private Sub PrintPage(ByVal sender As Object, ByVal ev As
System.Drawing.Printing.PrintPageEventArgs)
ev.Graphics.DrawImage(thisImage, ev.Graphics.VisibleClipBounds)
End Sub
End Class
 
J

Jon Skeet [C# MVP]

Dan Holmes said:
I figured out most of it but i don't know what to do with the addhandler
in the constructor.

Write

d.PrintPage += new PrintPageEventHandler(PrintPage);
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

It would be converted like this:

// Create the print document.
d = new System.Drawing.Printing.PrintDocument();

// Add the event handler.
d.PrintPage += new PrintPageEventHandler(this.PrintPage);

Or, in C# 2.0, you could shorten it to:

// Add the event handler.
d.PrintPage += this.PrintPage;

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top