A C# IP Address Control
A C# analogue to the MFC CIPAddressCtrl
- Download demo project - 34.2 KB
- Download source - 20.5 KB
- Download demo project [VS05] - 36 KB
- Download source [VS05] - 20 KB
- Download demo project [VS08] - 36.2 KB
- Download source [VS08] - 20.2 KB

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# here, and hopefully I've succeeded.
Background
IPAddressControl is really a composite UserControl that aggregates four specialized TextBox controls of type FieldCtrl and three specialized Controls of type DotCtrl. Here's a picture:

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. The interface to IPAddressControl is very simple.
Public Instance Properties
AutoHeight:gets orsets a value indicating whether the control is automatically sized vertically according to the current font and border. The default value istrue.Blank:gets a value indicating whether all of the fields in the control are empty.BorderStyle:gets orsets the border style of the control. The default value isBorderStyle.Fixed3D.ReadOnly:gets orsets a value indicating if the control is read-only.
Public Instance Methods
Clear: Clears the contents of the control.GetAddressBytes: Returns an array ofbytes representing the contents of the fields, index 0 being the leftmost field.SetAddressBytes:sets the values of the fields using an array ofbytes, index 0 being the leftmost field.SetFieldFocus:sets the keyboard focus to the specified field in the control.SetFieldRange:sets the lower and higher range of a specified field in the control.
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. The 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 fields 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() );
History
- 27 Apr 2008
- Added propagation of
KeyDown,KeyUp, andPreviewKeyDownevents.Keys.EnterandKeys.Returnwill now propagate aKeyPressevent.
- Added propagation of
- 23 Oct 2007
ReadOnlyshould now really be read-only. Thanks to t_suzuki for reporting this bug.
- 27 Sep 2007
- Added proper event propagation for focus, keypress, and some mouse events.
- Added
AllowInternalTabandAnyBlankproperties. - Removed superfluous code.
- Removed a potential resource leak when calculating text size and added a
nullcheck forSetAddressBytes(). - Compliant with FxCop 1.35.
- 13 Jun 2007
- Text set in design mode is persisted.
- Removed override of
AutoSize. UseAutoHeightinstead. - [VS05] Modified size calculations to conserve horizontal space.
- 6 Mar 2007
- Now checks for
nullwhen parsing incoming text.
- Now checks for
- 21 Feb 2007
- Added handling of [Backspace] across fields. Thanks to Antony for reporting this bug.
- Added better handling of [Delete], and new handlers for [Home] and [End].
- [VS05] Modified the
MinimumSizeproperty ofDotControlto tighten up the spacing.
- 5 May 2006
- [VS05] Added
Baselineto theSnapLinescollection for theControlDesignerclass. Made theTextproperty browsable in design mode. Fixed the control sizing bug when large fonts are used.
- [VS05] Added
- 13 Oct 2005
- Compliant with FxCop 1.32.
- 17 Sep 2005
- Enhanced to support Windows XP visual styles. Thanks to Carlos for requesting this.
- 3 Aug 2005
- Bug fix for
Focusedproperty. Thanks to Mario for reporting this.
- Bug fix for
- 22 Mar 2005
- Added a call to
OnTextChanged()for the control when the text of any field changes. Thanks to Bertrand for pointing that out.
- Added a call to
- 6 Feb 2005
- 20 Jan 2005 - Initial release.
