Coder Social home page Coder Social logo

ipaddresscontrollib's Introduction

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

  • AnyBlank: Gets whether any fields in the control are blank.
  • AutoHeight: Gets or sets a value indicating whether the control is sized according to the current font and border. 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. Default value is BorderStyle.Fixed3D.
  • IPAddress: Gets or sets an IPAddress representing the contents of the fields. (VS2010 only)
  • 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.

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.

ipaddresscontrollib's People

Contributors

m66n avatar stamepicmorg avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.