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

IP TextBox

Rate me:
Please Sign up or sign in to vote.
3.34/5 (18 votes)
8 Sep 20051 min read 142.1K   14.8K   49   23
An IP textbox control that mimics ip boxes in windows network settings

Introduction

This simple IP address textbox replaces the lack of an IP box in Windows Forms control. Using the DLL you can just add the control to your toolbox and its use is very similar to a regular TextBox control.

Background (optional)

While writing an application for some end-users, i had the need for a textbox that only took an ip address and performed some validation to make sure the text entered was actually an IP. Since there was nothing readily availible that did what I wanted, I wrote a control that can easily be added to any .NET Windows Form.

Using the code

Using this control is very easy. Just right-click somewhere in your toolbox, then click "Add/Remove Items...". Now browse to the DLL, click OK and the control should now be in your toolbox. The control inherits the standard TextBox in Windows.Forms and overrides the Text property so that you can programmatically set or get the Text value as a string; I also added a method IsValid() which returns true if the Text property falls in the parameters of an actual IP address and false otherwise;

Points of Interest

The control is actually 4 TextBoxs and 3 Labels with "." placed at the appropriate locations. Most validation is done on KeyPress and currently a MessageBox pops up to alert the user of Invalid inputs such as numbers less than 0 or greater than 255.

History

No history yet, but thinking of being able to turn off the MessageBox warnings. There also is no validation when setting the Text programmatically and an unhandled exception is thrown.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions

 
QuestionNice job! Pin
ssiirroouuss30-Dec-16 17:10
ssiirroouuss30-Dec-16 17:10 
GeneralMy vote of 2 Pin
i0018-Jan-12 12:47
i0018-Jan-12 12:47 
GeneralMy vote of 5 Pin
DonaldKnuth29-Oct-11 22:45
DonaldKnuth29-Oct-11 22:45 
GeneralNeed Pin
Hardikasd25-Feb-10 1:23
Hardikasd25-Feb-10 1:23 
GeneralNice Pin
Member 33287121-Dec-09 0:59
Member 33287121-Dec-09 0:59 
GeneralNew code update Pin
sysx19-Nov-09 7:33
sysx19-Nov-09 7:33 
QuestionRegarding IP textbox Pin
rajionline20-Sep-07 17:55
rajionline20-Sep-07 17:55 
GeneralCheck IP Textbox if it's filled with a ip address Pin
Luneto_HA20-Mar-07 3:38
Luneto_HA20-Mar-07 3:38 
GeneralRe: Check IP Textbox if it's filled with a ip address Pin
avramik14-Jun-10 20:20
avramik14-Jun-10 20:20 
GeneralDataBinding Pin
joelthegeek1-Dec-05 5:31
joelthegeek1-Dec-05 5:31 
GeneralMovement needs to be a little better Pin
luniv04048-Nov-05 9:26
luniv04048-Nov-05 9:26 
GeneralRe: Movement needs to be a little better Pin
joelthegeek1-Dec-05 3:32
joelthegeek1-Dec-05 3:32 
GeneralNice Control ! A little question :doh: Pin
FredyAlfredo20-Sep-05 2:25
FredyAlfredo20-Sep-05 2:25 
GeneralRe: Nice Control ! A little question :doh: Pin
mawnkay27-Sep-05 4:16
mawnkay27-Sep-05 4:16 
AnswerRe: Nice Control ! A little question :doh: Pin
mawnkay4-Oct-05 4:44
mawnkay4-Oct-05 4:44 
GeneralLittle changes Pin
MansonP15-Sep-05 2:48
MansonP15-Sep-05 2:48 
GeneralRe: Little changes Pin
mawnkay27-Sep-05 4:21
mawnkay27-Sep-05 4:21 
GeneralRe: Little changes Pin
pinquotriot19-Mar-09 12:27
pinquotriot19-Mar-09 12:27 
General2 comments: Pin
Almighty Bob8-Sep-05 9:33
Almighty Bob8-Sep-05 9:33 
1. put up pictures

2. you can wrap the standard IP Address control w/ much less code (this is a quick & dirty wrapper. I have a much better implementation, but this is just from memory):

