Hi Ranjani,
As far as I know, MaskTextBox does not support such a case. The most difficult part is to check ‘AM/PM� I have another solution, we can create our own TextBox and override the OnKeyPress method to check and avoid the illegal typed character. You can also handle the KeyPress event of a TextBox to do the same thing. This is the code snippet:
public class TimeTextBox : TextBox
{
//Check typed character and avoid illegal character.
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
//Save the old states.
string txt = this.Text;
int selectStart = this.SelectionStart;
int selectLen = this.SelectionLength;
if (Char.IsControl(e.KeyChar) == false)
{
//Cope with the common char, such as A,B,C,!,1,2
txt = txt.Remove(selectStart, selectLen);
txt = txt.Insert(selectStart, e.KeyChar.ToString());
selectStart += 1;
}
else
{
//Cope with control key.
Keys cmdKey = (Keys)e.KeyChar;
if (cmdKey == Keys.Delete)
{
if (selectLen == 0 && selectStart < txt.Length)
txt = txt.Remove(selectStart + 1);
else
txt = txt.Remove(selectStart, selectLen);
}
else if (cmdKey == Keys.Back)
{
if (selectLen == 0 && selectStart > 0)
{
txt = txt.Remove(selectStart - 1);
selectStart -= 1;
}
else
txt = txt.Remove(selectStart, selectLen);
}
}
if (ValidText(txt))
{
//Reset the text and states.
this.Text = txt.ToUpper();
this.SelectionStart = selectStart;
}
e.Handled = true;
}
//Check if the typed character would generate a legal text
private bool ValidText(string txt)
{
bool isLegal = true;
for (int i = 0; i < txt.Length; i++)
{
char c = txt[i];
bool isCharLegal = false;
switch (i)
{
case 0:
{
//The first character must be 0 or 1
if (c == '0' || c == '1')
isCharLegal = true;
break;
}
case 1:
{
//The second character depends on the first character
//If the first is 0, this character can range from 1 to 9,
//else it must be 1 or 2
if((txt[0] == '0' && (c >= '1' && c <= '9'))
|| ((txt[0] == '1') && (c == '1' || c == '2')))
isCharLegal = true;
break;
}
case 2:
{
//The third character must be ':'
if (c == ':')
isCharLegal = true;
break;
}
case 3:
{
//The fourth character must range from 0 to 5,
//because minites must range from 0 to 59
if (c >= '0' && c <= '5')
isCharLegal = true;
break;
}
case 4:
{
//The fifth character can be any digit.
if (Char.IsDigit(c))
isCharLegal = true;
break;
}
case 5:
{
//The sixth character must be a blank to split the time and AM/PM
if (Char.IsWhiteSpace(c))
isCharLegal = true;
break;
}
case 6:
{
//The seventh character can be A or P, a lower character is also legal.
if (c == 'A' || c == 'P'
|| c == 'a' || c == 'p')
isCharLegal = true;
break;
}
case 7:
{
//The eighth character can be M, a lower character is also legal.
if (c == 'M' || c == 'm')
isCharLegal = true;
break;
}
default:
break;
}
if (isCharLegal == false)
{
//If one character is illegal, the string is illegal.
isLegal = false;
break;
}
}
return isLegal;
}
}
Let me know if this helps.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.