Build Your First GUI Plugin


Note: We'll use Visual Studio 2022 (or 2019) for this purpose


1) In Visual Studio, Create a Class Library project



2. Choose .NET Framework 4.6.1




3. Remove "Class1" and add a new UserControl



4. From the Toolbox, add a "Label" object



5. Add reference to Stream.Common.Shared.





6. Here's a simple code in both C# and VB.NET


C# Example:


using Stream.Common.Shared;

using System;

using System.Windows.Forms;


namespace SimpleLabelPlugin

{

    // Note: Inherits UserControl for designer support, implements IPluginView directly

    // Cannot inherit from PluginViewBase due to single inheritance limitation

    public partial class SimpleLabelPlugin : UserControl, IPluginView

    {

        private IHostServices _host;


        public string Tag1 { get; set; }

        public string Tag2 { get; set; }


        public SimpleLabelPlugin()

        {

            InitializeComponent();                 // <-- create label1 and wire designer events

            this.Load += SimpleLabelPlugin_Load;   // optional

            this.Click += SimpleLabelPlugin_Click; // optional

        }


        private void SimpleLabelPlugin_Load(object sender, EventArgs e)

        {


        }


        public void Initialize(IHostServices host)

        {

            _host = host;

        }


        public Control GetControl()

        {

            return this;

        }


        private void SimpleLabelPlugin_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Clicked");

            //Examples:

            //_host.Tags.WriteValue(Tag2, any_value);

        }


        public void PluginUpdate()

        {

            try

            {

                if (_host?.Tags != null && !string.IsNullOrEmpty(Tag1))

                {

                    this.label1.Text = _host.Tags.ReadValue(Tag1)?.ToString() ?? "N/A";

                }

            }

            catch

            {

                // Code to run in case of Exception

            }

        }

    }

}



VB.NET Example:


Imports System.Windows.Forms

Imports Stream.Common.Shared


' Note: Inherits UserControl for designer support, implements IPluginView directly

' Cannot inherit from PluginViewBase due to single inheritance limitation

Public Class SimpleLabelPluginVB

    Inherits UserControl

    Implements IPluginView


    Private _host As IHostServices


    Public Property Tag1 As String

    Public Property Tag2 As String


    Public Sub Initialize(host As IHostServices) Implements IPluginView.Initialize

        _host = host

    End Sub


    Public Function GetControl() As Control Implements IPluginView.GetControl

        Return Me

    End Function


    Private Sub SimpleLabelPluginVB_Click(sender As Object, e As EventArgs) Handles Me.Click

        '_host.Tags.Write("Pump1.Speed_SP", Now.Second)

        MsgBox("clicked")

        'Examples:

        '_host.Tags.WriteValue(Tag2, any_value)

    End Sub


    Public Sub PluginUpdate() Implements IPluginView.PluginUpdate

        Try

            If _host IsNot Nothing AndAlso _host.Tags IsNot Nothing AndAlso Not String.IsNullOrEmpty(Tag1) Then

                Me.Label1.Text = _host.Tags.ReadValue(Tag1)?.ToString()

                If Me.Label1.Text Is Nothing Then Me.Label1.Text = "N/A"

            End If

        Catch ex As Exception

            'Code to run in case of Exception

        End Try

    End Sub


End Class




7. Build the project, and locate the dll file



8. Copy the dll file (and all preerquistes) to Stream application folder under "Plugins\plugin name"




9. In the Graphics Builder, place a new "GUI Plugin" and select the required one.




10. Notice the properties (matching the global properties in the dll)



11. You can use Tags or Dynamic Parameters (or any static values as it doesn't have to be Tags).