Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

A IP Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Shelton - 22 Jul 2004 06:06 GMT
I was goofing around tonight and decided to write a little IP address
control.  I had written a simple one a long time ago, but I decided I
should try to write one that was half way correct :)  Anyway, here is the
result of about an hour and half of work...

// IPControl.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Net;
using System.Runtime.InteropServices;

namespace FireAnt.Controls
{

    /// <summary>
    /// Simple API Based IP Control
    /// </summary>
    public class IPControl : System.Windows.Forms.UserControl
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private NativeIPControl ipControl;

        public event FieldChangedDelegate FieldChanged;

        public IPControl()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // create the native ip control
            this.ipControl =
                new NativeIPControl(this.Handle, this.Height, this.Width);
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        /// <summary>
        /// Overridden window procedure
        /// </summary>
        /// <param name="m">Window message to process</param>
        protected override void WndProc(ref Message m)
        {
            // we need to over ride this so that we
            // can receive notification messages from the
            // native ip control window...
            switch (m.Msg)
            {
                case Win32Interop.WM_NOTIFY:
                    unsafe
                    {
                        Win32Interop.NMHDR* nmhdr = (Win32Interop.NMHDR*) m.LParam;
                        if (nmhdr->code == Win32Interop.IPN_FIELDCHANGED)
                        {
                            Win32Interop.NMIPADDRESS* nmaddr
                                = (Win32Interop.NMIPADDRESS*) m.LParam;
                           
                            // fire a field changed event...
                            FieldChangedEventArgs e
                                = new FieldChangedEventArgs(nmaddr->iField, nmaddr->iValue);
                            if (this.FieldChanged != null)
                            {
                                this.FieldChanged (this, e);
                            }

                            // change the feild value...
                            nmaddr->iValue = e.Value;
                        }
                        else
                        {
                            base.WndProc(ref m);
                        }
                    }
                    break;
                default:
                    base.WndProc (ref m);
                    break;
            }
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            //
            // FAIPControl
            //
            this.Name = "IPControl";
            this.Size = new System.Drawing.Size(132, 20);
            this.Enter += new System.EventHandler(this.IPControl_Enter);

        }
        #endregion

        /// <summary>
        /// Get/Set the IP address for the control
        /// </summary>
        [Browsable(false),
            ReadOnly(true)]
        public IPAddress Address
        {
            get
            {
                uint address = 0;

                if (Win32Interop.SendMessage (
                    this.ipControl.Handle,
                    Win32Interop.IPM_ISBLANK,
                    0,
                    0) == 0)
                {
                    // get the address
                    Win32Interop.SendMessage (
                        this.ipControl.Handle,
                        Win32Interop.IPM_GETADDRESS,
                        0,
                        out address
                        );

                    // we need to reverse the order...
                    byte[] buffer =    BitConverter.GetBytes(address);
                    System.Array.Reverse(buffer, 0, buffer.Length);
                    address = BitConverter.ToUInt32(buffer, 0);
                }

                return new IPAddress(address);
            }
            set
            {
                // we need to get the bytes, and reverse the order
                byte[] buffer = value.GetAddressBytes();
                System.Array.Reverse(buffer, 0,    buffer.Length);
                uint address = BitConverter.ToUInt32(buffer, 0);

                // now set the value
                Win32Interop.SendMessage (
                    this.ipControl.Handle,
                    Win32Interop.IPM_SETADDRESS,
                    0,
                    address
                    );
            }
        }

        /// <summary>
        /// Clear the control
        /// </summary>
        public void Clear()
        {
            // clear the ip values...
            Win32Interop.SendMessage (
                this.ipControl.Handle,
                Win32Interop.IPM_CLEARADDRESS,
                0,
                0
                );
        }

        /// <summary>
        /// Set focus to a particular field in the control
        /// </summary>
        /// <param name="field">The zero based field index to set focus
to</param>
        public void Focus(int field)
        {
            if (field < 0 || field > 3)
            {
                throw new ArgumentOutOfRangeException (
                    "field",
                    field,
                    "Field must be between 0 and 3");
            }

            Win32Interop.SendMessage (
                this.ipControl.Handle,
                Win32Interop.IPM_SETFOCUS,
                (uint) field, 0
                );
        }

        private void IPControl_Enter(object sender, System.EventArgs e)
        {
            // set the focus to our ip control...
            Win32Interop.SendMessage (
                this.ipControl.Handle,
                Win32Interop.IPM_SETFOCUS,
                0,
                0
                );
        }

