LineWidthComboBox.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using FastReport.Utils;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. namespace FastReport.Controls
  6. {
  7. internal class LineWidthComboBox : ComboBox
  8. {
  9. private float lineWidth;
  10. private float[] stdWidths;
  11. public event EventHandler WidthSelected;
  12. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  13. public new ObjectCollection Items
  14. {
  15. get { return base.Items; }
  16. }
  17. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  18. public float LineWidth
  19. {
  20. get
  21. {
  22. string s = Text;
  23. if (s.StartsWith("."))
  24. s = "0" + s;
  25. float width = String.IsNullOrEmpty(s) ? 0 : Converter.StringToFloat(s);
  26. LineWidth = width;
  27. return width;
  28. }
  29. set
  30. {
  31. lineWidth = value;
  32. bool found = false;
  33. for (int i = 0; i < stdWidths.Length; i++)
  34. {
  35. if (Math.Abs(value - stdWidths[i]) < 1e-4)
  36. {
  37. Text = (string)Items[i];
  38. found = true;
  39. break;
  40. }
  41. }
  42. if (!found)
  43. {
  44. SelectedIndex = -1;
  45. Text = Converter.DecreasePrecision(lineWidth, 3).ToString();
  46. }
  47. }
  48. }
  49. private void OnWidthSelected()
  50. {
  51. if (WidthSelected != null)
  52. WidthSelected(this, EventArgs.Empty);
  53. }
  54. protected override void OnKeyDown(KeyEventArgs e)
  55. {
  56. if (e.KeyCode == Keys.Enter)
  57. {
  58. float w = LineWidth;
  59. OnWidthSelected();
  60. }
  61. }
  62. protected override void OnSelectedIndexChanged(EventArgs e)
  63. {
  64. base.OnSelectedIndexChanged(e);
  65. OnWidthSelected();
  66. }
  67. public LineWidthComboBox()
  68. {
  69. stdWidths = new float[] { 0.25f, 0.5f, 1, 1.5f, 2, 3, 4, 6 };
  70. Items.AddRange(new string[] {
  71. "0.25", "0.5", "1", "1.5", "2", "3", "4", "6" });
  72. }
  73. }
  74. }