, mahasen, . , . , Toolbox, Form
.
using System;
using System.Linq;
using System.Windows.Forms;
namespace MyApp.GUI
{
public class FilteredTextBox : TextBox
{
private char[] m_validCharacters;
private string m_filter;
private event EventHandler m_maxLength;
public string Filter
{
get
{
return m_filter;
}
set
{
m_filter = value;
m_validCharacters = value.ToCharArray();
}
}
public FilteredTextBox()
{
m_filter = "";
this.KeyPress += Validate_Char_OnKeyPress;
this.TextChanged += Check_Text_Length_OnTextChanged;
}
public event EventHandler TextBoxFull
{
add { m_maxLength += value; }
remove { m_maxLength -= value; }
}
void Validate_Char_OnKeyPress(object sender, KeyPressEventArgs e)
{
if (m_validCharacters.Contains(e.KeyChar) || e.KeyChar == '\b')
e.Handled = false;
else
e.Handled = true;
}
void Check_Text_Length_OnTextChanged(object sender, EventArgs e)
{
if (this.TextLength == this.MaxLength)
{
var Handle = m_maxLength;
if (Handle != null)
Handle(this, EventArgs.Empty);
}
}
}
}
, , , , 3 Form
TextBoxFull . 4 IP-. ...
private bool ValidateAddressChunk(string p_text)
{
byte AddressChunk = new byte();
return byte.TryParse(p_text, out AddressChunk);
}
private void filteredTextBox1_TextBoxFull(object sender, EventArgs e)
{
var Filtered_Text_Box = (FilteredTextBox)sender;
if (!ValidateAddressChunk(Filtered_Text_Box.Text))
filteredTextBox1.Text = "255";
else
filteredTextBox2.Focus();
}
private void filteredTextBox2_TextBoxFull(object sender, EventArgs e)
{
var Filtered_Text_Box = (FilteredTextBox)sender;
if (!ValidateAddressChunk(Filtered_Text_Box.Text))
filteredTextBox2.Text = "255";
}