private void AddressList_Validating(object sender, System.ComponentModel.CancelEventArgs e) { bool isValid = false; // Assume that the address is invalid until proven otherwise TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text}"); // Test whether the supplied IP address is valid and, if it is an IPv6 address, test whether it is in canonical form if (IPAddress.TryParse(addressList.Text.Trim(), out IPAddress ipAddress)) // The host name is an IP address { if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6) // This is an IPv6 address { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is an IPv6 address"); if (addressList.Text.Trim().StartsWith("[") & addressList.Text.Trim().EndsWith("]")) { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is a canonical IPv6 address"); // The IP v6 address is already in canonical form, no action required isValid = true; } else // The IPv6 address is not in canonical form so we need to add square brackets { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is NOT a canonical IPv6 address"); SetupErrorProvider.SetError(addressList, "IPv6 addresses must be in canonical form i.e. start with [ and end with ]."); } } else // This is an IPv4 address { // The IP v4 address is already in canonical form, no action required isValid = true; } } else // The host name is either an invalid IP address or a string so validate this { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is NOT a valid IP address"); MatchCollection matches = validHostnameRegex.Matches(addressList.Text); if (matches.Count == 0) { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is NOT a valid IP address or Host Name"); SetupErrorProvider.SetError(addressList, "Not a valid IP address or host name."); } else { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is a valid Host Name"); isValid = true; } } if (isValid) { SetupErrorProvider.Clear(); btnOK.Enabled = true; } else { btnOK.Enabled = false; } }
private void AddressList_Validating(object sender, System.ComponentModel.CancelEventArgs e) { bool isValid = false; if (IsIpAddress(addressList.Text)) // The host name is an IP address so test whether this is valid { MatchCollection matches = validIpAddressRegex.Matches(addressList.Text); if (matches.Count == 0) { SetupErrorProvider.SetError(addressList, "IP addresses can only contain digits and the point character in the form WWW.XXX.YYY.ZZZ."); } else { isValid = true; } } else // The host name is a string rather than an IP address so validate this { MatchCollection matches = validHostnameRegex.Matches(addressList.Text); if (matches.Count == 0) { SetupErrorProvider.SetError(addressList, "Not a valid host name."); } else { isValid = true; } } if (isValid) { SetupErrorProvider.Clear(); btnOK.Enabled = true; } else { btnOK.Enabled = false; } }
private void PopulateAddressList() { bool foundAnIPAddress = false; bool foundTheIPAddress = false; int selectedIndex = 0; TL.LogMessage(0, 0, 0, "PopulateAddressList", "Start"); addressList.Items.Clear(); // Add IPv4 addresses if (RadIpV4.Checked | RadIpV4AndV6.Checked) // IPv4 addresses are required { // Add a local host entry addressList.Items.Add(SharedConstants.LOCALHOST_NAME_IPV4); // Make "localhost" the first entry in the list of IPv4 addresses foreach (IPAddress ipAddress in HostPc.IpV4Addresses) { addressList.Items.Add(ipAddress.ToString()); TL.LogMessage(0, 0, 0, "PopulateAddressList", string.Format(" Added {0} Address: {1}", ipAddress.AddressFamily.ToString(), ipAddress.ToString())); foundAnIPAddress = true; if (ipAddress.ToString() == IPAddressString) { selectedIndex = addressList.Items.Count - 1; foundTheIPAddress = true; } } } // Add IPv6 addresses if (RadIpV6.Checked | RadIpV4AndV6.Checked) // IPv6 addresses are required { foreach (IPAddress ipAddress in HostPc.IpV6Addresses) { addressList.Items.Add($"[{ipAddress}]"); TL.LogMessage(0, 0, 0, "PopulateAddressList", string.Format(" Added {0} Address: {1}", ipAddress.AddressFamily.ToString(), ipAddress.ToString())); foundAnIPAddress = true; if ($"[{ipAddress}]" == IPAddressString) { selectedIndex = addressList.Items.Count - 1; foundTheIPAddress = true; } } } TL.LogMessage(0, 0, 0, "PopulateAddressList", string.Format($"Found an IP address: {foundAnIPAddress}, Found the IP address: {foundTheIPAddress}, Stored IP Address: {IPAddressString}")); if ((!foundTheIPAddress) & (IPAddressString != "")) // Add the last stored IP address if it isn't found in the search above { if (IPAddressString == "+") // Handle the "all addresses special case { addressList.Items.Add(IPAddressString); // Add the stored address to the list selectedIndex = addressList.Items.Count - 1; // Select this item in the list } else // One specific address so add it if it parses OK { IPAddress serverIpAddress = IPAddress.Parse(IPAddressString); if ( ((serverIpAddress.AddressFamily == AddressFamily.InterNetwork) & ((RadIpV4.Checked | RadIpV4AndV6.Checked))) | ((serverIpAddress.AddressFamily == AddressFamily.InterNetworkV6) & ((RadIpV6.Checked | RadIpV4AndV6.Checked))) ) { addressList.Items.Add(IPAddressString); // Add the stored address to the list selectedIndex = addressList.Items.Count - 1; // Select this item in the list } else { selectedIndex = 0; } } } // Add the wild card addresses at the end of the list // Include the strong wild card character in the list of addresses if not already in use if (IPAddressString != SharedConstants.STRONG_WILDCARD_NAME) { addressList.Items.Add(SharedConstants.STRONG_WILDCARD_NAME); } // Set the combo box selected item addressList.SelectedIndex = selectedIndex; SetupErrorProvider.Clear(); }