2017-03-09 68 views
0

我阻止了除鍵盤1-9之外的所有鍵盤鍵,並且我有一個問題如何啓用逗號?在KeyDown C中只允許逗號#

我的代碼:

private void textbox_KeyDown(object sender, KeyRoutedEventArgs e) 
    if (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9 || e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9 || e.Key == Windows.System.VirtualKey.Decimal) 
    { 
    e.handled = false; 
    } 
    else 
    { 
    e.handled = true; 
    } 

回答

0

此代碼將只允許數字和逗號

if (!char.IsDigit(e.KeyChar) && e.KeyChar != ',') 
{ 
    e.Handled = true; 
} 

替代

if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != ',') 
{ 
    e.Handled = true; 
} 
+1

這不回答這個問題。提問者使用KeyRoutedEventArgs而不是KeyPressEventArgs。 –

+0

希望這個[鏈接](http://stackoverflow.com/questions/24612653/windows-phone-8-1-textbox-character-virtualkey-validation)將有助於@OwenPauling –

+1

@ArunD這個答案與無關問題,並且比有幫助更容易混淆。沒有提及改變'事件'類型,因此會引起許多來自無經驗用戶的問題。 –

0

試試這個...

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if((e.KeyChar >= 48 && e.KeyChar <= 57) || (e.KeyChar >= 97 && e.KeyChar <= 105)) 
      { 
       e.Handled = true; 
      } 
      else 
      { 
       e.Handled = false; 
      } 
     } 
+1

沒有使用編譯器我看到的錯誤:'不能隱式轉換「KeyPressEventArgs」到「KeyRoutedEventArgs」' –

0

根據Virtual Key Codes的文檔,您需要OemComma,它是0xBC或(VirtualKey)188。

+0

我只能使用VirualKey.A或Del或Decimal,我不能寫VirualKey.188 – user7683925

0

你可以試試這個:

所有的
 string keyInput = e.KeyChar.ToString(); 

     if (Char.IsDigit(e.KeyChar)) 
     { 
      // Digits are OK 
     } 
     else if (e.KeyChar == '\b') 
     { 
      // Backspace key is OK 
     } 
     else if (e.KeyChar == ',') 
     { 
      // Comma key is OK 
     } 


     else 
     { 
      // Swallow this invalid key and beep 
      e.Handled = true; 
      // MessageBeep(); 
     } 
0

首先,你必須記住小數點(無論是.,)已在CultureInfo來進行設置。如果您打算將應用程序發佈給更多用戶,我會建議記住這一點。

另一件事是,你的情況是沒有意義的:立足CultureInfo

// check for the keys 
if(
    (// if numeric between 0 and 9 
     e.Key >= Windows.System.VirtualKey.Number0 
     && 
     e.Key <= Windows.System.VirtualKey.Number9 
    ) 
    || // or 
    (// numeric from numpad between 0 and 9 
     e.Key >= Windows.System.VirtualKey.NumberPad0 
     && 
     e.Key <= Windows.System.VirtualKey.NumberPad9 
    ) 
    || // or decimal mark 
    e.Key == Windows.System.VirtualKey.Decimal 
) 
{ 
    // your logic 
} 

記住Windows.System.VirtualKey.Decimal將不會返回小數點(分隔符):

// this has to be always true 
e.Key >= Windows.System.VirtualKey.Number0 
&& 
e.Key <= Windows.System.VirtualKey.Number9 
|| // above or below has to be true 
e.Key >= Windows.System.VirtualKey.NumberPad0 
&& // something from above has to be true and below has to be true 
e.Key <= Windows.System.VirtualKey.NumberPad9 
|| // or just decimal mark .. ? 
e.Key == Windows.System.VirtualKey.Decimal 

於是用代碼繼續而是來自數字小鍵盤的小數點。

如果你想使用文化信息(國際申請),你可以在CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator找到小數標記,然後比較文本輸入。

相關問題