This article has been dead for over three months
You
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace InternetConnectionState
{
public delegate void ConnectionEvent(object sender, EventArgs e);
public partial class InternetConnectionState : UserControl
{
public InternetConnectionState()
{
InitializeComponent();
this.MaximumSize = new Size(170, 22);
this.MinimumSize = new Size(170, 22);
}
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int desc, int reserved);
public enum ConnectionState
{
Disconnected,
Connected
}
public ConnectionState InternetConnection
{
get
{
int desc;
return (InternetGetConnectedState(out desc, 0)) ?
ConnectionState.Connected :
ConnectionState.Disconnected;
}
}
[Description("Occurs while disconnected to the internet.")]
public event ConnectionEvent WhileConnected;
[Description("Occurs while connected to the internet.")]
public event ConnectionEvent WhileDisconnected;
public virtual void Connected(object sender, EventArgs e)
{
if (WhileConnected != null)
WhileConnected(sender, e);
}
public virtual void Disconnected(object sender, EventArgs e)
{
if (WhileDisconnected != null)
WhileDisconnected(sender, e);
}
private void InternetConnectionState_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (InternetConnection == ConnectionState.Connected)
{
label1.Text = "State: Connected";
label1.ForeColor = Color.Green;
ovalShape1.BorderColor = Color.Lime;
ovalShape1.FillColor = Color.Lime;
ovalShape1.FillGradientColor = Color.Green;
}
else
{
label1.Text = "State: Disconnected";
label1.ForeColor = Color.Red;
ovalShape1.BorderColor = Color.Red;
ovalShape1.FillColor = Color.Red;
ovalShape1.FillGradientColor = Color.Maroon;
}
}
}
}