본문 바로가기

C Sharp

C#, TEXTBOX 숫자만 입력받기

반응형

C#, TEXTBOX 숫자만 입력받기


private void textBox1_TextChanged(object sender, EventArgs e)

{

     TextBox textBox = sender as TextBox;

     Int32 selectionStart = textBox.SelectionStart;

     Int32 selectionLength = textBox.SelectionLength;

     String newText = String.Empty;

     foreach (Char c in textBox.Text.ToCharArray())

     {

          if (Char.IsDigit(c) || Char.IsControl(c)) newText += c;

     }

     textBox.Text = newText;

     textBox.SelectionStart = selectionStart <=

     textBox.Text.Length ? selectionStart : textBox.Text.Length;

}

반응형