CNumeric.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Controls
  11. {
  12. public partial class CNumeric : UserControl
  13. {
  14. public CNumeric()
  15. {
  16. InitializeComponent();
  17. tbx_value.Text = _value.ToString();
  18. }
  19. /// <summary>
  20. /// 值改变事件
  21. /// </summary>
  22. public event DValueChanged ValueChanged;
  23. /// <summary>
  24. /// 值
  25. /// </summary>
  26. private string _value = string.Empty;
  27. public string Value
  28. {
  29. get
  30. {
  31. return _value;
  32. }
  33. set
  34. {
  35. _value = value;
  36. tbx_value.Text = value.ToString();
  37. }
  38. }
  39. private void tbx_value_KeyPress(object sender, KeyPressEventArgs e)
  40. {
  41. try
  42. {
  43. /*只能数字键、退格键、负号、小数点*/
  44. if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 &&
  45. (int)e.KeyChar != 45 && (int)e.KeyChar != 46) e.Handled = true;
  46. /*输入为负号和小数点时,且只能输入一次(负号只能最前面输入,小数点不可最前面输入)*/
  47. if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 ||
  48. ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
  49. if (e.KeyChar == 46 && (((TextBox)sender).SelectionStart == 0 ||
  50. ((TextBox)sender).Text.IndexOf(".") >= 0)) e.Handled = true;
  51. }
  52. catch { }
  53. }
  54. private void Numeric_Enter(object sender, EventArgs e)
  55. {
  56. lbl_line.Height = 2;
  57. lbl_line.BackColor = Color.FromArgb(18, 150, 219);
  58. }
  59. private void Numeric_Leave(object sender, EventArgs e)
  60. {
  61. lbl_line.Height = 1;
  62. lbl_line.BackColor = Color.Gray;
  63. }
  64. private void tbx_value_TextChanged(object sender, EventArgs e)
  65. {
  66. if (tbx_value.Text != string.Empty && tbx_value.Text != Value.ToString())
  67. Value = tbx_value.Text;
  68. if (ValueChanged != null)
  69. ValueChanged(Convert.ToDouble(Value));
  70. }
  71. }
  72. }