private void cmdAdd_Click(object sender, System.EventArgs e) { PortMapping mapping = FrmAddPort.GetPortMapping(this); if (mapping == null) { return; } foreach (ListViewItem existingItem in listPorts.Items) { PortMapping addedMapping = (PortMapping)existingItem.Tag; if (!addedMapping.Equals(mapping)) { continue; } MessageBox.Show($"Port number {mapping.Port} is already added to the list.", "Port exists!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ListViewItem item = new ListViewItem(mapping.ToString()) { Tag = mapping }; listPorts.Items.Add(item); }
public static PortMapping GetPortMapping(Form parent) { FrmAddPort addPort = new FrmAddPort(); if (addPort.ShowDialog(parent) == DialogResult.OK) { MappingProtocol protocol = MappingProtocol.Tcp; if (addPort.rbUdp.Checked) { protocol = MappingProtocol.Udp; } else if (addPort.rbTcpUdp.Checked) { protocol = MappingProtocol.Both; } return(new PortMapping() { Port = Convert.ToInt32(addPort.numPort.Value), Protocol = protocol }); } return(null); }