using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace ControlLib { public class IpNumericDigit : TextBox { public delegate void SelectNextDelegate(); public event SelectNextDelegate SelectNext; public delegate void SelectPreviousDelegate(); public event SelectPreviousDelegate SelectPrevious; public IpNumericDigit() { this.Width = 30; this.MaxLength = 3; this.TextAlign = HorizontalAlignment.Center; } private bool _Use223AsMax = false; public bool Use223AsMax { get { return _Use223AsMax; } set { _Use223AsMax = value; } } protected override void OnKeyPress(KeyPressEventArgs e) { if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57) { e.Handled = false; } if ((int)e.KeyChar == 32) { if (SelectNext != null) { SelectNext(); } e.Handled = false; } if ((int)e.KeyChar == 8) { if (this.Text.Length == 0) { if (SelectPrevious != null) { SelectPrevious(); } } e.Handled = false; } base.OnKeyPress(e); } protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyValue == 39) { if (this.SelectionStart == this.Text.Length && Validate()) { if (SelectNext != null) { SelectNext(); } } } else if (e.KeyValue == 37) { if (this.SelectionStart == 0) { if (SelectPrevious != null) { SelectPrevious(); } } } base.OnKeyDown(e); } protected override void OnTextChanged(EventArgs e) { if (this.Text.Trim().Length == 3) { if (Validate()) { if (SelectNext != null) { SelectNext(); } } } base.OnTextChanged(e); } private bool Validate() { int minValue = 0; int maxValue = 255; if (Use223AsMax) { minValue = 1; maxValue = 223; } if (this.Text.Trim().Length > 0) { if (!(Convert.ToInt32(this.Text) >= minValue && Convert.ToInt32(this.Text) <= maxValue)) { MessageBox.Show(minValue.ToString() + " ile " + maxValue.ToString() + " aras� bir say� giriniz"); this.Text = maxValue.ToString(); this.Select(0, 0); return false; } else { return true; } } else { return true; } } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)