|
|
IPBoxControl |
|
|
/**//* * Purpose: IPAddress TextBox Control. * Author: Vincent * Date: 2004-10-29 * */
using System; using System.Windows.Forms;
namespace Sci.Vincent.Controls { /**//// <summary> /// IP Address Box Control. /// </summary> public class IPBox : System.Windows.Forms.UserControl { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox4;
/**//// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null;
private System.Windows.Forms.Panel panel1;
private const int DEFAULT_WIDTH_SIZE = 136; private const int DEFAULT_HEIGHT_SIZE = 20; private string _text = ""; private bool _isAllowWarn = true; private Languages _language = Languages.English;
/**//// <summary> /// Usable languages enumeration. /// </summary> public enum Languages { /**//// <summary> /// English language /// </summary> English, /**//// <summary> /// Chinese language /// </summary> Chinese }
/**//// <summary> /// Creates an instance of the IPBox class. /// </summary> public IPBox() { // This call is required by the Windows.Forms Form Designer. InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
/**//// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); }
Component Designer generated code#region Component Designer generated code
/**//// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.panel1 = new System.Windows.Forms.Panel(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // textBox1 // this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox1.Location = new System.Drawing.Point(8, 0); this.textBox1.MaxLength = 3; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(20, 13); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); // // label1 // this.label1.BackColor = System.Drawing.SystemColors.Window; this.label1.Location = new System.Drawing.Point(32, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(8, 14); this.label1.TabIndex = 1; this.label1.Text = "."; this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter; // // label2 // this.label2.BackColor = System.Drawing.SystemColors.Window; this.label2.Location = new System.Drawing.Point(64, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(8, 14); this.label2.TabIndex = 3; this.label2.Text = "."; this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter; // // textBox2 // this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox2.Location = new System.Drawing.Point(40, 0); this.textBox2.MaxLength = 3; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(20, 13); this.textBox2.TabIndex = 2; this.textBox2.Text = ""; this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress); // // label3 // this.label3.BackColor = System.Drawing.SystemColors.Window; this.label3.Location = new System.Drawing.Point(96, 0); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(8, 14); this.label3.TabIndex = 5; this.label3.Text = "."; this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter; // // textBox3 // this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox3.Location = new System.Drawing.Point(72, 0); this.textBox3.MaxLength = 3; this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(20, 13); this.textBox3.TabIndex = 4; this.textBox3.Text = ""; this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox3_KeyPress); // // textBox4 // this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox4.Location = new System.Drawing.Point(104, 0); this.textBox4.MaxLength = 3; this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(20, 13); this.textBox4.TabIndex = 6; this.textBox4.Text = ""; this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox4_KeyPress); // // panel1 // this.panel1.BackColor = System.Drawing.SystemColors.Window; this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.panel1.Controls.Add(this.textBox1); this.panel1.Controls.Add(this.textBox4); this.panel1.Controls.Add(this.label1); this.panel1.Controls.Add(this.label3); this.panel1.Controls.Add(this.textBox3); this.panel1.Controls.Add(this.textBox2); this.panel1.Controls.Add(this.label2); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(136, 20); this.panel1.TabIndex = 8; // // IPBox // this.Controls.Add(this.panel1); this.Name = "IPBox"; this.Size = new System.Drawing.Size(136, 20); this.SizeChanged += new System.EventHandler(this.IPBox_SizeChanged); this.panel1.ResumeLayout(false); this.ResumeLayout(false);
}
#endregion
/**//// <summary> /// Masks IP Address. /// </summary> /// <param name="textBox"></param> /// <param name="e"></param> private void MaskIPAddress(TextBox textBox, KeyPressEventArgs e) { // Only allow digit, '.' and "BackSpace" key. if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8) { if (e.KeyChar != 8) { if (textBox.Text.Length == 2 && e.KeyChar != '.') { Validate(textBox, textBox.Text + e.KeyChar); e.Handled = true; } else if (e.KeyChar == '.') { if (textBox.Text.Length == 0) { textBox.Text = ""; } else { FocusNext(textBox); } e.Handled = true; } } else if (textBox.Text.Length == 0) { FocusPreviouse(textBox); e.Handled = false; } } else { e.Handled = true; // Ignore other keys } }
/**//// <summary> /// Focuses the previous textbox. /// </summary> /// <param name="textBox"></param> private void FocusPreviouse(TextBox textBox) { switch (textBox.Name) { case "textBox2": textBox1.Focus(); textBox1.SelectionStart = textBox1.Text.Length; break; case "textBox3": textBox2.Focus(); textBox2.SelectionStart = textBox2.Text.Length; break; case "textBox4": textBox3.Focus(); textBox3.SelectionStart = textBox3.Text.Length; break; default: break; } }
/**//// <summary> /// Focuses the next textbox. /// </summary> /// <param name="textBox"></param> private void FocusNext(TextBox textBox) { switch (textBox.Name) { case "textBox1": textBox2.Focus(); textBox2.SelectAll(); break; case "textBox2": textBox3.Focus(); textBox3.SelectAll(); break; case "textBox3": textBox4.Focus(); textBox4.SelectAll(); break; default: break; } }
/**//// <summary> /// Validates whether the string is correct. /// </summary> /// <param name="currentTextBox">Current textbox</param> /// <param name="str">input string</param> private void Validate(TextBox currentTextBox, string str) { int maxvalue = currentTextBox.Name.Equals("textBox1") ? 233 : 255;
TextBox nextTextBox = currentTextBox; switch (currentTextBox.Name) { case "textBox1": nextTextBox = textBox2; break; case "textBox2": nextTextBox = textBox3; break; case "textBox3": nextTextBox = textBox4; break; default: break; }
if (Int32.Parse(str) > maxvalue) { currentTextBox.Text = maxvalue.ToString(); currentTextBox.Focus(); if (AllowWarn) { switch (Language) { case Languages.English: MessageBox.Show(str + " is not a valid entry. Please specify a value between 1 and " + maxvalue, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; case Languages.Chinese: MessageBox.Show(str + " 不是一个有效值。请指定一个介于 1 和 " + maxvalue + " 之间的数值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; } } } else { currentTextBox.Text = str; if (!nextTextBox.Equals(currentTextBox)) { nextTextBox.Focus(); nextTextBox.SelectAll(); } else { currentTextBox.SelectionStart = currentTextBox.Text.Length; } } }
/**//// <summary> /// Overrided. Returns IP Address. /// </summary> public override string Text { get { if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0 || textBox4.Text.Length == 0) { _text = ""; } else { _text = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + "." + textBox4.Text; } return _text; } }
/**//// <summary> /// Returns whether allow warining informaiton when user input incorrect format. /// </summary> public bool AllowWarn { get { return _isAllowWarn; } set { _isAllowWarn = value; } }
/**//// <summary> /// Specifies language of warning information. /// </summary> public Languages Language { get { return _language; } set { _language = value; } }
/**//// <summary> /// Overrided. Returns whether the IPBox control is focused. /// </summary> public override bool Focused { get { if (textBox1.Focused || textBox2.Focused || textBox3.Focused || textBox4.Focused) { return true; } else { return false; } } }
/**//// <summary> /// Focus the IPBox. /// </summary> /// <returns></returns> public new bool Focus() { return textBox1.Focus(); }
/**//// <summary> /// Clears the IPBox's Text. /// </summary> public void Clear() { textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox1.Focus(); }
/**//// <summary> /// Forbids to change the size of IPBox Control. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void IPBox_SizeChanged(object sender, System.EventArgs e) { this.Width = DEFAULT_WIDTH_SIZE; this.Height = DEFAULT_HEIGHT_SIZE; }
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { MaskIPAddress(textBox1, e); }
private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { MaskIPAddress(textBox2, e); }
private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { MaskIPAddress(textBox3, e); }
private void textBox4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { MaskIPAddress(textBox4, e); } } }
缺缺 发表于 2004-11-4 [所属栏目: .Net | 返回首页]
|
|
|