Sylloge
A C# helper library
code/Controls/IPv4.cs
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00018 using System;
00019 using System.Collections.Generic;
00020 using System.ComponentModel;
00021 using System.Drawing;
00022 using System.Data;
00023 using System.Text;
00024 using System.Windows.Forms;
00025 
00026 namespace Sylloge.Controls
00027 {
00028     public partial class IPv4 : UserControl
00029     {
00030         #region Events
00031 
00032         public delegate void DelegateOctetChanged(int octet, string value);
00036         public event DelegateOctetChanged OctetChanged;
00037         public delegate void DelegateIpAddressChanged(string value);
00041         public event DelegateIpAddressChanged IpAddressChanged;
00042 
00043         private void OnOctetChange(int octet, string value)
00044         {
00045             if (this.OctetChanged != null) { this.OctetChanged(octet, value); }
00046             this.OnIpChange();
00047         }
00048 
00049         private void OnIpChange()
00050         {
00051             if (this.IpAddressChanged != null) { this.IpAddressChanged(this.IpAddress); }
00052         }
00053 
00054         #endregion
00055 
00056         #region Local Attributes
00057 
00058         private System.Windows.Forms.TextBox _CurrentTextBox;
00059         private bool _IsSettingIp;
00060 
00061         #endregion
00062 
00063         #region Class Methods and Properties
00064 
00065         public IPv4()
00066         {
00067             this.InitializeComponent();
00068             this.c_TextBoxOctet1.Tag = 1;
00069             this.c_TextBoxOctet1.Tag = 2;
00070             this.c_TextBoxOctet1.Tag = 3;
00071             this.c_TextBoxOctet1.Tag = 4;
00072         }
00073 
00074         ~IPv4()
00075         {
00076         }
00077 
00081         public string IpAddress
00082         {
00083             get
00084             {
00085                 string Ip = this.c_TextBoxOctet1.Text + "." +
00086                             this.c_TextBoxOctet2.Text + "." +
00087                             this.c_TextBoxOctet3.Text + "." +
00088                             this.c_TextBoxOctet4.Text;
00089                 return Ip;
00090             }
00091             set
00092             {
00093                 try {
00094                     this._IsSettingIp = true;
00095                     if (!Sylloge.Net.IPv4.IsValidIp(value)) {
00096                         this.c_TextBoxOctet1.Text = "0";
00097                         this.c_TextBoxOctet2.Text = "0";
00098                         this.c_TextBoxOctet3.Text = "0";
00099                         this.c_TextBoxOctet4.Text = "0";
00100                     } else {
00101                         string[] Ip = value.Split('.');
00102                         this.c_TextBoxOctet1.Text = Ip[0];
00103                         this.c_TextBoxOctet2.Text = Ip[1];
00104                         this.c_TextBoxOctet3.Text = Ip[2];
00105                         this.c_TextBoxOctet4.Text = Ip[3];
00106                     }
00107                 } catch (Exception ex) {
00108                     // This should only happen if there was a programmer error
00109                     // Don't alert anyone, just print it out.
00110                     //MyMessageBox.Show("Error setting IP address:" + Environment.NewLine + ex.ToString());
00111                     Console.WriteLine("IPv4 Error:" + ex.Message);
00112                     this.c_TextBoxOctet1.Text = "0";
00113                     this.c_TextBoxOctet2.Text = "0";
00114                     this.c_TextBoxOctet3.Text = "0";
00115                     this.c_TextBoxOctet4.Text = "0";
00116                 }
00117                 this._IsSettingIp = false;
00118             }
00119         }
00120 
00125         public override string ToString()
00126         {
00127             return this.IpAddress;
00128         }
00129 
00130         #endregion
00131 
00132         #region Control Event Handlers
00133 
00134         private void c_TextBoxOctet_Enter(object sender, System.EventArgs e)
00135         {
00136             System.Windows.Forms.TextBox box = (System.Windows.Forms.TextBox)sender;
00137             this._CurrentTextBox = box;
00138             box.BackColor = Color.LightBlue;
00139             box.SelectAll();
00140         }
00141 
00142         private void c_TextBoxOctet_Leave(object sender, System.EventArgs e)
00143         {
00144             ((System.Windows.Forms.TextBox)sender).BackColor = Color.White;
00145         }
00146 
00147         private void c_TextBoxOctet_TextChanged(object sender, System.EventArgs e)
00148         {
00149             System.Windows.Forms.TextBox box = (System.Windows.Forms.TextBox)sender;
00150             this._CurrentTextBox = box;
00151             if (box.Text == string.Empty || box.Text == null) { box.Text = "0"; }
00152             if (Convert.ToInt32(box.Text) < 0) { box.Text = "0"; }
00153             if (Convert.ToInt32(box.Text) > 255) { box.Text = "255"; }
00154             if (box.Text.Length == 3 && !this._IsSettingIp) { this.SelectNextControl(box, true, true, true, false); }
00155             this.OnOctetChange((int)box.Tag, box.Text);
00156         }
00157 
00158         private void c_TextBoxOctet_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
00159         {
00160             System.Windows.Forms.TextBox box = (System.Windows.Forms.TextBox)sender;
00161             this._CurrentTextBox = box;
00162             if (e.KeyChar == '.') { this.SelectNextControl(box, true, true, true, false); }
00163             e.Handled = !Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar);
00164         }
00165 
00166         #endregion
00167 
00168     }
00169 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events