581 lines
18 KiB
C#
581 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Data;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AMT_SW_GUI
|
|
{
|
|
public delegate void OutOfRangeeventhandler();
|
|
public delegate void TextChangedEventHandler(object sender, EventArgs e);
|
|
|
|
public partial class AMT_IPAddress : UserControl
|
|
{
|
|
private Control activeControl;
|
|
|
|
public event OutOfRangeeventhandler OutOfRangeError;
|
|
public event TextChangedEventHandler TextChanged;
|
|
public AMT_IPAddress()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
public Control ActiveControl
|
|
{
|
|
get
|
|
{
|
|
return activeControl;
|
|
}
|
|
|
|
set
|
|
{
|
|
if (this.Controls.Contains(value))
|
|
{
|
|
activeControl = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool ActivateControl(Control active)
|
|
{
|
|
if (this.Controls.Contains(active))
|
|
{
|
|
active.Select();
|
|
this.activeControl = active;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
{
|
|
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(158, 191, 217), 1), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
|
|
}
|
|
|
|
public override string Text
|
|
{
|
|
get
|
|
{
|
|
return Box1.Text + "." + Box2.Text + "." + Box3.Text + "." + Box4.Text;
|
|
}
|
|
set
|
|
{
|
|
if (value != "" && value != null)
|
|
{
|
|
string[] pieces = new string[4];
|
|
pieces = value.ToString().Split(".".ToCharArray(), 4);
|
|
Box1.Text = pieces[0];
|
|
Box2.Text = pieces[1];
|
|
Box3.Text = pieces[2];
|
|
Box4.Text = pieces[3];
|
|
}
|
|
else
|
|
{
|
|
Box1.Text = "";
|
|
Box2.Text = "";
|
|
Box3.Text = "";
|
|
Box4.Text = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
public TextBox Octet1
|
|
{
|
|
get { return Box1; }
|
|
}
|
|
|
|
public TextBox Octet2
|
|
{
|
|
get { return Box2; }
|
|
}
|
|
|
|
public TextBox Octet3
|
|
{
|
|
get { return Box3; }
|
|
}
|
|
|
|
public TextBox Octet4
|
|
{
|
|
get { return Box4; }
|
|
}
|
|
|
|
public bool IsValid()
|
|
{
|
|
try
|
|
{
|
|
int checkval = int.Parse(Box1.Text);
|
|
if (checkval < 0 || checkval > 223)
|
|
return false;
|
|
checkval = int.Parse(Box2.Text);
|
|
if (checkval < 0 || checkval > 255)
|
|
return false;
|
|
checkval = int.Parse(Box3.Text);
|
|
if (checkval < 0 || checkval > 255)
|
|
return false;
|
|
checkval = int.Parse(Box4.Text);
|
|
if (checkval < 0 || checkval > 255)
|
|
return false;
|
|
else
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private bool IsValid(TextBox sender, string inString)
|
|
{
|
|
try
|
|
{
|
|
int theValue = int.Parse(inString);
|
|
if (sender == Box1)
|
|
{
|
|
if (theValue >= 1 && theValue <= 223)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
else if (theValue >= 0 && theValue <= 255)
|
|
return true;
|
|
else
|
|
{
|
|
if (OutOfRangeError != null)
|
|
{
|
|
OutOfRangeError();
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private bool DiscoverPrefixZero(TextBox sender, System.Windows.Forms.KeyPressEventArgs e)
|
|
{
|
|
|
|
if (((((TextBox)(sender)).Text == "0") && (((TextBox)(sender)).SelectionStart == 1) && (Char.IsDigit(e.KeyChar))) ||
|
|
((e.KeyChar.ToString() == "0") && (((TextBox)(sender)).SelectionStart == 0) && (((TextBox)(sender)).Text.Length > 0)))
|
|
{
|
|
if (((TextBox)(sender)).SelectedText.Length < ((TextBox)(sender)).Text.Length)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
private void Box1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
|
|
{
|
|
bool prefixZero = DiscoverPrefixZero((TextBox)sender, e);
|
|
if (prefixZero)
|
|
{
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
if (e.KeyChar.ToString() == "." || Char.IsDigit(e.KeyChar) || e.KeyChar == 8)
|
|
{
|
|
if (e.KeyChar.ToString() == ".")
|
|
{
|
|
if (Box1.Text != "" && Box1.Text.Length != Box1.SelectionLength)
|
|
{
|
|
if (IsValid(Box1, Box1.Text))
|
|
Box2.Focus();
|
|
else
|
|
Box1.SelectAll();
|
|
}
|
|
e.Handled = true;
|
|
}
|
|
|
|
else if (Box1.SelectionLength != Box1.Text.Length)
|
|
{
|
|
if (Box1.Text.Length == 2)
|
|
{
|
|
if (e.KeyChar == 8)
|
|
Box1.Text.Remove(Box1.Text.Length - 1, 1);
|
|
else if (!IsValid((TextBox)sender, InsertKeyChar((TextBox)sender, e)))
|
|
{
|
|
|
|
Box2.SelectAll();
|
|
e.Handled = true;
|
|
}
|
|
else
|
|
{
|
|
Box2.Focus();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
else
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void Box2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
|
|
{
|
|
bool prefixZero = DiscoverPrefixZero((TextBox)sender, e);
|
|
if (prefixZero)
|
|
{
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
if (e.KeyChar.ToString() == "." || Char.IsDigit(e.KeyChar) || e.KeyChar == 8)
|
|
{
|
|
if (e.KeyChar.ToString() == ".")
|
|
{
|
|
if (Box2.Text != "" && Box2.Text.Length != Box2.SelectionLength)
|
|
{
|
|
if (IsValid(Box2, Box2.Text))
|
|
Box3.Focus();
|
|
else
|
|
Box2.SelectAll();
|
|
}
|
|
e.Handled = true;
|
|
}
|
|
else if (e.KeyChar == 8 && Box2.SelectionStart == 0)
|
|
{
|
|
if(Box1.Text!="")
|
|
{
|
|
Box1.Text = Box1.Text.Remove(Box1.Text.Length - 1, 1);
|
|
}
|
|
Box1.Focus();
|
|
Box1.SelectionStart = Box1.Text.Length;
|
|
Box1.SelectionLength = 0;
|
|
return;
|
|
}
|
|
|
|
else if (Box2.SelectionLength != Box2.Text.Length)
|
|
{
|
|
if (Box2.Text.Length == 2)
|
|
{
|
|
if (e.KeyChar == 8)
|
|
{
|
|
Box2.Text.Remove(Box2.Text.Length - 1, 1);
|
|
}
|
|
else if (!IsValid((TextBox)sender, InsertKeyChar((TextBox)sender, e)))
|
|
{
|
|
|
|
Box2.SelectAll();
|
|
e.Handled = true;
|
|
}
|
|
else
|
|
{
|
|
Box3.Focus();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
else if (Box2.Text.Length == 0 && e.KeyChar == 8)
|
|
{
|
|
Box1.Focus();
|
|
Box1.SelectionStart = Box1.Text.Length;
|
|
}
|
|
|
|
else
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
private void Box3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
|
|
{
|
|
bool prefixZero = DiscoverPrefixZero((TextBox)sender, e);
|
|
if (prefixZero)
|
|
{
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
if (e.KeyChar.ToString() == "." || Char.IsDigit(e.KeyChar) || e.KeyChar == 8)
|
|
{
|
|
if (e.KeyChar.ToString() == ".")
|
|
{
|
|
if (Box3.Text != "" && Box3.SelectionLength != Box3.Text.Length)
|
|
{
|
|
if (IsValid(Box3, Box3.Text))
|
|
Box4.Focus();
|
|
else
|
|
Box3.SelectAll();
|
|
}
|
|
e.Handled = true;
|
|
}
|
|
else if (e.KeyChar == 8 && Box3.SelectionStart == 0)
|
|
{
|
|
if (Box2.Text != "")
|
|
{
|
|
Box2.Text = Box2.Text.Remove(Box2.Text.Length - 1, 1);
|
|
}
|
|
Box2.Focus();
|
|
Box2.SelectionStart = Box1.Text.Length;
|
|
Box2.SelectionLength = 0;
|
|
return;
|
|
}
|
|
else if (Box3.SelectionLength != Box3.Text.Length)
|
|
{
|
|
if (Box3.Text.Length == 2)
|
|
{
|
|
if (e.KeyChar == 8)
|
|
{
|
|
Box3.Text.Remove(Box3.Text.Length - 1, 1);
|
|
}
|
|
else if (!IsValid((TextBox)sender, InsertKeyChar((TextBox)sender, e)))
|
|
{
|
|
Box3.SelectAll();
|
|
e.Handled = true;
|
|
}
|
|
else
|
|
{
|
|
Box4.Focus();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
else if (Box3.Text.Length == 0 && e.KeyChar == 8)
|
|
{
|
|
Box2.Focus();
|
|
Box2.SelectionStart = Box2.Text.Length;
|
|
}
|
|
|
|
else
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void Box4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
|
|
{
|
|
bool prefixZero = DiscoverPrefixZero((TextBox)sender, e);
|
|
if (prefixZero)
|
|
{
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
if (Char.IsDigit(e.KeyChar) || e.KeyChar == 8)
|
|
{
|
|
if (e.KeyChar == 8 && Box4.SelectionStart == 0)
|
|
{
|
|
if(Box3.Text!="")
|
|
{
|
|
Box3.Text = Box3.Text.Remove(Box3.Text.Length - 1, 1);
|
|
}
|
|
Box3.Focus();
|
|
Box3.SelectionStart = Box1.Text.Length;
|
|
Box3.SelectionLength = 0;
|
|
return;
|
|
}
|
|
if (Box4.SelectionLength != Box4.Text.Length)
|
|
{
|
|
if (Box4.Text.Length == 2)
|
|
{
|
|
if (e.KeyChar == 8)
|
|
{
|
|
Box4.Text.Remove(Box4.Text.Length - 1, 1);
|
|
}
|
|
else if (!IsValid((TextBox)sender, InsertKeyChar((TextBox)sender, e)))
|
|
{
|
|
Box4.SelectAll();
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
}
|
|
else if (Box4.Text.Length == 0 && e.KeyChar == 8)
|
|
{
|
|
Box3.Focus();
|
|
Box3.SelectionStart = Box3.Text.Length;
|
|
}
|
|
}
|
|
else
|
|
e.Handled = true;
|
|
}
|
|
|
|
void Box_TextChanged(object sender, System.EventArgs e)
|
|
{
|
|
if (TextChanged != null)
|
|
TextChanged(this, new EventArgs());
|
|
}
|
|
|
|
private string InsertKeyChar(TextBox sender, System.Windows.Forms.KeyPressEventArgs e)
|
|
{
|
|
string prefix = "";
|
|
string postfix = "";
|
|
|
|
try
|
|
{
|
|
if (((TextBox)(sender)).SelectionStart != 0)
|
|
{
|
|
prefix = ((TextBox)(sender)).Text.Substring(0, ((TextBox)(sender)).SelectionStart);
|
|
}
|
|
|
|
if (((TextBox)(sender)).SelectionStart != Box4.Text.Length)
|
|
{
|
|
postfix = ((TextBox)(sender)).Text.Substring(((TextBox)(sender)).SelectionStart, ((TextBox)(sender)).Text.Length - Box4.SelectionStart);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
|
|
string str = prefix + e.KeyChar.ToString() + postfix;
|
|
|
|
return str;
|
|
}
|
|
|
|
private void Box_Enter(object sender, System.EventArgs e)
|
|
{
|
|
TextBox tb = (TextBox)sender;
|
|
tb.SelectAll();
|
|
}
|
|
|
|
private void label_EnabledChanged(object sender, System.EventArgs e)
|
|
{
|
|
Label lbl = (Label)sender;
|
|
if (lbl.Enabled)
|
|
lbl.BackColor = SystemColors.Window;
|
|
else
|
|
lbl.BackColor = SystemColors.Control;
|
|
Invalidate();
|
|
}
|
|
|
|
private void AMT_IPAddress_EnabledChanged(object sender, System.EventArgs e)
|
|
{
|
|
if (sender is AMT_IPAddress)
|
|
{
|
|
AMT_IPAddress pan = (AMT_IPAddress)sender;
|
|
if (pan.Enabled)
|
|
pan.BackColor = SystemColors.Window;
|
|
else
|
|
pan.BackColor = SystemColors.Control;
|
|
}
|
|
}
|
|
|
|
protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m)
|
|
{
|
|
switch (m.WParam.ToInt32())
|
|
{
|
|
case 37: // <--- left arrow.
|
|
SetFocus(EARROWS.LEFT);
|
|
break;
|
|
case 38: // <--- up arrow.
|
|
SetFocus(EARROWS.LEFT);
|
|
break;
|
|
case 39: // <--- right arrow.
|
|
SetFocus(EARROWS.RIGHT);
|
|
break;
|
|
case 40: // <--- down arrow.
|
|
SetFocus(EARROWS.RIGHT);
|
|
break;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
{
|
|
if ((keyData == Keys.Up || keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.Down))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return base.ProcessCmdKey(ref msg, keyData);
|
|
}
|
|
}
|
|
|
|
private void SetFocus(EARROWS direction)
|
|
{
|
|
TextBox box;
|
|
box = FocusedBox();
|
|
if (box == null)
|
|
return;
|
|
|
|
switch (direction)
|
|
{
|
|
case EARROWS.RIGHT:
|
|
{
|
|
if (box.Text.Length > box.SelectionStart)
|
|
{
|
|
box.SelectionStart += 1;
|
|
}
|
|
else
|
|
{
|
|
if (box == Box1)
|
|
{
|
|
Box2.Focus();
|
|
Box2.SelectionLength = 0;
|
|
}
|
|
else if (box == Box2)
|
|
{
|
|
Box3.Focus();
|
|
Box3.SelectionLength = 0;
|
|
}
|
|
else if (box == Box3)
|
|
{
|
|
Box4.Focus();
|
|
Box4.SelectionLength = 0;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
|
|
case EARROWS.LEFT:
|
|
{
|
|
if (box.SelectionStart > 0)
|
|
{
|
|
box.SelectionStart -= 1;
|
|
}
|
|
else
|
|
{
|
|
if (box == Box2)
|
|
{
|
|
Box1.Focus();
|
|
Box1.SelectionStart = Box1.Text.Length;
|
|
}
|
|
else if (box == Box3)
|
|
{
|
|
Box2.Focus();
|
|
Box2.SelectionStart = Box1.Text.Length;
|
|
}
|
|
else if (box == Box4)
|
|
{
|
|
Box3.Focus();
|
|
Box3.SelectionStart = Box1.Text.Length;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private TextBox FocusedBox()
|
|
{
|
|
if (this.Box1.Focused)
|
|
return (Box1);
|
|
if (this.Box2.Focused)
|
|
return (Box2);
|
|
if (this.Box3.Focused)
|
|
return (Box3);
|
|
if (this.Box4.Focused)
|
|
return (Box4);
|
|
return null;
|
|
}
|
|
|
|
public enum EARROWS
|
|
{
|
|
LEFT = 0,
|
|
RIGHT = 1
|
|
}
|
|
|
|
}
|
|
}
|