        /// <summary>
        /// Native Windows IP Control Class
        /// </summary>
        private class NativeIPControl : System.Windows.Forms.NativeWindow
        {
            public NativeIPControl(IntPtr parent, int height, int width)
            {
                // create the create params structure
                CreateParams cp = new CreateParams();

                // fill it in...
                cp.ClassName = Win32Interop.WC_IPADDRESS;
                cp.Style = Win32Interop.WS_CHILD | Win32Interop.WS_VISIBLE;
                       cp.X = 0;
                cp.Y = 0;
                cp.Height = height;
                cp.Width = width;
                cp.Parent = parent;
               
                // create the window
                this.CreateHandle(cp);
            }

            // we'll probably need this latter...
            protected override void WndProc(ref Message m)
            {
                // for now just call the default procedure
                base.DefWndProc(ref m);
            }

        }
   
    }

    public class FieldChangedEventArgs : System.EventArgs
    {
        private int field;
        private int newValue;

        internal FieldChangedEventArgs(int field, int newValue)
        {
            this.field = field;
            this.newValue = newValue;
        }

        public int Field
        {
            get
            {
                return this.field;
            }
        }

        public int Value
        {
            get
            {
                return this.newValue;
            }
            set
            {
                if (value >= 0 && value <= 255)
                {
                    this.newValue = value;
                }
            }
        }
    }

    public delegate void FieldChangedDelegate (
        object sender, FieldChangedEventArgs e);

}

// Win32Interop.cs
using System;
using System.Runtime.InteropServices;

namespace FireAnt.Controls
{
    /// <summary>
    /// Win32 Interop Definitions
    /// </summary>
    internal sealed class Win32Interop
    {
        // Window Style Constants
        public const int WS_CHILD   = 0x40000000;
        public const int WS_VISIBLE = 0x10000000;

        // Window Class Names
        public const string WC_IPADDRESS = "SysIPAddress32";

        // IP Control Window Messages
        public const uint IPM_CLEARADDRESS = (WM_USER+100); // no parameters
        public const uint IPM_SETADDRESS   = (WM_USER+101); // lparam = TCP/IP
address
        public const uint IPM_GETADDRESS   = (WM_USER+102); // lresult = # of non
black fields.  lparam = LPDWORD for TCP/IP address
        public const uint IPM_SETRANGE      = (WM_USER+103); // wparam = field,
lparam = range
        public const uint IPM_SETFOCUS      = (WM_USER+104); // wparam = field
        public const uint IPM_ISBLANK      = (WM_USER+105); // no parameters

        // IP Control Notification Messages
        public const int  WM_NOTIFY          = 0x004E;
        public const uint IPN_FIELDCHANGED = (IPN_FIRST - 0);

        // IP Control Structures
        [StructLayout(LayoutKind.Sequential)]
        public struct NMHDR
        {
            public IntPtr hwndFrom;
            public uint idFrom;
            public uint code;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct NMIPADDRESS
        {
            public NMHDR hdr;
            public int iField;
            public int iValue;
        }

        #region API Function Calls
        [DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern uint SendMessage (
            IntPtr hWnd,
            uint Msg,
            uint wParam,
            uint lParam
            );

        [DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern uint SendMessage (
            IntPtr hWnd,
            uint Msg,
            uint wParam,
            out uint lParam
            );
        #endregion

        #region Private Data
        // private window message constants
        private const uint WM_USER    = 0x0400;
        private const uint IPN_FIRST = unchecked(0U-860U); // internet address
        private const uint IPN_LAST  = unchecked(0U-879U); // internet address
        private Win32Interop() {}
        #endregion

    }
}

Sorry, the code is in C# - but I thought I'd share it with you guys anyway.

HTH
Signature

Tom Shelton [MVP]

Cor Ligthert - 22 Jul 2004 07:53 GMT
Hi Tom,

Thanks for sending this, however when I would do it this way I had the
change to be flamed by Herfried.

:-)

And than I would laugh.

Cor

> I was goofing around tonight and decided to write a little IP address
> control.  I had written a simple one a long time ago, but I decided I
[quoted text clipped - 365 lines]
>
> HTH
scorpion53061 - 24 Oct 2004 12:57 GMT
This is really cool.

> I was goofing around tonight and decided to write a little IP address
> control.  I had written a simple one a long time ago, but I decided I
[quoted text clipped - 368 lines]
>
> HTH

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.