In this section, I’ll show complete demo of how to create Custom User Control in C# windows applications.
I’ll show descriptions of custom user control and basics of the custom user control. After learning this document you will be able to make the custom user control according to your own requirement.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ATPropertyEditor;
namespace ATSCADA.iWinTools
{
// iStatus is an UserControl that is used to show the connection status of tags
public partial class iStatus : UserControl
{
protected string _TagName; //tag name
protected string _TaskName; //task name
protected iDriver _Driver; //driver name
protected Timer _SysTimer; // processing timer
// Add Tag
[Description(“Select Tag for SCADA control”)]
[TypeConverter(typeof(TaskConverter)), CategoryAttribute(“ATSCADA Settings”)]
public string TagName
{
get
{
return _TaskName + “.” + _TagName;
}
set
{
_TaskName = value.Split(‘.’)[0];
_TagName = value.Split(‘.’)[1];
}
}
// Add driver
[Description(“Select Driver for SCADA control”)]
[Browsable(true), Category(“ATSCADA Settings”)]
public iDriver Driver
{
get
{
return _Driver;
}
set
{
_Driver = value;
}
}
private void iUpdateValue(object o, TagStatusEventArgs e)
{
try
{
//Update Label Value
string nv;
nv = e.NewStatus;
if (nv == “Good”) // if the conncetion status is good,the backcolor of iStatus will be green
this.BackColor = Color.LimeGreen;
else // else if the connection status is bad, the backcolor of iStatus will be red
this.BackColor = Color.Red;
}
catch { }
}
private void _SysTimer_Tick(object o, EventArgs e)
{
//In runtime
try
{
//Attach Driver for Item
//Get the Tag with _TagName
if ((_TagName != null) && (_Driver != null) && (_Driver.Task(_TaskName) != null))
{
//Get Update Event from tag
if (_Driver.Task(_TaskName).Tag(_TagName) != null)
{
_Driver.Task(_TaskName).Tag(_TagName).TagStatusChanged += new TagStatusHandler(iUpdateValue);
//Init Value
if (_Driver.Task(_TaskName).Tag(_TagName).Status == “Good”) // check the connection status of tag, if status is good,the backcolor of iStatus will be green
this.BackColor = Color.LimeGreen;
else // else if status is bad, the backcolor of iStatus will be red
this.BackColor = Color.Red;
}
_SysTimer.Enabled = false;
_SysTimer.Dispose();
}
}
catch { }
}
// initialize iStatus
public iStatus()
{
InitializeComponent();
try
{
//not support design time
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == “devenv”
|| System.Diagnostics.Process.GetCurrentProcess().ProcessName == “VCSExpress”
|| System.Diagnostics.Process.GetCurrentProcess().ProcessName == “vbexpress”
|| System.Diagnostics.Process.GetCurrentProcess().ProcessName == “WDExpress”)
{
return;
}
}
catch
{
return;
}
// initialize Timer for runtime
_SysTimer = new Timer();
_SysTimer.Interval = 1000;
_SysTimer.Tick += new EventHandler(_SysTimer_Tick);
_SysTimer.Enabled = true;
}
}
}