2013-07-06 18 views
6

Chcę utworzyć pole tekstowe w WPF, które akceptuje tylko numery ... Mam już reaserched i ludzie mówią, aby użyć zdarzenia keypress lub pole tekstowe maskowane, ale są one w formularzach windows ...Numeric TextBox w C# - WPF

Odpowiedz

32

dla WPF:

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

Dla Windows Forms:

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

być ostrożnym z kopiowaniem wklejanie do swojej skrzynki, nie będą kontrolowane przez takiego kodu – javirs

+0

Łatwe rozwiązania Maciek i J3soon docenione –