Get Internet Connection State (Re-Release)

lxXTaCoXxl 1 Tallied Votes 129 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()
            this.MaximumSize = new Size(170, 22);
            this.MinimumSize = new Size(170, 22);

        private extern static bool InternetGetConnectedState(out int desc, int reserved);

        public enum ConnectionState

        public ConnectionState InternetConnection
                int desc;
                return (InternetGetConnectedState(out desc, 0)) ?
                            ConnectionState.Connected :

        [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)

        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;

                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, learning, and sharing knowledge.