Click here to Skip to main content
15,899,026 members
Articles / Programming Languages / C#
Article

A C# IP Address Control

Rate me:
Please Sign up or sign in to vote.
4.72/5 (98 votes)
28 Apr 2008MIT4 min read 789.5K   39.8K   297   195
A C# analogue to the MFC CIPAddressCtrl

Screenshot - TestIPAddressControl.png

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:

Screenshot - close_up.gif

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 or sets a value indicating whether the control is automatically sized vertically according to the current font and border. The default value is true.
  • Blank: gets a value indicating whether all of the fields in the control are empty.
  • BorderStyle: gets or sets the border style of the control. The default value is BorderStyle.Fixed3D.
  • ReadOnly: gets or sets a value indicating if the control is read-only.

Public Instance Methods

  • Clear: Clears the contents of the control.
  • GetAddressBytes: Returns an array of bytes representing the contents of the fields, index 0 being the leftmost field.
  • SetAddressBytes: sets the values of the fields using an array of bytes, 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:

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

History

  • 27 Apr 2008
    • Added propagation of KeyDown, KeyUp, and PreviewKeyDown events. Keys.Enter and Keys.Return will now propagate a KeyPress event.
  • 23 Oct 2007
    • ReadOnly should 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 AllowInternalTab and AnyBlank properties.
    • Removed superfluous code.
    • Removed a potential resource leak when calculating text size and added a null check for SetAddressBytes().
    • Compliant with FxCop 1.35.
  • 13 Jun 2007
    • Text set in design mode is persisted.
    • Removed override of AutoSize. Use AutoHeight instead.
    • [VS05] Modified size calculations to conserve horizontal space.
  • 6 Mar 2007
    • Now checks for null when parsing incoming text.
  • 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 MinimumSize property of DotControl to tighten up the spacing.
  • 5 May 2006
    • [VS05] Added Baseline to the SnapLines collection for the ControlDesigner class. Made the Text property browsable in design mode. Fixed the control sizing bug when large fonts are used.
  • 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 Focused property. Thanks to Mario for reporting this.
  • 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.
  • 6 Feb 2005
    • Added missing key handlers. Thanks to Norm and Ed for the heads-up on missing key handlers.
    • Added sizing.
    • Added ReadOnly property.
    • Non-standard color choices now render properly.
    • Added an event to notify clients of text change.
  • 20 Jan 2005 - Initial release.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: One other way Pin
mid=574123-Jan-05 16:38
mid=574123-Jan-05 16:38 
GeneralRe: One other way Pin
NormDroid23-Jan-05 20:35
professionalNormDroid23-Jan-05 20:35 
GeneralRe: One other way Pin
mid=574124-Jan-05 5:35
mid=574124-Jan-05 5:35 
GeneralRe: One other way Pin
mid=574124-Jan-05 9:26
mid=574124-Jan-05 9:26 
GeneralRe: Once other way Pin
NormDroid23-Jan-05 20:38
professionalNormDroid23-Jan-05 20:38 
GeneralRe: One other way Pin
mid=574124-Jan-05 6:09
mid=574124-Jan-05 6:09 
GeneralRe: One other way Pin
NormDroid24-Jan-05 20:52
professionalNormDroid24-Jan-05 20:52 
GeneralRe: One other way Pin
SonicEd2-Feb-05 10:07
SonicEd2-Feb-05 10:07 
Michael,

This is an excellent control. I've been looking for something that works like the Windows IP Address control. I was able to load that control after much effort. I should have read these replies first! I am interested in seeing th code you created to use the Windows IP Address control. Could you please send it to me also. I will email you at the given address and ask there too.

I noticed that this control did not behave exactly as the Windows IP Address control, so I made the changes required to get it to do so. I will email these updates also, so that you can post them if you like. I added the ability to navigate backwards with the Left key and tab backwards with the Shift+Tab if tabbing is enabled. I added advancing to the next field with the Space key. I fixed the cursor position when editing before the last digit. The cursor was always being advanced to the end of the field. I added the functionality to go back to the previous field and delete from the end when the BackSpace key is pressed at the begining of a field if it's not the first field. When navigavting left or right with the Left or Right keys from one field to the next, the whole field is no longer selected. Instead the cursor is now placed in the proper position.

That's all that I can remember now. Let me know if I missed something, but I think I got it to function almost exactly like the Windows IP Address control. I also fixed the TrackBar control in the TestCtrl application. The event to set the field focus on needs to be MouseUp in this case. If the trackbar is already on the desired position, then it won't set the focus because the value didn't change. I removed the trackBar1_ValueChanged event method, because it is no longer needed.

I can't believe the timeliness of your post. I've wanted this control for over a year now , but I didn't have the time to build it. I decided last week I would find a way to get the Windows IP Address control to work in .NET. I came across your post, but I didn't think it was what I was looking for. After an associate at work lead me to this post again, I decided to see if I could make it work like the Windows IP Address control. And with a little effort, I was able to do so! Thank you very much for creating this control and sharing it with us!

Regards,

Ed Cool | :cool:
GeneralRe: One other way Pin
mid=57412-Feb-05 10:20
mid=57412-Feb-05 10:20 
GeneralRe: One other way Pin
Richard Deeming30-Mar-05 7:15
mveRichard Deeming30-Mar-05 7:15 
GeneralRe: One other way Pin
mid=574130-Mar-05 7:43
mid=574130-Mar-05 7:43 
GeneralRe: One other way Pin
Richard Deeming30-Mar-05 23:51
mveRichard Deeming30-Mar-05 23:51 
GeneralRe: One other way Pin
mid=574131-Mar-05 4:43
mid=574131-Mar-05 4:43 
GeneralRe: One other way Pin
Almighty Bob10-Jun-05 18:17
Almighty Bob10-Jun-05 18:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.