Click here to Skip to main content
15,895,740 members
Articles / Programming Languages / Visual Basic

Ping Monitor

Rate me:
Please Sign up or sign in to vote.
4.90/5 (24 votes)
20 Jul 20075 min read 206.4K   10.4K   102  
Yet another Ping Monitor utility written in VB.NET on .NET Framework 2.0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.VisualBasic;

namespace PingMonitor
{
  public partial class frmConfig : Form
  {
    private DataTable dtHosts = new DataTable();
    private SqlDataAdapter daHosts;

    public frmConfig()
    {
      InitializeComponent();
    }

    private void frmConfig_Load(object sender, EventArgs e)
    {
      string ConnStr = ConfigurationManager.AppSettings["ConnStr"].ToString();
      daHosts = new SqlDataAdapter("SELECT * FROM HostList", ConnStr);
      SqlCommandBuilder cb = new SqlCommandBuilder(daHosts);
      daHosts.InsertCommand = cb.GetInsertCommand();
      daHosts.UpdateCommand = cb.GetUpdateCommand();
      daHosts.DeleteCommand = cb.GetDeleteCommand();

      ReLoadTree();
    }

    private void ReLoadTree()
    {
      dtHosts.Clear();
      daHosts.Fill(dtHosts);

      tvHosts.Nodes.Clear();
      tvHosts.Nodes.Add("All");
      LoadChildNodes("IDparent IS NULL", tvHosts.Nodes[0]);
      tvHosts.SelectedNode = tvHosts.Nodes[0];
    }

    private void LoadChildNodes(string Filter, TreeNode Node)
    {
      DataView dv = new DataView(dtHosts, Filter, "Host", DataViewRowState.Unchanged);
      DataRowView dr;
      for (int i = 0; i <= dv.Count - 1; i++)
      {
        dr = dv[i];
        Node.Nodes.Add(dr["Host"].ToString());
        Node.Nodes[i].Tag = dr;
        if ((!object.ReferenceEquals(dr["IsHost"], DBNull.Value)) && dr["IsHost"].ToString() == "Y")
        {
          Node.Nodes[i].ImageIndex = 1;
          Node.Nodes[i].SelectedImageIndex = 2;
        }
        else
        {
          Node.Nodes[i].ImageIndex = 0;
        }
        LoadChildNodes("IDparent=" + dr["ID"], Node.Nodes[i]);
        Node.Expand();
      }
    }

