Create a subscription
You create a subscription inside the
Start()
method.
TIP:
To insert a new method automatically:
- In Visual Studio, place the caret after the+=operator, and presstwice.TAB
- 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
- TheIUAObjectobject 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 (UserChangeEventArgsin 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 theVariableChangeevent handler, supplied by theIUAVariableclass. See IUAVariable.VariableChange.
- All events generated by an object
- To subscribe a method to all events generated by an object, use theUAEventevent handler, supplied by theIUAObjectclass. 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.In this example, theOnMouseClickevent handler runs theButton2_OnMouseClickmethod each timeButton2is pressed. TheOnMouseClickevent handler is supplied by theButtonclass.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