Free cookie consent management tool by TermsFeed Policy Generator

source: branches/irace/HeuristicLab.PluginInfrastructure/3.3/Advanced/MultiSelectListView.cs @ 11868

Last change on this file since 11868 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Windows.Forms;
25
26namespace HeuristicLab.PluginInfrastructure.Advanced {
27  internal partial class MultiSelectListView : ListView {
28    public MultiSelectListView() {
29      InitializeComponent();
30    }
31
32    private bool inhibitAutoCheck;
33    protected override void OnMouseDown(MouseEventArgs e) {
34      inhibitAutoCheck = true;
35      base.OnMouseDown(e);
36    }
37
38    protected override void OnMouseUp(MouseEventArgs e) {
39      base.OnMouseUp(e);
40      inhibitAutoCheck = false;
41    }
42
43    // item check is raised for each selected item that was not directly clicked (those should be ignored)
44    // and then one the item whose checkbox was clicked
45    protected override void OnItemCheck(ItemCheckEventArgs ice) {
46      if (ice == null) throw new ArgumentNullException("ice");
47      // don't change the checked state of items that were not clicked directly
48      if (inhibitAutoCheck) {
49        ice.NewValue = ice.CurrentValue;
50      }
51      // if this item was directly clicked then check if it is selected as well
52      var item = Items[ice.Index];
53      if (!item.Selected) {
54        // when the item was not selected previously
55        // clear any selection
56        List<ListViewItem> selectedItems = new List<ListViewItem>(SelectedItems.OfType<ListViewItem>());
57        selectedItems.ForEach(x => x.Selected = false);
58        // select only the clicked item
59        item.Selected = true;
60      }
61      base.OnItemCheck(ice);
62    }
63
64    private bool suppressItemCheckedEvents;
65    public bool SuppressItemCheckedEvents {
66      get {
67        return suppressItemCheckedEvents;
68      }
69      set {
70        suppressItemCheckedEvents = value;
71      }
72    }
73    protected override void OnItemChecked(ItemCheckedEventArgs e) {
74      if (!suppressItemCheckedEvents)
75        base.OnItemChecked(e);
76    }
77
78    public void CheckItems(IEnumerable<ListViewItem> items) {
79      suppressItemCheckedEvents = true;
80
81      foreach (var item in items)
82        item.Checked = true;
83      suppressItemCheckedEvents = false;
84    }
85
86    public void UncheckItems(IEnumerable<ListViewItem> items) {
87      suppressItemCheckedEvents = true;
88
89      foreach (var item in items)
90        item.Checked = false;
91      suppressItemCheckedEvents = false;
92    }
93
94  }
95}
Note: See TracBrowser for help on using the repository browser.