private void btnRegister_Click()

in Hands-on lab/lab-files/starter-project/SmartMeterSimulator/MainForm.cs [46:85]


        private void btnRegister_Click(object sender, EventArgs e)
        {
            //There are 10 devices (Sensors) in this sample, Device0 - Device9
            //Provision installed devices with a Device Key
            //Use buttons as a container for each device
            try
            {
                //Loop through the building windows (Buttons)
                foreach (Button button in pDevices.Controls)
                {
                    if (button.Tag == null)
                        continue;

                    var sensor = (Sensor)button.Tag;
                    if(sensor.State == DeviceState.Installed)
                    {
                        string deviceId = button.Name;

                        Task<SmartMeterDevice> taskResult = Task.Run(() => DeviceManager.RegisterDeviceAsync(txtGroupEnrollmentKey.Text, txtIdScope.Text, deviceId));
                        var device = taskResult.Result;

                        //Create a new device (Sensor) object, embed its unique device key
                        sensor.SetRegistrationInformation(device.IoTHubHostName, device.AuthenticationKey);

                        //Change each button color from Yellow to Cyan
                        button.BackColor = Color.Cyan;                
                    }
                    else
                    {
                        //disable uninstalled devices
                        button.Enabled = false;
                    }                   
                }
                btnRegister.Enabled = false;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }