Click here to Skip to main content
12,351,441 members (36,819 online)
Click here to Skip to main content
Add your own
alternative version

Stats

419.8K views
26.6K downloads
290 bookmarked
Posted

A C# IP Address Control

, 28 Apr 2008 MIT
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

mid=5741
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralRe: Problem under Vista with larger font sizes Pin
Michael Chapman26-Jun-08 5:08
memberMichael Chapman26-Jun-08 5:08 
GeneralRe: Problem under Vista with larger font sizes Pin
StefanFrank26-Jun-08 5:23
memberStefanFrank26-Jun-08 5:23 
General[Message Deleted] Pin
Michael Chapman26-Jun-08 9:37
memberMichael Chapman26-Jun-08 9:37 
GeneralRe: Problem under Vista with larger font sizes Pin
StefanFrank26-Jun-08 21:30
memberStefanFrank26-Jun-08 21:30 
AnswerRe: Problem under Vista with larger font sizes Pin
vincio7723-Mar-09 3:57
membervincio7723-Mar-09 3:57 
GeneralRe: Problem under Vista with larger font sizes Pin
vincio7723-Mar-09 21:51
membervincio7723-Mar-09 21:51 
GeneralIPAddressControl -->public void Clear() need a Invoke Pin
wangkuang522-May-08 16:12
memberwangkuang522-May-08 16:12 
GeneralRe: IPAddressControl.Clear() needs an InvokeRequired Pin
Michael Chapman22-May-08 16:51
memberMichael Chapman22-May-08 16:51 
GeneralRe: IPAddressControl.Clear() needs an InvokeRequired Pin
wangkuang522-May-08 18:07
memberwangkuang522-May-08 18:07 
GeneralRe: IPAddressControl.Clear() needs an InvokeRequired Pin
Michael Chapman22-May-08 19:17
memberMichael Chapman22-May-08 19:17 
GeneralEvents Pin
amanda08926-Apr-08 19:14
memberamanda08926-Apr-08 19:14 
GeneralRe: Events Pin
Michael Chapman27-Apr-08 4:07
memberMichael Chapman27-Apr-08 4:07 
AnswerRe: Events Pin
Michael Chapman27-Apr-08 7:42
memberMichael Chapman27-Apr-08 7:42 
QuestionQuestion... Pin
amanda08927-Apr-08 11:54
memberamanda08927-Apr-08 11:54 
GeneralRe: Question... Pin
Michael Chapman27-Apr-08 12:11
memberMichael Chapman27-Apr-08 12:11 
GeneralRe: Question... Pin
amanda08927-Apr-08 12:44
memberamanda08927-Apr-08 12:44 
GeneralThank you! Pin
mory198425-Feb-08 23:55
membermory198425-Feb-08 23:55 
GeneralRe: Thank you! Pin
Michael Chapman26-Feb-08 5:35
memberMichael Chapman26-Feb-08 5:35 
GeneralVS 2008 Project Pin
great_scandinavian13-Dec-07 20:51
membergreat_scandinavian13-Dec-07 20:51 
GeneralRe: VS 2008 Project Pin
Michael Chapman14-Dec-07 5:36
memberMichael Chapman14-Dec-07 5:36 
GeneralRe: VS 2008 Project Pin
great_scandinavian15-Dec-07 19:25
membergreat_scandinavian15-Dec-07 19:25 
GeneralRe: VS 2008 Project Pin
Michael Chapman16-Dec-07 9:12
memberMichael Chapman16-Dec-07 9:12 
NewsBug Fix: ReadOnly property Pin
Michael Chapman22-Oct-07 22:03
memberMichael Chapman22-Oct-07 22:03 
GeneralReadOnly property Pin
t_suzuki17-Oct-07 21:50
membert_suzuki17-Oct-07 21:50 
GeneralRe: ReadOnly property Pin
Michael Chapman18-Oct-07 4:45
memberMichael Chapman18-Oct-07 4:45 
GeneralRe: ReadOnly property Pin
t_suzuki18-Oct-07 14:10
membert_suzuki18-Oct-07 14:10 
GeneralRe: ReadOnly property Pin
Michael Chapman18-Oct-07 16:21
memberMichael Chapman18-Oct-07 16:21 
NewsUpdate Available Pin
Michael Chapman28-Sep-07 9:56
memberMichael Chapman28-Sep-07 9:56 
GeneralSimplified Chinese XP Pin
disore24-Sep-07 2:00
memberdisore24-Sep-07 2:00 
GeneralRe: Simplified Chinese XP Pin
Michael Chapman24-Sep-07 4:07
memberMichael Chapman24-Sep-07 4:07 
GeneralRe: Simplified Chinese XP Pin
disore24-Sep-07 8:22
memberdisore24-Sep-07 8:22 
GeneralRe: Simplified Chinese XP Pin
Michael Chapman24-Sep-07 8:47
memberMichael Chapman24-Sep-07 8:47 
GeneralRe: Simplified Chinese XP Pin
Michael Chapman24-Sep-07 21:05
memberMichael Chapman24-Sep-07 21:05 
GeneralRe: Simplified Chinese XP Pin
disore24-Sep-07 22:29
memberdisore24-Sep-07 22:29 
GeneralRe: Simplified Chinese XP Pin
Michael Chapman25-Sep-07 8:04
memberMichael Chapman25-Sep-07 8:04 
GeneralRe: Simplified Chinese XP Pin
disore25-Sep-07 20:26
memberdisore25-Sep-07 20:26 
GeneralRe: Simplified Chinese XP Pin
Michael Chapman25-Sep-07 22:17
memberMichael Chapman25-Sep-07 22:17 
GeneralRe: Simplified Chinese XP Pin
disore26-Sep-07 0:58
memberdisore26-Sep-07 0:58 
GeneralRe: Simplified Chinese XP [modified] Pin
Michael Chapman26-Sep-07 7:17
memberMichael Chapman26-Sep-07 7:17 
GeneralIPv6 support Pin
puglovrs20-Sep-07 9:47
memberpuglovrs20-Sep-07 9:47 
GeneralRe: IPv6 support Pin
Michael Chapman20-Sep-07 9:51
memberMichael Chapman20-Sep-07 9:51 
GeneralCombobox Pin
Maxim V.2-Sep-07 8:55
memberMaxim V.2-Sep-07 8:55 
GeneralRe: Combobox Pin
visualhint10-Sep-07 5:41
membervisualhint10-Sep-07 5:41 
GeneralRe: Combobox Pin
Maxim V.23-Sep-07 17:04
memberMaxim V.23-Sep-07 17:04 
GeneralRe: Combobox Pin
visualhint24-Sep-07 1:52
membervisualhint24-Sep-07 1:52 
GeneralProblem when I embedded it into my C++ App Pin
EdisonCPP20-Jul-07 7:17
memberEdisonCPP20-Jul-07 7:17 
GeneralRe: Problem when I embedded it into my C++ App Pin
Michael Chapman20-Jul-07 8:03
memberMichael Chapman20-Jul-07 8:03 
NewsBug Fix: Inconsistent KeyPress, GotFocus, and LostFocus events Pin
Michael Chapman2-Jul-07 18:16
memberMichael Chapman2-Jul-07 18:16 
NewsIP Address validation Pin
stotti_no128-Jun-07 6:28
memberstotti_no128-Jun-07 6:28 
AnswerRe: IP Address validation Pin
stotti_no128-Jun-07 6:32
memberstotti_no128-Jun-07 6:32 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 28 Apr 2008
Article Copyright 2005 by mid=5741
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid