Get Internet Connection State (Re-Release)

lxXTaCoXxl 1 Tallied Votes 150 Views Share

This release is an updated version of my previously released code snippet entitled "Get Internet Connection State". I've made this snippet into an actual control that you can add to your toolbox. It can be used with all versions of Visual Studio C#. I'm also posting the main source code, I'm still working on the two custom events for it.

TheForeRunner commented: This is a great tool. Thanks for sharing it. +0
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;
            }
        }
    }
}
lxXTaCoXxl 26 Posting Whiz in Training

I forgot to include that it even updates in designer mode.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.