m66n / ipaddresscontrollib

Automatically exported from code.google.com/p/ipaddresscontrollib
Other
61 stars 25 forks source link

NuGet

ipaddresscontrollib

alt IPAddressControl in use

Introduction

Why didn't Microsoft include an IP address control in the stock toolbox for Visual Studio .NET? I needed something similar to the MFC CIPAddressCtrl class in a C# application recently, and was forced to roll my own. I tried to mimic the behavior of CIPAddressCtrl using C#, and hopefully I've succeeded.

If you need a C# IPv6 address control or a C# MAC address control, take a look at this project.

Background

IPAddressControl is a ContainerControl that aggregates four specialized TextBox controls of type FieldCtrl and three specialized Controls of type DotCtrl.

alt close up view of IPAddressControl

The FieldCtrls do some validation and keyboard filtering in addition to standard TextBox behavior. The DotCtrls do nothing but draw a dot.

Using the Code

Once the library containing IPAddressControl (IPAddressControlLib.dll) is built, add the control to the Toolbox in Visual Studio. From the Toolbox, just drag the control onto a form and you're ready to go.

NB: When using this code with .NET Framework 4, the WinForms application should target ".NET Framework 4" rather than the default ".NET Framework 4 Client Profile."

The interface to IPAddressControl is very simple.

Public Instance Properties

Public Instance Methods

The above properties and methods are in addition to the stock properties and methods of UserControl. Stock properties such as Text, Enabled, and Font, as well as stock methods such as ToString() work as expected.

Client code can register a handler for the public event, FieldChangedEvent, to be notified when any text in the fields of the control changes.

Note that Text and ToString() may not return the same value. If there are any empty fields in the control, Text will return a value that will reflect the empty fields. ToString() will fill in any empty field with that field's RangeLower value. Also, if you are using the control to create an IPAddress, you can easily do so using this control's GetAddressBytes() method:

IPAddress ipAddress = new IPAddress( ipAddressControl.GetAddressBytes() );

Update: The VS2010 version of this code adds an IPAddress property that renders the above unnecessary.

Copyright (c) Michael Chapman. All rights reserved.