private void addNewToolStripMenuItem_Click(object sender, EventArgs e) // add a TabPage
        {
            foreach (TabPage t in RecipientsTab.Controls)
            {
                if (t.Name == "Not Set")
                {
                    return;
                }
            }

            TabPage NewTab = new System.Windows.Forms.TabPage();

            NewTab.Text = NewTab.Name = "Not Set";

            RecipientUserCtrl content = new RecipientUserCtrl(NewTab);

            content.DeviceAddrOK += new Action <RecipientUserCtrl, bool>(content_DeviceAddrOK);

            NewTab.Controls.Add(content);
            RecipientsTab.Controls.Add(NewTab);

            RecipientsTab.SelectTab("Not Set");

            labelEmpty.Visible = false;
        }
        private void LoadProperties()
        {
            RecipientsTab.Controls.Clear();

            // Two properties
            List <BacnetPropertyReference> props = new List <BacnetPropertyReference>();

            props.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_RECIPIENT_LIST, ASN1.BACNET_ARRAY_ALL));
            props.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_PRIORITY, ASN1.BACNET_ARRAY_ALL));
            IList <BacnetReadAccessResult> PropertiesValues;

            comm.ReadPropertyMultipleRequest(adr, object_id, props, out PropertiesValues);

            foreach (BacnetPropertyValue aProp in PropertiesValues[0].values)
            {
                // priorities are common to all recipients
                if (aProp.property.propertyIdentifier == (uint)BacnetPropertyIds.PROP_PRIORITY)
                {
                    P_Off.Text    = ((uint)aProp.value[0].Value).ToString();
                    P_Fault.Text  = ((uint)aProp.value[1].Value).ToString();
                    P_Normal.Text = ((uint)aProp.value[2].Value).ToString();
                }
                else
                {
                    // a TabPage in the TabControl for each recipient
                    for (int i = 0; i < aProp.value.Count / 7; i++)
                    {
                        // convert the List<BacnetValue> into a DeviceReportingRecipient
                        DeviceReportingRecipient recipient = new DeviceReportingRecipient(aProp.value[i * 7], aProp.value[i * 7 + 1], aProp.value[i * 7 + 2], aProp.value[i * 7 + 3], aProp.value[i * 7 + 4], aProp.value[i * 7 + 5], aProp.value[i * 7 + 6]);

                        TabPage NewTab = new System.Windows.Forms.TabPage();
                        NewTab.Text = NewTab.Name = i.ToString();

                        // Create a Usercontrol and put it into the TabPage
                        RecipientUserCtrl content = new RecipientUserCtrl(NewTab, recipient);
                        content.DeviceAddrOK += new Action <RecipientUserCtrl, bool>(content_DeviceAddrOK);

                        NewTab.Controls.Add(content);
                        RecipientsTab.Controls.Add(NewTab);

                        labelEmpty.Visible = false;
                    }
                }
            }
        }
        private void WriteProperties()
        {
            try // Write Priorities
            {
                List <BacnetValue> PropVal = new List <BacnetValue>();

                PropVal.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, Convert.ToUInt32(P_Off.Text)));
                PropVal.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, Convert.ToUInt32(P_Fault.Text)));
                PropVal.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, Convert.ToUInt32(P_Normal.Text)));

                comm.WritePropertyRequest(adr, object_id, BacnetPropertyIds.PROP_PRIORITY, PropVal);
            }
            catch { }
            try  // Write recipient List
            {
                List <BacnetValue> PropVal = new List <BacnetValue>();
                foreach (TabPage t in RecipientsTab.Controls)
                {
                    if (t.Name != "Not Set") // Entry is OK ?
                    {
                        RecipientUserCtrl        r = (RecipientUserCtrl)t.Controls[0];
                        DeviceReportingRecipient newrp;
                        if (r.adr != null) // recipient is an IP address
                        {
                            newrp = new DeviceReportingRecipient(r.WeekOfDay, r.fromTime.Value, r.toTime.Value, r.adr, Convert.ToUInt16(r.ProcessId.Text), r.AckRequired.Checked, r.EventType);
                        }
                        else // recipient is a deviceId
                        {
                            newrp = new DeviceReportingRecipient(r.WeekOfDay, r.fromTime.Value, r.toTime.Value, new BacnetObjectId(BacnetObjectTypes.OBJECT_DEVICE, r.deviceid), Convert.ToUInt16(r.ProcessId.Text), r.AckRequired.Checked, r.EventType);
                        }

                        PropVal.Add(new BacnetValue(newrp));
                    }
                }

                comm.WritePropertyRequest(adr, object_id, BacnetPropertyIds.PROP_RECIPIENT_LIST, PropVal);
            }
            catch { }
        }
        void content_DeviceAddrOK(RecipientUserCtrl Sender, bool AddrOk) // Handler called on each changement in recipient address TextBox
        {
            if (AddrOk == false)
            {
                Sender.myTab.Text = Sender.myTab.Name = "Not Set";
            }
            else
            {
                Sender.myTab.Text = Sender.myTab.Name = "";
            }

            // Re-numbering of each TabPage
            int i = 1;

            foreach (TabPage t in RecipientsTab.Controls)
            {
                if (t.Name != "Not Set")
                {
                    t.Text = t.Name = i.ToString();
                    i++;
                }
            }
        }