954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Get Internet Connection State (Re-Release)

By Jamie on Jan 2nd, 2012 9:59 pm

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.

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;
            }
        }
    }
}

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

lxXTaCoXxl
Posting Whiz
300 posts since Mar 2011
Reputation Points: 22
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You