code_junkie

Replacing the decimal point key (from numpad) with correct decimal separator (in silverlight!)

Nov 12th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. void Cell_KeyDown(object sender, KeyEventArgs e)
  2. {
  3. if (e.Key == Key.Decimal)
  4.  
  5. void CellText_KeyUp(object sender, KeyEventArgs e)
  6. {
  7. var DecSep = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
  8.  
  9. if (e.Key == Key.Decimal && DecSep != ".")
  10. {
  11. if (e.OriginalSource is TextBox)
  12. {
  13. var TB = (TextBox)e.OriginalSource;
  14. string sText = TB.Text;
  15.  
  16. int iPos = TB.SelectionStart - 1;
  17. if (iPos >= 0)
  18. {
  19. System.Diagnostics.Debug.Assert(sText.Substring(iPos, 1) == ".");
  20.  
  21. TB.Text = sText.Substring(0, iPos) + DecSep + sText.Substring(iPos + 1);
  22. TB.SelectionStart = iPos + 1; // reposition cursor
  23. }
  24. }
  25. }
  26. }
  27.  
  28. <html>
  29. <heaD>
  30. <script language="javascript">
  31. function keypress1 ()
  32. {
  33. var e=window.event || e
  34. unicode = e.charCode ? e.charCode : e.keyCode;
  35. if (unicode==46)
  36. { return (e.charCode ? e.charCode=44 : e.keyCode=44); }
  37. }
  38. function keypress2 ()
  39. {
  40. var e=window.event || e
  41. unicode = e.charCode ? e.charCode : e.keyCode;
  42. if (unicode==46)
  43. { return (e.charCode ? e.charCode=46 : e.keyCode=46); }
  44. }
  45. function keyDown(e){
  46. if (!e){
  47. e = event
  48. }
  49. var code=e.keyCode;
  50. if(code==110)
  51. return document.onkeypress=keypress1
  52. else if(code=188)
  53. { document.onkeypress=keypress2 }
  54. }
  55. document.onkeydown = keyDown
  56. </script>
  57. </head>
  58. <body>
  59. <input type=text>
  60. </body>
  61. </html>
  62.  
  63. public class NumericUpDown : System.Windows.Controls.NumericUpDown
  64. {
  65. [DebuggerStepThrough]
  66. protected override double ParseValue(string text)
  67. {
  68. text = text.Replace(".", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  69. return base.ParseValue(text);
  70. }
  71. }
Add Comment
Please, Sign In to add comment