Click here to Skip to main content
6,629,377 members and growing! (21,329 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Beginners     Intermediate License: The MIT License

A C# IP Address Control

By mid=5741

A C# analogue to the MFC CIPAddressCtrl
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:19 Jan 2005
Updated:28 Apr 2008
Views:197,834
Bookmarked:232 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
73 votes for this article.
Popularity: 8.36 Rating: 4.48 out of 5
2 votes, 2.7%
1

2
1 vote, 1.4%
3
16 votes, 21.9%
4
54 votes, 74.0%
5

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

About the Author

mid=5741


Member

Location: Canada Canada

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 180 (Total in Forum: 180) (Refresh)FirstPrevNext
Generaldisplay ordinary textbox shortcut menu Pinmemberpillesoft3:32 19 Nov '09  
General[HOWTO] Adding this control or another to VS Toolbox PinmemberVincent DUVERNET (Nolmë Informatique)23:32 2 Aug '09  
GeneralMDI Child Bug Pinmembermikerileyii6:50 21 May '09  
GeneralWrong layout in Korean XP Pinmemberdisore1:08 29 Apr '09  
GeneralGreat! Pinmemberjose_camacho22:28 25 Mar '09  
GeneralProblem with ShortCuts PinmemberStefanFrank7:14 11 Feb '09  
GeneralRe: Problem with ShortCuts Pinmembermid=57419:44 11 Feb '09  
GeneralRe: Problem with ShortCuts PinmemberStefanFrank5:17 23 Mar '09  
GeneralThanks PinmemberDr.Luiji22:53 28 Jan '09  
Generalgood article PinmemberDonsw18:30 17 Jan '09  
GeneralSmall request with the auto correction of too big value Pinmembersmudge14:17 7 Nov '08  
GeneralThank you ! Pinmembergaby_la_star2:40 12 Oct '08  
GeneralProblem under Vista with larger font sizes PinmemberStefanFrank1:08 26 Jun '08  
GeneralRe: Problem under Vista with larger font sizes PinmemberMichael Chapman4:25 26 Jun '08  
GeneralRe: Problem under Vista with larger font sizes PinmemberStefanFrank4:33 26 Jun '08  
GeneralRe: Problem under Vista with larger font sizes PinmemberMichael Chapman6:08 26 Jun '08  
GeneralRe: Problem under Vista with larger font sizes PinmemberStefanFrank6:23 26 Jun '08  
General[Message Deleted] PinmemberMichael Chapman10:37 26 Jun '08  
GeneralRe: Problem under Vista with larger font sizes PinmemberStefanFrank22:30 26 Jun '08  
AnswerRe: Problem under Vista with larger font sizes Pinmembervincio774:57 23 Mar '09  
GeneralRe: Problem under Vista with larger font sizes Pinmembervincio7722:51 23 Mar '09  
GeneralIPAddressControl -->public void Clear() need a Invoke Pinmemberwangkuang517:12 22 May '08  
GeneralRe: IPAddressControl.Clear() needs an InvokeRequired PinmemberMichael Chapman17:51 22 May '08  
GeneralRe: IPAddressControl.Clear() needs an InvokeRequired Pinmemberwangkuang519:07 22 May '08  
GeneralRe: IPAddressControl.Clear() needs an InvokeRequired PinmemberMichael Chapman20:17 22 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Apr 2008
Editor: Smitha Vijayan
Copyright 2005 by mid=5741
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project