    private void mniDelete_Click(object sender, System.EventArgs e)
    {
      if (tvHosts.SelectedNode.Tag == null) return; // TODO: might not be correct. Was : Exit Sub

      DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
      if (dtHosts.Select("IDparent=" + dr["ID"]).Length > 0)
      {
        MessageBox.Show("You can only delete leaf nodes!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return; 
      }
      foreach (DataRow r in dtHosts.Rows)
      {
        if (r["ID"] == dr["ID"])
        {
          r.Delete();
          break; 
        }
      }
      daHosts.Update(dtHosts);
      ReLoadTree();
    }

    private void mniAddFolder_Click(object sender, System.EventArgs e)
    {
      object ParentID;
      if (tvHosts.SelectedNode.Tag == null)
      {
        ParentID = DBNull.Value;
      }
      else
      {
        DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
        if ((!object.ReferenceEquals(dr["IsHost"], DBNull.Value)) && dr["IsHost"].ToString() == "Y")
        {
          MessageBox.Show("You can only add children to folder nodes!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          return; 
        }
        ParentID = dr["ID"];
      }

      DataRow r = dtHosts.NewRow();
      r["Host"] = "New folder";
      r["IsHost"] = DBNull.Value;
      r["ShowInMonitor"] = DBNull.Value;
      r["DoPing"] = DBNull.Value;
      r["PingFreq"] = DBNull.Value;
      r["IDparent"] = ParentID;
      dtHosts.Rows.Add(r);

      daHosts.Update(dtHosts);
      ReLoadTree();
    }

    private void tvHosts_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {
      if (tvHosts.SelectedNode.Tag == null)
      {
        // Root node
        pnlDetail.Visible = false;
      }
      else
      {
        pnlDetail.Visible = true;
        DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
        if ((!object.ReferenceEquals(dr["IsHost"], DBNull.Value)) && dr["IsHost"].ToString() == "Y")
        {
          // Host node
          chkDoPing.Visible = true;
          lblPingFreq.Visible = true;
          txtPingFreq.Visible = true;
          chkShowInMonitor.Visible = true;
          txtHost.Text = dr["Host"].ToString();
          if ((!object.ReferenceEquals(dr["DoPing"], DBNull.Value)) && dr["DoPing"].ToString() == "Y")
          {
            chkDoPing.Checked = true;
          }
          else
          {
            chkDoPing.Checked = false;
          }
          txtPingFreq.Text = dr["PingFreq"].ToString();
          if ((!object.ReferenceEquals(dr["ShowInMonitor"], DBNull.Value)) && dr["ShowInMonitor"].ToString() == "Y")
          {
            chkShowInMonitor.Checked = true;
          }
          else
          {
            chkShowInMonitor.Checked = false;
          }
        }
        else
        {
          // Folder node
          chkDoPing.Visible = false;
          lblPingFreq.Visible = false;
          txtPingFreq.Visible = false;
          chkShowInMonitor.Visible = false;
          txtHost.Text = dr["Host"].ToString();
        }
      }
    }

    private void chkDoPing_CheckedChanged(object sender, System.EventArgs e)
    {
      try
      {
        if (tvHosts.SelectedNode.Tag == null) 
          return; 
      }
      catch
      {
        return; 
      }
      txtPingFreq.Enabled = chkDoPing.Checked;
      chkShowInMonitor.Enabled = chkDoPing.Checked;
      DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
      if (chkDoPing.Checked)
      {
        dr["DoPing"] = "Y";
      }
      else
      {
        dr["DoPing"] = DBNull.Value;
      }
      daHosts.Update(dtHosts);
    }

    private void chkShowInMonitor_CheckedChanged(object sender, System.EventArgs e)
    {
      try
      {
        if (tvHosts.SelectedNode.Tag == null) 
          return; 
      }
      catch
      {
        return; 
      }
      DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
      if (chkShowInMonitor.Checked)
      {
        dr["ShowInMonitor"] = "Y";
      }
      else
      {
        dr["ShowInMonitor"] = DBNull.Value;
      }
      daHosts.Update(dtHosts);
    }

    private void txtPingFreq_LostFocus(object sender, System.EventArgs e)
    {
      try
      {
        if (tvHosts.SelectedNode.Tag == null) 
          return; 
      }
      catch
      {
        return; 
      }
      DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
      if (Information.IsNumeric(txtPingFreq.Text))
      {
        dr["PingFreq"] = int.Parse(txtPingFreq.Text);
        daHosts.Update(dtHosts);
      }
    }

    private void txtHost_LostFocus(object sender, System.EventArgs e)
    {
      try
      {
        if (tvHosts.SelectedNode.Tag == null) 
          return; 
      }
      catch
      {
        return; 
      }
      DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
      if (txtHost.Text.Trim() != "")
      {
        tvHosts.SelectedNode.Text = txtHost.Text.Trim();
        dr["Host"] = txtHost.Text.Trim();
        daHosts.Update(dtHosts);
      }
    }

    private void mniAddHost_Click(object sender, System.EventArgs e)
    {
      object ParentID;
      if (tvHosts.SelectedNode.Tag == null)
      {
        ParentID = DBNull.Value;
      }
      else
      {
        DataRowView dr = (DataRowView)tvHosts.SelectedNode.Tag;
        if ((!object.ReferenceEquals(dr["IsHost"], DBNull.Value)) && dr["IsHost"].ToString() == "Y")
        {
          MessageBox.Show("You can only add children to folder nodes!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          return; 
        }
        ParentID = (int)dr["ID"];
      }

      DataRow r = dtHosts.NewRow();
      r["Host"] = "New host";
      r["IsHost"] = "Y";
      r["ShowInMonitor"] = "Y";
      r["DoPing"] = "Y";
      r["PingFreq"] = 60;
      r["IDparent"] = ParentID;
      dtHosts.Rows.Add(r);

      daHosts.Update(dtHosts);
      ReLoadTree();
    }

  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions