private void DHCPCheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;

            // Find correct index for the adapter
            int index = Int32.Parse(cb.Name.Split('_').ElementAt(1));

            // The correct element is found by searching the hierarchy down iteratively
            TextBox AdapterIP      = VisualTreeHelpers.FindChild <TextBox>(AdapterStackPanel, ("AdapterIP_" + index));
            TextBox AdapterNetmask = VisualTreeHelpers.FindChild <TextBox>(AdapterStackPanel, ("AdapterNetmask_" + index));
            TextBox AdapterGateway = VisualTreeHelpers.FindChild <TextBox>(AdapterStackPanel, ("AdapterGateway_" + index));

            if (cb.IsChecked == true)
            {
                AdapterIP.IsReadOnly      = true;
                AdapterNetmask.IsReadOnly = true;
                AdapterGateway.IsReadOnly = true;

                AdapterIP.Background      = (SolidColorBrush) new BrushConverter().ConvertFromString(GREY_BG);
                AdapterNetmask.Background = (SolidColorBrush) new BrushConverter().ConvertFromString(GREY_BG);
                AdapterGateway.Background = (SolidColorBrush) new BrushConverter().ConvertFromString(GREY_BG);
            }
            else
            {
                AdapterIP.IsReadOnly      = false;
                AdapterNetmask.IsReadOnly = false;
                AdapterGateway.IsReadOnly = false;

                AdapterIP.Background      = (SolidColorBrush) new BrushConverter().ConvertFromString(WHITE_BG);
                AdapterNetmask.Background = (SolidColorBrush) new BrushConverter().ConvertFromString(WHITE_BG);
                AdapterGateway.Background = (SolidColorBrush) new BrushConverter().ConvertFromString(WHITE_BG);
            }
        }
        private void applyButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            // Find correct index for the adapter
            int index = Int32.Parse(btn.Name.Split('_').ElementAt(1));

            // The correct element is found by searching the hierarchy down iteratively
            TextBlock adapterDescription = VisualTreeHelpers.FindChild <TextBlock>(AdapterStackPanel, ("adapter_" + index));
            TextBox   AdapterIP          = VisualTreeHelpers.FindChild <TextBox>(AdapterStackPanel, ("AdapterIP_" + index));
            TextBox   AdapterNetmask     = VisualTreeHelpers.FindChild <TextBox>(AdapterStackPanel, ("AdapterNetmask_" + index));
            TextBox   AdapterGateway     = VisualTreeHelpers.FindChild <TextBox>(AdapterStackPanel, ("AdapterGateway_" + index));
            CheckBox  DHCPselection      = VisualTreeHelpers.FindChild <CheckBox>(AdapterStackPanel, ("AdapterDHCPon_" + index));

            ManagementClass            adapterMC         = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection adapterCollection = adapterMC.GetInstances();

            foreach (ManagementObject adapter in adapterCollection)
            {
                if (String.Equals(adapter["Description"], adapterDescription.Text))
                {
                    // Enable static IP settings or DHCP based on the checkbox selection
                    if (DHCPselection.IsChecked == false)
                    {
                        try
                        {
                            // Set Default Gateway
                            var newGateway = adapter.GetMethodParameters("SetGateways");

                            // First set up the gateways with an empty array to clear the existing gateways
                            // It is assumed here that one gateway per adapter is sufficient
                            newGateway["DefaultIPGateway"]  = new string[] { };
                            newGateway["GatewayCostMetric"] = new int[] { 1 };
                            adapter.InvokeMethod("SetGateways", newGateway, null);

                            // Then add the actual gateway
                            newGateway["DefaultIPGateway"] = new string[] { AdapterGateway.Text };
                            adapter.InvokeMethod("SetGateways", newGateway, null);

                            // Set IP address and Subnet Mask
                            var newAddress = adapter.GetMethodParameters("EnableStatic");
                            newAddress["IPAddress"]  = new string[] { AdapterIP.Text };
                            newAddress["SubnetMask"] = new string[] { AdapterNetmask.Text };
                            adapter.InvokeMethod("EnableStatic", newAddress, null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Unable to set static IP:\n" + ex.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            // Clear the gateway list before DHCP updates it so that
                            // no unintended gateways remain in the list
                            var newGateway = adapter.GetMethodParameters("SetGateways");
                            newGateway["DefaultIPGateway"]  = new string[] { };
                            newGateway["GatewayCostMetric"] = new int[] { 1 };
                            adapter.InvokeMethod("SetGateways", newGateway, null);

                            adapter.InvokeMethod("EnableDHCP", null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Unable to set dynamic IP:\n" + ex.Message);
                        }
                    }

                    // Adapt the callback function to the ElapsedEventHandler format with a lambda expression
                    ApplyButtonTimer.Elapsed += (sender2, e2) => changeApplyButtonColor(sender2, e2, btn);

                    btn.Content    = "Applied";
                    btn.FontSize   = 10;
                    btn.Margin     = new Thickness(680, 5, 0, 0);
                    btn.Background = (SolidColorBrush) new BrushConverter().ConvertFromString(APPLYBUTTON_ALT_BG);

                    ApplyButtonTimer.Stop();
                    ApplyButtonTimer.Start();


                    break; // Further iteration unnecessary
                }
            }
        }