Triggering PowerScript events from C# code

You can trigger the PowerScript user event in the C# code.

Step 1: Define the PowerScript user event.

The user event can be defined in any PowerBuilder object.

The user event can have only one string-type parameter or no parameter, with one string-type return value or no return value.

Syntax 1:

(None) EventName ({string parameter})

Syntax 2:

string EventName ({string parameter})
  • EventName indicates the name of the user defined event.

  • parameter (optional) indicates the string-type parameter.

  • Syntax 1 has no return value; syntax 2 has a string-type return value.

Step 2: Register the PowerBuilder object where the user event is defined.

You can register the PowerBuilder object using the DotNetObject RegisterObject function.

For example, the following scripts register the PowerBuilder object w_dotnettest with the name "w_dotnettest":

ll_return = lcs_object.RegisterObject("w_dotnettest", w_dotnettest)
if ll_return <> 1 then
   MessageBox("Error", "RegisterObject w_dotnettest failed") 
end if

Step 3: Trigger the PowerScript user event in the C# code through the PowerBuilder.RegisteredObject.TriggerEvent method.

For example, the following C# code triggers the ue_ondotnetcallback event of the PowerBuilder object w_dotnettest:

// Need to add the corresponding pbdotnetcoreinvoker.dll or pbdotnetframeworkinvoker.dll or pbdotnetinvoker.dll to the project reference namespace Appeon.PowerBuilder.DotNet.Test
{
public class TriggerPBEvent
  {
    public string Method(string str1)
    {
     string strResult = "";
     PowerBuilder.RegisteredObject.TriggerEvent("w_dotnettest","ue_ondotnetcallback", "Param from Method", ref strResult);// Trigger the ue_ondotnetcallback event in the PB object w_dotnettest
     return str1 + "(Method)\nReturn:" + strResult;
    }
  }
}

The user event ue_ondotnetcallback of the w_dotnettest window has the following definitions:

// w_dotnettest window event ue_ondotnetcallback
string ls_test
ls_test= "ue_ondotnetcallback(as_param:" + as_param +")"
return ls_test

The following PowerScript calls the C# method to get the result of the user event:

// Call the C# method in PowerScript
ls_return = lcs_ass.Method("PB event callback test")
MessageBox("Info", ls_return)