IPBoxControl 开发手记(二)
如果哪位发现什么问题和bug,请多多指出,我会及时修改。谢谢!
/*
* 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
/// <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);
}
}
}
* 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
/// <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);
}
}
}