PDA

View Full Version : آموزش: ایجاد ماسک برای textBox



آرادخان
یک شنبه 24 فروردین 1399, 22:04 عصر
کافیه اینو تو کد رو تو KeyDown بنویسید.

حالا همین رو میتونید برا شماره ملی - شماره کارت بانکی و.... بنویسید

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{


bool select;


if (textBox1.Text.Length == 8)
{
textBox1.ReadOnly = true;


}
if (e.KeyCode == Keys.Back)
{
textBox1.ReadOnly = false;
select = true;
}


else
{
select = false;
}


if (select == false)
{
if (textBox1.Text.Length == 2)
{
textBox1.Text = textBox1.Text + ":";
textBox1.SelectionStart = textBox1.Text.Length ;
}
if (textBox1.Text.Length == 5)
{
textBox1.Text = textBox1.Text + ":";
textBox1.SelectionStart = textBox1.Text.Length;


}
else
{

textBox1.SelectionStart = textBox1.Text.Length;


}
}
}