private void btnSave_Click(object sender, EventArgs e) { if(! Validator.IsValidHostName(txtHostName.Text.Trim())) { MessageBox.Show(Resources.msg_warning_correct_hostname, Resources.warning, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtHostName.Focus(); } else if (! Validator.IsValidIP(txtIP.Text.Trim())) { MessageBox.Show(Resources.msg_warning_correct_ip_address, Resources.warning, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtIP.Focus(); } else { Host newHost = new Host(); newHost.HostName = txtHostName.Text.Trim(); newHost.IP = txtIP.Text.Trim(); newHost.Enabled = chkEnabled.Checked; newHost.Comment = txtComment.Text.Trim(); this.newHosts.Add(newHost); this.Close(); } }
public static List<Host> ReadHosts(out FileOperationResult result) { result = FileOperationResult.Success; List<Host> hosts = new List<Host>(); TextReader txtStreamReader = null; try { txtStreamReader = new StreamReader(HostFileManager.HostPath); int id = 0; string line = String.Empty; while ((line = txtStreamReader.ReadLine()) != null) { // Split where \t or space, // 4 times, // Remove empty entries string[] data = line.Split(new char[] { '\t', ' ' }, 4, StringSplitOptions.RemoveEmptyEntries); // Valid: [#] IP HOST-Name [#Comment] // Items that are in [] are optional if (!Validator.IsValid(data)) continue; Host newHost = new Host(); newHost.Id = id++; // checks if binding is disabled if (data[0] == "#") { newHost.Enabled = false; newHost.IP = data[1]; newHost.HostName = data[2]; } else { newHost.Enabled = true; newHost.IP = data[0]; newHost.HostName = data[1]; } // checks if it binding has any comments if (data.Length > 2) { // for bindings that are not disabled int commentIndex = 2; // for bindings that are disabled if (data.Length > 3) commentIndex = 3; // remove '#' from comment if (data[commentIndex][0] == '#') newHost.Comment = data[commentIndex].Substring(1); else newHost.Comment = data[commentIndex]; } hosts.Add(newHost); } // end while } catch (FileNotFoundException) { result = FileOperationResult.FileNotFound; } catch { result = FileOperationResult.Failed; } finally { // close the file anyways if (txtStreamReader != null) txtStreamReader.Close(); } return hosts; }
private void AddNewListViewItem(Host host) { ListViewItem hostItem = this.listView1.Items.Add(String.Empty); if (host.Enabled) hostItem.ImageIndex = 0; else hostItem.ImageIndex = 1; hostItem.SubItems.Add(host.Id.ToString()); hostItem.SubItems.Add(host.HostName); hostItem.SubItems.Add(host.IP); hostItem.SubItems.Add(host.Comment); }
private void EditListViewItem(int index, Host host) { ListViewItem hostItem = this.listView1.Items[index]; if (host.Enabled) hostItem.ImageIndex = 0; else hostItem.ImageIndex = 1; hostItem.SubItems[2].Text = host.HostName; hostItem.SubItems[3].Text = host.IP; hostItem.SubItems[4].Text = host.Comment; }