cancel
Showing results for 
Search instead for 
Did you mean: 

What are the different methods of Handling Events?

leon_laikan
Participant
0 Kudos
712

Hi, everybody

I am a bit confused about how to handle events.

I am not a professional programmer, and very often, I copy codes from the SDK forum and adapt them to my requirements.

My add-ons finally work, but I don't always understand the basic principles behind.

If I had to master all programming concepts before starting to develop add-ons, I would still be learning...

Without realizing it, while copying codes from the forum, it seems to me that I have been using 2 different methods of handling events, and have obtained satisfactory results from both methods.

I don't know the names of the 2 methods, but here are some codes to help you recognize them.

Method 1

Let's call it: Event Type method

If FormUID = "abcde" And pVal.ItemUID = "Btn1" And pVal.InnerEvent = False And pVal.BeforeAction = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_CLICK Then

  . . .

  . . .

  End If



Method 2

Let's call it: Handler Method


RemoveHandler B_OK.PressedBefore, AddressOf OnBeforeB_OKPressed

AddHandler B_OK.PressedBefore, AddressOf OnBeforeB_OKPressed

- - - - - -

Private Sub OnBeforeB_OKPressed(ByVal Source As Object, ByVal pVal As SAPbouiCOM.SBOItemEventArg, ByRef Bubble As Boolean)

. . .

. . .

End Sub


MY QUESTION


How many different methods of handling events are there?

Do they have names?

What are the differences between these methods?

Which method is better?

Which method is more likely to be still supported by SAP in say 10 years?



Thanks

Leon Lai



Accepted Solutions (1)

Accepted Solutions (1)

pedro_magueija
Active Contributor
0 Kudos

Hi Leon,


How many different methods of handling events are there?

In SAP B1 there are two different ways of handling events.


Do they have names?

Not officially, but I've seen them called global events (method 1) and local events (method 2).


What are the differences between these methods?

Local events have been implemented recently and they use different objects (interfaces) for things like the arguments of a event e.g.: ItemEvent for method 1 and SBOItemEventArg for method 2.


Which method is better?

They are equivalent. It's a matter of which way you prefer to use them.


Which method is more likely to be still supported by SAP in say 10 years?

Probably both. SAP is very reluctant in removing legacy code.


Pedro Magueija


LinkedIn Logo View Pedro Magueija's profile on LinkedIn
Follow @pedromagueija on Twitter

If this answer was helpful or correct consider marking it as such.

leon_laikan
Participant
0 Kudos

Hi Pedro,

Thanks a lot for your reply.

It is very informative.

One last question before closing the thread;

In a single program (class), can we use both methods, or must we adopt a single one throughout?

Best Regards,

leon

former_member233854
Active Contributor
0 Kudos

You can use both it is like a normal delegate, it will execute all the events you added the handler.

For reference

http://www.dotnetperls.com/event-vbnet

edy_simon
Active Contributor
0 Kudos

Hi Leon,

To complement Pedro :

How many different methods of handling events are there?

Well, officially, as with Pedro's answer : 2 ways, unofficially, there is the 3rd way : B1DE way.

Even though that B1DE is actually derived from the 1st way in your example.

In the order of when it is available (note: I am not sure of the naming, I just use my own):

1. Application Level Event (your 1st sample)

2. Form Level (this is what the B1DE and the B1Listener introduce)

3. Item Level Event (your 2nd sample) - This is introduced in SBO 9 + the VS for B1Studio

The first method: this comes from the very beginning of SBO, there are actually only a few events.

The events are all on the SBO_Application level. Namely ItemEvent, AppEvent, MenuEvent etc.

In order to filter out the correct event you want to handle, you need to do the SELECT CASE, IF THEN.

My personal view : I don't like this way, to filter the event, you need a lot of nested SELECT CASE and IF THEN clause. The more complex your add-on, the more this nested filter. The code will be very ugly and you will have a hard time reading and debugging the code.

