本文转自http://www.codeproject.com/KB/miscctrl/IpAddrCtrlLib.aspx
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.
IPAddressControl
is really a composite UserControl
that aggregates four specialized TextBox
controls of type FieldCtrl
and three specialized Control
s of type DotCtrl
. Here's a picture:
The FieldCtrl
s do some validation and keyboard filtering, in addition to standard TextBox
behavior. The DotCtrl
s do nothing but draw a dot.
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.
AutoHeight
: get
s or set
s a value indicating whether the control is automatically sized vertically according to the current font and border. The default value is true
. Blank
: get
s a value indicating whether all of the fields in the control are empty. BorderStyle
: get
s or set
s the border style of the control. The default value is BorderStyle.Fixed3D
. ReadOnly
: get
s or set
s a value indicating if the control is read-only. Clear
: Clears the contents of the control. GetAddressBytes
: Returns an array of byte
s representing the contents of the fields, index 0 being the leftmost field. SetAddressBytes
: set
s the values of the fields using an array of byte
s, index 0 being the leftmost field. SetFieldFocus
: set
s the keyboard focus to the specified field in the control. SetFieldRange
: set
s 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() );
KeyDown
, KeyUp
, and PreviewKeyDown
events. Keys.Enter
and Keys.Return
will now propagate a KeyPress
event. ReadOnly
should now really be read-only. Thanks to t_suzuki for reporting this bug. AllowInternalTab
and AnyBlank
properties. null
check for SetAddressBytes()
. AutoSize
. Use AutoHeight
instead. null
when parsing incoming text. MinimumSize
property of DotControl
to tighten up the spacing. 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. Focused
property. Thanks to Mario for reporting this. OnTextChanged()
for the control when the text of any field changes. Thanks to Bertrand for pointing that out.