1

I'm starting with SAP B1 UI API (9.0) and I'm trying to handle a button click without any luck so far. This is how I'm doing it (removing unnecessary to make it shorter):

static void Main(string[] args)
{
    SetApplication(args);

    var cParams = (FormCreationParams)App.CreateObject(BoCreatableObjectType.cot_FormCreationParams);
    cParams.UniqueID = "MainForm_";
    cParams.BorderStyle = BoFormBorderStyle.fbs_Sizable;

    _form = App.Forms.AddEx(cParams);
    /*Setting form's title, left, top, width and height*/

    // Button
    var item = _form.Items.Add("BtnClickMe", BoFormItemTypes.it_BUTTON);
    /*Setting button's left, top, width and height*/
    var btn = (Button)item.Specific;
    btn.Caption = "Click Me";
    _form.VisibleEx = true;

    App.ItemEvent += new _IApplicationEvents_ItemEventEventHandler(App_ItemEvent);
}

private static void SetApplication(string[] args)
{
    string connectionString = args[0];
    int appId = -1;
    try
    {
        var guiApi = new SboGuiApi();
        guiApi.Connect(connectionString);
        App = guiApi.GetApplication(appId);
    }
    catch (Exception e)
    { /*Notify error and exit*/ }
}

private static void App_ItemEvent(string FormUID, ref ItemEvent pVal, out bool BubbleEvent)
{
    BubbleEvent = true;

    if (FormUID == "MainForm_" && pVal.EventType == BoEventTypes.et_CLICK &&
        pVal.BeforeAction && pVal.ItemUID == "BtnClickMe")
    {
        App.MessageBox("You just click on me!");
    }
}

When I click the button nothing happens, is this the way to go? I've made so many variations in the handler method but nothing yet. Another detail is that the visual studio's debugger terminates as soon as the addon is started (maybe this has something to do with my problem).

I hope you can help me. Thanks in advance.

David.

dcg
  • 4,187
  • 1
  • 18
  • 32

1 Answers1

2

Since the application stops running there are two possible answers to this question depending on what you prefer to use.

If you are using the SAPbouiCOM library you need a way to keep the application running, the way I use is the System.Windows.Forms.Application.Run(); from the windows forms assembly.

If you are using the SAPBusinessOneSDK and SAPbouiCOM.Framework as a reference you can use the App.Run();.

Both of these need to be invoked as soon as your setup code has run.

  • 1
    Thanks a lot Vyron, I'm using `SAPbouiCOM` and with `System.Windows.Forms.Application.Run()` works like a charm. – dcg Oct 23 '19 at 13:01