The 2nd Method : With the introduction of the unofficial tools B1DE, the Application level events are split into form level events. With your class inherits from B1Event and your methods decorated by the B1Listener attribute, you can split the handling of events for each form in 1 class. and 1 Event Type in 1 procedure of your class.

Sample of this code :


Imports B1WizardBase

Imports SAPbobsCOM

Imports SAPbouiCOM

Imports System.Windows.Forms

Public Class TestEvents

    Inherits B1Event

    Public Sub New()

        MyBase.New()

    End Sub

    <B1Listener(BoEventTypes.et_ITEM_PRESSED, True, New String() {"frmTest"})> _

    Public Overridable Function OnBeforeItemPressed(ByVal pVal As ItemEvent) As Boolean

        Dim form As SAPbouiCOM.Form = B1Connections.theAppl.Forms.Item(pVal.FormUID)

        If pVal.ItemUID = "1" And form.Mode = BoFormMode.fm_UPDATE_MODE Then

            'Update Form

            Try

                 'Do your thing here

            Catch ex As Exception

                SBO_Application.MessageBox("Unable to update form." & ex.Message & vbCrLf & ex.StackTrace)

                SBO_Application.StatusBar.SetText("Unable to Update form.", BoMessageTime.bmt_Medium, BoStatusBarMessageType.smt_Error)

                Return False

            End Try

        End If

          Return True

    End Function

End Class

My view : This will make your code clearer as you can use 1 class to handle 1 sbo form and 1 Procedure to handle 1 event type.

Still, you need to have the SELECT CASE and IF THEN to filter the items you want to handle.

The 3rd Way : Introduced since SBO9, SAP took a major change in the UI API, (although it is still back ward compatible), They introduced the event on the item level. Means, you can hook up a procedure for each item and event type. This is similar to how the Visual Studio works.

With this you don't need the extra SELECT CASE and IF THEN to filter the event anymore.

Personally, The 3rd way is the best, codes are clear, I can handle 1 form in 1 class, and in this class, each procedure handles each event of each item.

Regards

Edy

leon_laikan
Participant
0 Kudos

Hi, Danilo

Thanks a lot for your reply.

Your link is very interesting and I think I can make good use of it in future projects.

Best Regards,

Leon

leon_laikan
Participant
0 Kudos

Hi Edy,

Thanks a lot for your very clear explanation.

Coming from you, I am not surprised that you find Method 3 (Item Level Event method) the best one, because it was you who initiated me to this method when I first started programming using classes in VB.NET.

I find the method very clear and easy to use and debug once you get used to it, and I almost always use this method now.

However, I have an old add-on where I use the old Method 1 (Application Level Event method).

I now want to add another event handler. Rather than rewriting everything using Method 3, I am wondering if I can retain the old events under method 1, and add the new event using Method 3.

So, a single project would be using 2 "different" methods of handling events.

Danilo gave me a hope. He said that we could use both methods in the same project if we use the Delegate keyword like:

Delegate Sub EventHandler()

Event _show As EventHandler


Do you think I can modify my project using this idea?

Never tried, but I'm testing now. Would be great if it works!

Best Regards,

Leon

edy_simon
Active Contributor
0 Kudos

Hi Leon,

For your case, yes it is possible to mix both model.

Just be careful  not to let the same event handled in both model, the task will be run multiple times.

There isn't a need to have a delegate in your code, just declare it as usual.

If i were you, I would not mix the two if possible.

Some one else debugging your code would have a very difficult time

Regards

Edy

pedro_magueija
Active Contributor
0 Kudos

Hi Leon,

Technically you can. I wouldn't recommend it though. Using a single way to handle events will improve your application consistency and it's easier to understand what is going on.

I'd choose the one I'm more comfortable with and use that one through the entire project.


Pedro Magueija


LinkedIn Logo View Pedro Magueija's profile on LinkedIn
Follow @pedromagueija on Twitter

If this answer was helpful or correct consider marking it as such.

leon_laikan
Participant
0 Kudos

Hi Edy

Thanks a lot.

Thanks a lot also to Pedro and Danilo

Best Regards

Leon

Answers (0)