Get Internet Connection State (Re-Release)

lxXTaCoXxl

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
123 Views
About the Author

I'm a very difficult person to describe because I'm all over the place. If I could sum me up into one word it would be insane. I'm an incredibly vibrant person. I love surfing, playing guitar, programming, photography, video games, skating, snowboarding, running parkour, hanging out with friends, spending time with my girlfriend, and much more. I'm currently enrolled in college courses and pursuing jobs in my area. Ask if you want to know more.

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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.