LineWidthComboBox.cs 2.0 KB

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