MatrixStyleListBox.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using FastReport.Matrix;
  8. using FastReport.Utils;
  9. namespace FastReport.Controls
  10. {
  11. #if !DEBUG
  12. [DesignTimeVisible(false)]
  13. #endif
  14. internal class MatrixStyleListBox : ListBox
  15. {
  16. private bool updating;
  17. private MatrixStyleSheet styles;
  18. public event EventHandler StyleSelected;
  19. public string Style
  20. {
  21. get
  22. {
  23. if (SelectedIndex < 1)
  24. return "";
  25. return (string)Items[SelectedIndex];
  26. }
  27. set
  28. {
  29. updating = true;
  30. int i = Items.IndexOf(value);
  31. SelectedIndex = i != -1 ? i : 0;
  32. updating = false;
  33. }
  34. }
  35. public MatrixStyleSheet Styles
  36. {
  37. get { return styles; }
  38. set
  39. {
  40. styles = value;
  41. if (value != null)
  42. UpdateItems();
  43. }
  44. }
  45. protected override void OnDrawItem(DrawItemEventArgs e)
  46. {
  47. e.DrawBackground();
  48. Graphics g = e.Graphics;
  49. if (e.Index >= 0)
  50. {
  51. string name = (string)Items[e.Index];
  52. int styleIndex = styles.IndexOf(name);
  53. if (styleIndex != -1)
  54. name = Res.Get("ComponentsMisc,Matrix," + name);
  55. Image img = styleIndex == -1 ? this.GetImage(76) : Styles.GetStyleBitmap(styleIndex);
  56. this.DrawImageAndText(e, img, name);
  57. }
  58. }
  59. protected override void OnSelectedIndexChanged(EventArgs e)
  60. {
  61. base.OnSelectedIndexChanged(e);
  62. if (updating)
  63. return;
  64. if (StyleSelected != null)
  65. StyleSelected(this, EventArgs.Empty);
  66. }
  67. private void UpdateItems()
  68. {
  69. Items.Clear();
  70. Items.Add(Res.Get("Designer,Toolbar,Style,NoStyle"));
  71. foreach (StyleCollection s in styles)
  72. {
  73. Items.Add(s.Name);
  74. }
  75. }
  76. public MatrixStyleListBox()
  77. {
  78. DrawMode = DrawMode.OwnerDrawFixed;
  79. ItemHeight = 19;
  80. IntegralHeight = false;
  81. Size = new Size(150, 300);
  82. }
  83. }
  84. }