0

I have wrtitten the code below in C#, but I would need to re-write it in VB.Net. How can I do it?

       public void ImprimirDXReport(DXReportParams reportParams, int trayIdx, bool preview = false)
    {
        ...

        report.PrintingSystem.StartPrint += PrintingSystem_StartPrint;

        ...
    }

    private void PrintingSystem_StartPrint(object sender, PrintDocumentEventArgs e)
    {
        e.PrintDocument.DefaultPageSettings.PaperSource = e.PrintDocument.PrinterSettings.PaperSources[this.TrayIndex];
    }

I've been reading about RaiseEvent and AddHandler, but I'm pretty confused about them.

Thank you.

garciafigueres
  • 73
  • 1
  • 11
  • 2
    Does this answer your question? [Syntax for adding an event handler in VB.NET](https://stackoverflow.com/questions/17511140/syntax-for-adding-an-event-handler-in-vb-net) – MatSnow Jul 21 '20 at 15:29
  • 2
    `AddHandler` is what you want, as indicated in the link provided above. It's pretty simple stuff. In C# it's `someObject.SomeEvent += SomeMethod;` and in VB it's `AddHandler someObject.SomeEvent, AddressOf SomeMethod`. – jmcilhinney Jul 21 '20 at 17:07
  • 2
    If you use `AddHandler`, I would very strongly recommend to also use `Option Strict On`. Without it, you can get very surprising behavior if you don't match the event signature exactly and then try to unsubscribe from the event. – Craig Jul 21 '20 at 18:48

1 Answers1

0

First, for code converting I recommend:
https://converter.telerik.com/

That's not that difficult. The class which will fire the events should be declared like this:

Public Class A

    Public Event SomethingDone(sender As Object, e As EventArgs)

    Public Sub DoSomething()
        RaiseEvent SomethingDone(Me, New EventArgs)
    End Sub

End Class

So this class has a declared event SomethingDone and this will be fired when the method raises it.

The recieving class could be declared like this:

Public Class B

    Private WithEvents DoIt As A

    Private Sub DoIt_SomethingDone(sender As Object, e As EventArgs) Handles DoIt.SomethingDone
        Dim X As Integer
        ' Some other stuff
    End Sub

End Class

So this class uses the class A and can recieve their events. The keyword is here "WithEvents". You have to declare the class with this keyword to recieve their events.

In the IDE you have now the possibility to choose the class and choose an event like you do with control events.

The event handler will be automatically created with the keyword "Handles" at the end. This indicates which event(s) will be handled.

If you want to give parameters with the event, you have to create a new class which inherits from EventArgs.

Your code would be sufficent maybe with:

Private Sub PrintingSystem_StartPrint(ByVal sender As Object, ByVal e As PrintDocumentEventArgs) Handles report.PrintingSystem.StartPrint
    e.PrintDocument.DefaultPageSettings.PaperSource = e.PrintDocument.PrinterSettings.PaperSources(Me.TrayIndex)
End Sub

For the AddHandler, AddressOf stuff I recommend
https://www.codeproject.com/Articles/5041/Step-by-Step-Event-handling-in-VB-NET

I dont want to go in to Delegates, that's to much stuff for now.

Aranxo
  • 677
  • 4
  • 15
  • Removing the "Handles report.PrintingSystem.StartPrint" part made it work. Otherwise, I would have to add a WithEvents variable which seems unnecessary to me, since it's working. What kind of problems might I run into by doing it this way, without the "WithEvents" and the "Handles..." ? – garciafigueres Jul 22 '20 at 06:45
  • If it works for you, there should be no problem. Maybe you used the code converter which translated it to "report.PrintingSystem.StartPrint += AddressOf PrintingSystem_StartPrint". I am not that firm to it. Maybe this is a short version of the dynamical AddHandler statement. Then you registered the correct handler this way. – Aranxo Jul 22 '20 at 12:41