Reputation: 1464
I need some help with my code.
I need the following format,
12345-1234567-1
Basically I want to type just digits and when text length reaches 5, it should append '-' and again on reaching to the length of 13, again it should append '-'.
My code is doing this fine. But when I use backspace/delete, it always append '-' to the 6th and 14th location.
Here is my code,
private void nicNo_txt_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() != "Back" || e.KeyCode.ToString() != "Space" || e.KeyCode.ToString() != "Delete")
{
if (nicNo_txt.TextLength == 5 || nicNo_txt.TextLength == 13)
nicNo_txt.AppendText("-");
}
}
Regards
Upvotes: 0
Views: 645
Reputation: 4779
One below will do
For formating after changes - replace format method with anything you like:
void oTextBoxAmount_TextChanged(object sender, EventArgs e)
{
//throw new NotImplementedException();
if (sender is TextBox)
{
TextBox tb = sender as TextBox;
tb.Text = FormatAmount(tb.Text);
tb.SelectionStart = tb.Text.Length;
}
}
For filtering keys (example below filters digits but you can change conditions):
void oTextBoxAmount_KeyPress(object sender, KeyPressEventArgs e)
{
int val = (int)e.KeyChar;
if (val >= 0x30 && val <= 0x39)
{
//Digits are ok
}
else if (val == 0x08)
{
//Backspace is ok
}
else
{
//Other are disallowed
e.Handled = true;
}
}
Upvotes: 1
Reputation: 3720
You can use AJAX Control Toolkit's Masked Edit. It does exactly what you want.
Ajax Control Toolkit - Masked Edit
Upvotes: 0
Reputation: 1380
Have you tried MaskedTextBox, in it you can specify a mask in whatever format you need
Upvotes: 4