C# textbox’a sadece nümerik veri girmek

Merhabalar,

Bu günkü yazımda textbox’a nümerik veri girmenin yöntemini sizlere anlatacağım. Textbox’ın WF için KeyPress Event’ine WPF için ise PreviewTextInput’una aşağıdaki gibi bir kod yazmanız işi çözecektir.

– Uygulamanın C# kodları…

WF için;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 {
 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
 (e.KeyChar != '.'))
 {
 e.Handled = true;
 }

 // only allow one decimal point
 if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
 {
 e.Handled = true;
 }
 }

WPF için;

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
 {
 if (!char.IsDigit(e.Text, e.Text.Length - 1))
 {
 e.Handled = true;
 }
 }

Kolay gele.

Paylaşmayı unutmayın!
0 0 votes
Article Rating
Subscribe
Bildir
guest
0 Yorum
Inline Feedbacks
View all comments