namespace NullFX.Controls	{
    using System;
    using System.Net;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    [StructLayout(LayoutKind.Sequential)]
    public struct Nmhdr	{
        public IntPtr HWndFrom;
        public int IdFrom;
        public int Code;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct NmIPAddress {
        public Nmhdr Hdr;
        public int Field;
        public int Value;
    }
    public enum	IPField	{OctetOne =	0,OctetTwo=1,OctetThree=2,OctetFour=3}
    public delegate	void TextChangedHandler(object sender, TextChangedEventArgs	e);
    public class TextChangedEventArgs :	EventArgs {
        private	int	_field,	_value;
        public int Field {
            get{return _field;}
        }
        public int Value {
            get{return _value;}
        }
        public TextChangedEventArgs(int	field, int value):base() {
            _field = field;
            _value = value;
        }
    }
    public class IPAddressControl : TextBox {
        private	const int WM_NOTIFY	= 0x004E,
            WM_USER	= 0x0400,
            WM_REFLECT = WM_USER + 0x1C00,
            IPM_SETRANGE = (WM_USER+103),
            IPM_GETADDRESS = (WM_USER+102),
            IPM_SETADDRESS = (WM_USER+101),
            IPM_CLEARADDRESS = (WM_USER+100),
            IPM_ISBLANK = (WM_USER+105);
        private	int[] values = new int[4];
        new	public event TextChangedHandler	TextChanged;
        public IPAddressControl() : base()	{
            for(int	i =	0; i < 4; i++)
                values[i] =	0;
        }
        protected virtual void OnTextChanged(TextChangedEventArgs e) {
            if(TextChanged != null)	TextChanged(this, e);
        }
        protected override CreateParams	CreateParams {
            get	{
                CreateParams cp	= base.CreateParams;
                cp.ClassName = "SysIPAddress32";
                cp.Height =	23;
                return cp;
            }
        }
        public bool	SetIPRange(IPField field, byte lowValue, byte highValue) {
            Message	m =	Message.Create(Handle, IPM_SETRANGE, (IntPtr)((int)field) ,	MakeRange(lowValue,	highValue));
            WndProc(ref	m);
            return m.Result.ToInt32() >	0;
        }
        public System.Net.IPAddress IPAddress {
            get{
                return IPAddress.Parse(base.Text);
            }
        }
        public bool IsBlank {
            get{
                Message m = Message.Create(Handle, IPM_ISBLANK, IntPtr.Zero, IntPtr.Zero);
                WndProc(ref m);
                return m.Result.ToInt32() > 0;
            }
        }
        new public void Clear() {
            Message m = Message.Create(Handle, IPM_CLEARADDRESS, IntPtr.Zero, IntPtr.Zero);
            WndProc(ref m); 
        }
        private System.Net.IPAddress GetIpAddress(IntPtr ip) {
            return new IPAddress(ip.ToInt64());
        }
        private	IntPtr MakeRange(byte low, byte	high) {
            return (IntPtr)((int)((high	<< 8) +	low));
        }
        protected override void	WndProc(ref	Message	m) {
            if(m.Msg ==	(WM_REFLECT	+ WM_NOTIFY)) {
                NmIPAddress	ipInfo =  (NmIPAddress)Marshal.PtrToStructure(m.LParam,	typeof(NmIPAddress));
                if(ipInfo.Hdr.Code == -860)	{
                    if(values[ipInfo.Field]	!= ipInfo.Value) {
                        values[ipInfo.Field] = ipInfo.Value;
                        OnTextChanged(new TextChangedEventArgs(ipInfo.Field, ipInfo.Value));
                    }
                }
            }
            base.WndProc (ref m);
        }
    }
}





/bb|[^b]{2}/
GeneralRe: 2 comments: Pin
malharone8-Sep-05 15:23
malharone8-Sep-05 15:23 
GeneralRe: 2 comments: Pin
Almighty Bob8-Sep-05 17:25
Almighty Bob8-Sep-05 17:25 
GeneralRe: 2 comments: Pin
mawnkay9-Sep-05 4:41
mawnkay9-Sep-05 4:41 
GeneralRe: 2 comments: Pin
Almighty Bob9-Sep-05 7:33
Almighty Bob9-Sep-05 7:33 

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.