C# TextBox 限制輸入長度+限制只能輸入16進制字串
C# TextBox 限制輸入長度+限制只能輸入16進制字串
private void W1_txt_UUID_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 8)//刪除鍵要直接允許
{
e.Handled = false;
}
else
{
if (W1_txt_UUID.Text.Length < 8)//長度限制在8
{
if ((e.KeyChar >= ‘a’ && e.KeyChar <= ‘f’) || (e.KeyChar >= ‘A’ && e.KeyChar <= ‘F’) || (e.KeyChar >= ‘0’ && e.KeyChar <= ‘9’))//限制0~9和A~F
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
}
private void W1_txt_InterfereCode_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 8)//刪除鍵要直接允許
{
e.Handled = false;
}
else
{
if (W1_txt_InterfereCode.Text.Length < 4)//長度限制在4
{
if ((e.KeyChar >= ‘a’ && e.KeyChar <= ‘f’) || (e.KeyChar >= ‘A’ && e.KeyChar <= ‘F’) || (e.KeyChar >= ‘0’ && e.KeyChar <= ‘9’))//限制0~9和A~F
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
}