Create a subscription

You create a subscription inside the
Start()
method.
TIP:
To automatically insert a new method:
  • In Visual Studio, place the caret after the
    +=
    operator, and press
    TAB
    twice.
  • In Visual Studio Code, refer to the editor suggestions.

Example

In this example, the
Session_UserChange
method is subscribed to the user change event using the
UserChange
event handler and the
+=
operator.
public override void Start() { Session.UserChange += Session_UserChange; }
IMPORTANT: Always cancel the subscription in the
Stop()
method to avoid a memory leak. See Cancel a subscription.
The following example shows the
Session_UserChange
method.
private void Session_UserChange(object sender, UserChangeEventArgs e) { Log.Info(e.newUser.BrowseName); }
sender
The
IUAObject
object that corresponds to the node origin of the event.
e
The C# object that contains the event data based on the data type of the event handler (
UserChangeEventArgs
in the example).

Event handlers

Depending on the event type, create a subscription by using different event handlers.
Value change of a variable event
To subscribe a method to the value change of a variable, use the
VariableChange
event handler, supplied by the
IUAVariable
class. See IUAVariable.VariableChange.
All events generated by an object
To subscribe a method to all events generated by an object, use the
UAEvent
event handler, supplied by the
IUAObject
class. See IUAObject.UAEvent.
A specific event of an object
To subscribe a method to a specific event generated by an object, use the corresponding event handler supplied by the type. For more information about type event handlers, see Object and variable references.
In this example, the
OnMouseClick
event handler runs the
Button2_OnMouseClick
method each time
Button2
is pressed. The
OnMouseClick
event handler is supplied by the
Button
class.
public override void Start() { var button2 = Owner.Get<Button>("Button2"); button2.OnMouseClick += Button2_OnMouseClick; } private void Button2_OnMouseClick(object sender, MouseClickEvent e) { var label2 = Owner.Get<Label>("Label2"); var button = (Button)sender; label2.Text = "Mouse click event on " + button.BrowseName; }
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.