public CarbolistItem AddItem(string title, object data = null) { if (title == null) { throw new Exception("Argument title must be non-null."); } Color backgroundColor = Items.Count % 2 == 0 ? SampleButtonColors.BlueDark : SampleButtonColors.BlueLight; CarbolistItem item = new CarbolistItem( title, backgroundColor, SampleButtonColors.BlueSelected, itemSize, new Point(0, Items.Count * itemSize.Height), itemFont, data, this); Controls.Add(item); item.Background.Click += OnCarbolistItemClick; item.Background.MouseDown += OnCarbolistItemMouseDown; Items.Add(item); Height += item.Height; IsDragged = false; return(item); }
protected void OnCarbolistItemClick(object target, EventArgs e) { CarbolistItem item = ((Button)target).Parent as CarbolistItem; if (item == SelectedItem) { if (CanDeselect) { item.Selected = false; SelectedItem = null; } } else { if (SelectedItem != null) { SelectedItem.Selected = false; } item.Selected = true; SelectedItem = item; } // callback event listener OnSelect?.Invoke(item); }
public void RemoveItem(CarbolistItem item) { if (item == null) { throw new Exception("Argument item must be non-null"); } if (item == SelectedItem) { SelectedItem = null; } Controls.Remove(item); Items.Remove(item); item.Background.Click -= OnCarbolistItemClick; item.Background.MouseDown -= OnCarbolistItemMouseDown; item.Dispose(); Height -= itemSize.Height; IsDragged = false; SortItems(); if (!CanDeselect) { SelectItem(Items[Items.Count - 1]); } }
public CarbolistItem SelectItem(CarbolistItem item) { if (!Items.Contains(item)) { throw new Exception("Item not found in this Carbolist."); } OnCarbolistItemClick(item.Background, null); return(item); }
protected void OnDraggingItemMouseUp(object target, MouseEventArgs e) { Button button = target as Button; button.MouseMove -= OnDraggingItemMouseMove; button.MouseUp -= OnDraggingItemMouseUp; // restore click event if (isDraggingItem) { button.Click += OnCarbolistItemClick; } draggingItem = null; isDraggingItem = false; SortItems(); if (!IsDragged) { IsDragged = true; } }
protected void OnCarbolistItemMouseDown(object target, MouseEventArgs e) { // drag-and-drop preparations // drag-and-drop only when the list has more than 1 item. if (Items.Count <= 1) { return; } if (e.Button != MouseButtons.Left) { return; } Button button = target as Button; draggingItem = button.Parent as CarbolistItem; draggingStartPoint = e.Location; button.MouseMove += OnDraggingItemMouseMove; button.MouseUp += OnDraggingItemMouseUp; }