tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Child.Material;
using DPumpHydr.WinFrmUI.RLT.Enum.Material;
using DPumpHydr.WinFrmUI.RLT.Helper;
using DPumpHydr.WinFrmUI.RLT.Manager;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.MaterialDrawHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region MaterialListBox
 
    [DefaultProperty("Items")]
    [DefaultEvent("SelectedIndexChanged")]
    [ComVisible(true)]
    public class MaterialListBox : Control, MaterialControlI
    {
        #region Internal Vars
 
        private List<object> _indicates;
        private bool _multiSelect;
        private int _selectedIndex;
        private MaterialListBoxItem _selectedItem;
        private bool _showScrollBar;
        private bool _multiKeyDown;
        private int _hoveredItem;
        private MaterialScrollBar _scrollBar;
        private bool _updating = false;
        private int _itemHeight;
        private bool _showBorder;
        private Color _borderColor;
        private Font _primaryFont;
        private Font _secondaryFont;
 
        private const int _leftrightPadding = 16;
        private int _primaryTextBottomPadding = 0;
        private int _secondaryTextTopPadding = 0;
        private int _secondaryTextBottomPadding = 0;
 
        public enum ListBoxStyle
        {
            SingleLine,
            TwoLine,
            ThreeLine
        }
        private ListBoxStyle _style = ListBoxStyle.SingleLine;
 
        public enum MaterialItemDensity
        {
            Default,
            Dense
        }
 
        private MaterialItemDensity _density;
 
        #endregion Internal Vars
 
        #region Properties
 
        //Properties for managing the material design properties
        [Browsable(false)]
        public int Depth { get; set; }
 
        [Browsable(false)]
        public MaterialSkinManager SkinManager => MaterialSkinManager.Instance;
 
        [Browsable(false)]
        public MaterialMouseState MouseState { get; set; }
 
        private bool useAccentColor;
 
        [Category("Material"), DefaultValue(false), DisplayName("Use Accent Color")]
        public bool UseAccentColor
        {
            get => useAccentColor;
            set { useAccentColor = value; _scrollBar.UseAccentColor = value; Invalidate(); }
        }
 
        [TypeConverter(typeof(CollectionConverter))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(MaterialItemCollectionEditor), typeof(UITypeEditor))]
        [Category("Material"), Description("Gets the items of the ListBox.")]
        public ObservableCollection<MaterialListBoxItem> Items { get; } = new ObservableCollection<MaterialListBoxItem>();
 
        [Browsable(false)]
        [Category("Material"), Description("Gets a collection containing the currently selected items in the ListBox.")]
        public List<object> SelectedItems { get; private set; }
 
        [Browsable(false), Category("Material"), Description("Gets or sets the currently selected item in the ListBox.")]
        public MaterialListBoxItem SelectedItem
        {
            get => _selectedItem;
            set
            {
                _selectedItem = value;
                _selectedIndex = Items.IndexOf(_selectedItem);
                update_selection();
                Invalidate();
            }
        }
 
        [Browsable(false), Category("Material"),
         Description("Gets the currently selected Text in the ListBox.")]
        public string SelectedText
        {
            get; private set;
            //set
            //{
            //    _selectedText = value;
            //    Invalidate();
            //}
        }
 
        [Browsable(false), Category("Material"), Description("Gets or sets the zero-based index of the currently selected item in a ListBox.")]
        public int SelectedIndex
        {
            get => _selectedIndex;
            set
            {
                _selectedIndex = value;
                update_selection();
                Invalidate();
            }
        }
 
        [Browsable(true), Category("Material"), Description("Gets the value of the member property specified by the ValueMember property.")]
        public object SelectedValue
        {
            get; private set;
            //set
            //{
            //    _selectedValue = value;
            //    Invalidate();
            //}
        }
 
        [Category("Material"), DefaultValue(false), Description("Gets or sets a value indicating whether the ListBox supports multiple rows.")]
        public bool MultiSelect
        {
            get => _multiSelect;
            set
            {
                _multiSelect = value;
 
                if (SelectedItems.Count > 1)
                {
                    SelectedItems.RemoveRange(1, SelectedItems.Count - 1);
                }
 
                Invalidate();
            }
        }
 
        [Browsable(false)]
        public int Count => Items.Count;
 
        [Category("Material"), DefaultValue(false), Description("Gets or sets a value indicating whether the vertical scroll bar be shown or not.")]
        public bool ShowScrollBar
        {
            get => _showScrollBar;
            set
            {
                _showScrollBar = value;
                _scrollBar.Visible = value;
                Invalidate();
            }
        }
 
        [Category("Material"), DefaultValue(true), Description("Gets or sets a value indicating whether the border shown or not.")]
        public bool ShowBorder
        {
            get => _showBorder;
            set
            {
                _showBorder = value;
                Refresh();
            }
        }
 
        [Category("Material"), Description("Gets or sets backcolor used by the control.")]
        public override Color BackColor { get; set; }
 
        [Category("Material"), Description("Gets or sets forecolor used by the control.")]
        public override Color ForeColor { get; set; }
 
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string Text { get => base.Text; set => base.Text = value; }
 
        [Category("Material"), Description("Gets or sets border color used by the control.")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                Refresh();
            }
        }
 
        [Category("Material"), DefaultValue(ListBoxStyle.SingleLine)]
        [Description("Gets or sets the control style.")]
        public ListBoxStyle Style
        {
            get => _style;
            set
            {
                _style = value;
                UpdateItemSpecs();
 
                InvalidateScroll(this, null);
                Refresh();
            }
        }
 
        [Category("Material"), DefaultValue(MaterialItemDensity.Dense)]
        [Description("Gets or sets list density")]
        public MaterialItemDensity Density
        {
            get => _density;
            set
            {
                _density = value;
                UpdateItemSpecs();
                Invalidate();
            }
        }
 
        #endregion Properties
 
        #region Constructors
 
        public MaterialListBox()
        {
            SetStyle
            (
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.Selectable |
                ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.SupportsTransparentBackColor,
                    true
            );
            UpdateStyles();
            base.BackColor = Color.Transparent;
            base.Font = SkinManager.GetFontByType(MaterialSkinManager.FontType.Subtitle1);
            _secondaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body1);
            SetDefaults();
            ShowBorder = true;
            ShowScrollBar = false;
            MultiSelect = false;
            UseAccentColor = false;
            ForeColor = SkinManager.TextHighEmphasisColor; // Color.Black;
            BackColor = Color.White;
            BorderColor = Color.LightGray;
            UpdateProperties();
        }
 
        private void SetDefaults()
        {
            SelectedIndex = -1;
            _hoveredItem = -1;
            _showScrollBar = false;
            Items.CollectionChanged += InvalidateScroll;
            SelectedItems = new List<object>();
            _indicates = new List<object>();
            _multiKeyDown = false;
            _scrollBar = new MaterialScrollBar()
            {
                Orientation = MateScrollOrientation.Vertical,
                Size = new Size(12, Height),
                Maximum = Items.Count * _itemHeight,
                SmallChange = _itemHeight,
                LargeChange = _itemHeight
            };
            _scrollBar.Scroll += HandleScroll;
            _scrollBar.MouseDown += VS_MouseDown;
            _scrollBar.BackColor = Color.Transparent;
            if (!Controls.Contains(_scrollBar))
            {
                Controls.Add(_scrollBar);
            }
 
            Style = ListBoxStyle.SingleLine;
            Density = MaterialItemDensity.Dense;
        }
 
        #endregion Constructors
 
        #region ApplyTheme
 
        private void UpdateProperties()
        {
            Invalidate();
        }
 
        private void UpdateItemSpecs()
        {
            if (_style == ListBoxStyle.TwoLine)
            {
                _secondaryTextTopPadding = 4;
                if (_density == MaterialItemDensity.Dense)
                {
                    _itemHeight = 60;
                    _secondaryTextBottomPadding = 10;
                    _primaryTextBottomPadding = 2;
                    _primaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body1);
                    _secondaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body2);
                }
                else
                {
                    _itemHeight = 72;
                    _secondaryTextBottomPadding = 16;
                    _primaryTextBottomPadding = 4;
                    _primaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Subtitle1);
                    _secondaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body1);
                }
            }
            else if (_style == ListBoxStyle.ThreeLine)
            {
                _primaryTextBottomPadding = 4;
                _secondaryTextTopPadding = 4;
                if (_density == MaterialItemDensity.Dense)
                {
                    _itemHeight = 76;
                    _secondaryTextBottomPadding = 16;
                    _primaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body1);
                    _secondaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body2);
                }
                else
                {
                    _itemHeight = 88;
                    _secondaryTextBottomPadding = 12;
                    _primaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Subtitle1);
                    _secondaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body1);
                }
            }
            else
            {
                //SingleLine
                if (_density == MaterialItemDensity.Dense)
                {
                    _itemHeight = 40;
                }
                else
                {
                    _itemHeight = 48;
                }
 
                _primaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Subtitle1);
                _secondaryFont = SkinManager.GetFontByType(MaterialSkinManager.FontType.Body1);
            }
 
        }
 
        #endregion ApplyTheme
 
        #region Draw Control
 
        protected override void OnPaint(PaintEventArgs e)
        {
            if (_updating == true)
            {
                return;
            }
 
            Graphics g = e.Graphics;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            Rectangle mainRect = new(0, 0, Width - (ShowBorder ? 1 : 0), Height - (ShowBorder ? 1 : 0));
 
            int lastItem = (_scrollBar.Value / _itemHeight) + (Height / _itemHeight) + 1 > Items.Count ? Items.Count : (_scrollBar.Value / _itemHeight) + (Height / _itemHeight) + 1;
            int firstItem = _scrollBar.Value / _itemHeight < 0 ? 0 : (_scrollBar.Value / _itemHeight);
 
            g.FillRectangle(Enabled ? SkinManager.BackgroundBrush : SkinManager.BackgroundDisabledBrush, mainRect);
 
            //Set TextAlignFlags
            MaterialNativeTextRenderer.TextAlignFlags primaryTextAlignFlags;
            MaterialNativeTextRenderer.TextAlignFlags secondaryTextAlignFlags = MaterialNativeTextRenderer.TextAlignFlags.Left | MaterialNativeTextRenderer.TextAlignFlags.Top;
            if (_style is ListBoxStyle.TwoLine or ListBoxStyle.ThreeLine)
            {
                primaryTextAlignFlags = MaterialNativeTextRenderer.TextAlignFlags.Left | MaterialNativeTextRenderer.TextAlignFlags.Bottom;
            }
            else
            {
                //SingleLine
                primaryTextAlignFlags = MaterialNativeTextRenderer.TextAlignFlags.Left | MaterialNativeTextRenderer.TextAlignFlags.Middle;
            }
 
            //Set color and brush
            Color SelectedColor = new();
            if (UseAccentColor)
            {
                SelectedColor = SkinManager.ColorScheme.AccentColor;
            }
            else
            {
                SelectedColor = SkinManager.ColorScheme.PrimaryColor;
            }
 
            SolidBrush SelectedBrush = new(SelectedColor);
 
            //Draw items
            for (int i = firstItem; i < lastItem; i++)
            {
                string itemText = Items[i].Text;
                string itemSecondaryText = Items[i].SecondaryText;
 
                Rectangle itemRect = new(0, (i - firstItem) * _itemHeight, Width - (_showScrollBar && _scrollBar.Visible ? _scrollBar.Width : 0), _itemHeight);
 
                if (MultiSelect && _indicates.Count != 0)
                {
                    if (i == _hoveredItem && !_indicates.Contains(i))
                    {
                        g.FillRectangle(SkinManager.BackgroundHoverBrush, itemRect);
                    }
                    else if (_indicates.Contains(i))
                    {
                        g.FillRectangle(Enabled ?
                            SelectedBrush :
                            new SolidBrush(BlendColor(SelectedColor, SkinManager.SwitchOffDisabledThumbColor, 197)),
                            itemRect);
                    }
                }
                else
                {
                    if (i == _hoveredItem && i != SelectedIndex)
                    {
                        g.FillRectangle(SkinManager.BackgroundHoverBrush, itemRect);
                    }
                    else if (i == SelectedIndex)
                    {
                        g.FillRectangle(Enabled ?
                            SelectedBrush :
                            new SolidBrush(BlendColor(SelectedColor, SkinManager.SwitchOffDisabledThumbColor, 197)),
                            itemRect);
                    }
                }
 
                //Define primary & secondary Text Rect
                Rectangle primaryTextRect = new(itemRect.X + _leftrightPadding, itemRect.Y, itemRect.Width - (2 * _leftrightPadding), itemRect.Height);
                Rectangle secondaryTextRect = new();
 
                if (_style == ListBoxStyle.TwoLine)
                {
                    primaryTextRect.Height = (primaryTextRect.Height / 2) - _primaryTextBottomPadding;
                }
                else if (_style == ListBoxStyle.ThreeLine)
                {
                    if (_density == MaterialItemDensity.Default)
                    {
                        primaryTextRect.Height = 36 - _primaryTextBottomPadding;
                    }
                    else
                    {
                        primaryTextRect.Height = 30 - _primaryTextBottomPadding;
                    }
                }
                secondaryTextRect = new Rectangle(primaryTextRect.X, primaryTextRect.Y + primaryTextRect.Height + _primaryTextBottomPadding + _secondaryTextTopPadding, primaryTextRect.Width, _itemHeight - _secondaryTextBottomPadding - primaryTextRect.Height - (_primaryTextBottomPadding + _secondaryTextTopPadding));
 
                using MaterialNativeTextRenderer NativeText = new(g);
                NativeText.DrawTransparentText(
                itemText,
                _primaryFont,
                Enabled ? (i != SelectedIndex || UseAccentColor) ?
                SkinManager.TextHighEmphasisColor :
                SkinManager.ColorScheme.TextColor :
                SkinManager.TextDisabledOrHintColor, // Disabled
                primaryTextRect.Location,
                primaryTextRect.Size,
                primaryTextAlignFlags);
                if (_style == ListBoxStyle.TwoLine)
                {
                    NativeText.DrawTransparentText(
                    itemSecondaryText,
                    _secondaryFont,
                    Enabled ? (i != SelectedIndex || UseAccentColor) ?
                    SkinManager.TextDisabledOrHintColor :
                    SkinManager.ColorScheme.TextColor.Darken(0.25f) :
                    SkinManager.TextDisabledOrHintColor, // Disabled
                    secondaryTextRect.Location,
                    secondaryTextRect.Size,
                    secondaryTextAlignFlags);
                }
                else if (_style == ListBoxStyle.ThreeLine)
                {
                    NativeText.DrawMultilineTransparentText(
                    itemSecondaryText,
                    _secondaryFont,
                    Enabled ? (i != SelectedIndex || UseAccentColor) ?
                    SkinManager.TextDisabledOrHintColor :
                    SkinManager.ColorScheme.TextColor.Darken(0.25f) :
                    SkinManager.TextDisabledOrHintColor, // Disabled
                    secondaryTextRect.Location,
                    secondaryTextRect.Size,
                    secondaryTextAlignFlags);
                }
 
            }
            if (ShowBorder)
            {
                g.DrawRectangle(Pens.LightGray, mainRect);
            }
        }
 
        #endregion Draw Control
 
        #region Methods
 
        public void AddItem(MaterialListBoxItem newItem)
        {
            Items.Add(newItem);
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        public void AddItem(string newItem)
        {
            MaterialListBoxItem _newitemMLBI = new(newItem);
            Items.Add(_newitemMLBI);
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        public void AddItems(MaterialListBoxItem[] newItems)
        {
            _updating = true;
            foreach (MaterialListBoxItem str in newItems)
            {
                AddItem(str);
            }
            _updating = false;
 
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        public void AddItems(string[] newItems)
        {
            _updating = true;
            foreach (string str in newItems)
            {
                AddItem(str);
            }
            _updating = false;
 
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        public void RemoveItemAt(int index)
        {
            if (index <= _selectedIndex)
            {
                _selectedIndex -= 1;
                update_selection();
            }
            Items.RemoveAt(index);
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        public void RemoveItem(MaterialListBoxItem item)
        {
            if (Items.IndexOf(item) <= _selectedIndex)
            {
                _selectedIndex -= 1;
                update_selection();
            }
            Items.Remove(item);
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        public int IndexOf(MaterialListBoxItem value)
        {
            return Items.IndexOf(value);
        }
 
        public void RemoveItems(MaterialListBoxItem[] itemsToRemove)
        {
            _updating = true;
            foreach (MaterialListBoxItem item in itemsToRemove)
            {
                if (Items.IndexOf(item) <= _selectedIndex)
                {
                    _selectedIndex -= 1;
                    update_selection();
                }
                Items.Remove(item);
            }
            _updating = false;
 
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        private void update_selection()
        {
            if (_selectedIndex >= 0)
            {
                _selectedItem = Items[_selectedIndex];
                SelectedValue = Items[_selectedIndex];
                SelectedText = Items[_selectedIndex].ToString();
            }
            else
            {
                _selectedItem = null;
                SelectedValue = null;
                SelectedText = null;
            }
        }
 
        public void Clear()
        {
            _updating = true;
            for (int i = Items.Count - 1; i >= 0; i += -1)
            {
                Items.RemoveAt(i);
            }
            _updating = false;
            _selectedIndex = -1;
            update_selection();
 
            InvalidateScroll(this, null);
            ItemsCountChanged?.Invoke(this, new EventArgs());
        }
 
        public void BeginUpdate()
        {
            _updating = true;
        }
 
        public void EndUpdate()
        {
            _updating = false;
        }
 
        #endregion Methods
 
        #region Events
 
        [Category("Behavior")]
        [Description("Occurs when selected index change.")]
        public event SelectedIndexChangedEventHandler SelectedIndexChanged;
 
        public delegate void SelectedIndexChangedEventHandler(object sender, MaterialListBoxItem selectedItem);
 
        [Category("Behavior")]
        [Description("Occurs when selected value change.")]
        public event SelectedValueEventHandler SelectedValueChanged;
 
        public delegate void SelectedValueEventHandler(object sender, MaterialListBoxItem selectedItem);
 
        [Category("Behavior")]
        [Description("Occurs when item is added or removed.")]
        public event EventHandler ItemsCountChanged;
 
        #endregion Events
 
        protected override void OnSizeChanged(EventArgs e)
        {
            InvalidateScroll(this, e);
            InvalidateLayout();
            base.OnSizeChanged(e);
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            Focus();
            if (e.Button == MouseButtons.Left)
            {
                int index = (_scrollBar.Value / _itemHeight) + (e.Location.Y / _itemHeight);
                if (index >= 0 && index < Items.Count)
                {
                    if (MultiSelect && _multiKeyDown)
                    {
                        _indicates.Add(index);
                        SelectedItems.Add(Items[index]);
                    }
                    else
                    {
                        _indicates.Clear();
                        SelectedItems.Clear();
                        _selectedItem = Items[index];
                        _selectedIndex = index;
                        SelectedValue = Items[index];
                        SelectedText = Items[index].ToString();
                        SelectedIndexChanged?.Invoke(this, _selectedItem);
                        SelectedValueChanged?.Invoke(this, _selectedItem);
                    }
                }
                Invalidate();
            }
            base.OnMouseDown(e);
        }
 
        private void HandleScroll(object sender, ScrollEventArgs e)
        {
            if (_scrollBar.Maximum < _scrollBar.Value + Height)
            {
                _scrollBar.Value = _scrollBar.Maximum - Height;
            }
 
            Invalidate();
        }
 
        private void InvalidateScroll(object sender, EventArgs e)
        {
            _scrollBar.Maximum = Items.Count * _itemHeight;
            _scrollBar.SmallChange = _itemHeight;
            _scrollBar.LargeChange = Height;
            _scrollBar.Visible = (Items.Count * _itemHeight) > Height;
            if (Items.Count == 0)
            { _scrollBar.Value = 0; }
            Invalidate();
        }
 
        private void VS_MouseDown(object sender, MouseEventArgs e)
        {
            Focus();
        }
 
        private void InvalidateLayout()
        {
            _scrollBar.Size = new Size(12, Height - (ShowBorder ? 2 : 0));
            _scrollBar.Location = new Point(Width - (_scrollBar.Width + (ShowBorder ? 1 : 0)), ShowBorder ? 1 : 0);
            Invalidate();
        }
 
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            if (_scrollBar.Visible == true)
            {
                if (_scrollBar.Minimum > _scrollBar.Value - (e.Delta / 2))
                {
                    _scrollBar.Value = _scrollBar.Minimum;
                }
                else if (_scrollBar.Maximum < _scrollBar.Value + Height)
                {
                    if (e.Delta > 0)
                    {
                        _scrollBar.Value -= e.Delta / 2;
                    }
                    else
                    { } //Do nothing, maximum reached
                }
                else
                {
                    _scrollBar.Value -= e.Delta / 2;
                }
 
                _updateHoveredItem(e);
 
                Invalidate();
                base.OnMouseWheel(e);
                ((HandledMouseEventArgs)e).Handled = true;
            }
        }
 
        protected override bool IsInputKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Down:
                    try
                    {
                        SelectedItems.Remove(Items[SelectedIndex]);
                        SelectedIndex += 1;
                        SelectedItems.Add(Items[SelectedIndex]);
                    }
                    catch
                    {
                        //
                    }
                    break;
 
                case Keys.Up:
                    try
                    {
                        SelectedItems.Remove(Items[SelectedIndex]);
                        SelectedIndex -= 1;
                        SelectedItems.Add(Items[SelectedIndex]);
                    }
                    catch
                    {
                        //
                    }
                    break;
            }
            Invalidate();
            return base.IsInputKey(keyData);
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Cursor = Cursors.Hand;
            _updateHoveredItem(e);
 
            Invalidate();
        }
 
        private void _updateHoveredItem(MouseEventArgs e)
        {
            int index = (_scrollBar.Value / _itemHeight) + (e.Location.Y / _itemHeight);
 
            if (index >= Items.Count)
            {
                index = -1;
            }
 
            if (index >= 0 && index < Items.Count)
            {
                _hoveredItem = index;
            }
 
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            _hoveredItem = -1;
            Cursor = Cursors.Default;
            Invalidate();
            base.OnMouseLeave(e);
        }
 
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            _scrollBar.Size = new Size(12, Height - (ShowBorder ? 2 : 0));
            _scrollBar.Location = new Point(Width - (_scrollBar.Width + (ShowBorder ? 1 : 0)), ShowBorder ? 1 : 0);
            InvalidateScroll(this, e);
        }
 
        public const int WM_SETCURSOR = 0x0020;
        public const int IDC_HAND = 32649;
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetCursor(IntPtr hCursor);
 
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SETCURSOR)
            {
                SetCursor(LoadCursor(IntPtr.Zero, IDC_HAND));
                m.Result = IntPtr.Zero;
                return;
            }
            base.WndProc(ref m);
        }
    }
 
    #endregion
}