Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Advanced/MultiSelectListView.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
22using System.Linq;
23using System.Windows.Forms;
24
25namespace HeuristicLab.PluginInfrastructure.Advanced {
26  internal partial class MultiSelectListView : ListView {
27    public MultiSelectListView() {
28      InitializeComponent();
29    }
30
31    private bool inhibitAutoCheck;
32    protected override void OnMouseDown(MouseEventArgs e) {
33      inhibitAutoCheck = true;
34      base.OnMouseDown(e);
35    }
36
37    protected override void OnMouseUp(MouseEventArgs e) {
38      base.OnMouseUp(e);
39      inhibitAutoCheck = false;
40    }
41
42    // item check is raised for each selected item that was not directly clicked (those should be ignored)
43    // and then one the item whose checkbox was clicked
44    protected override void OnItemCheck(ItemCheckEventArgs ice) {
45      // don't change the checked state of items that were not clicked directly
46      if (inhibitAutoCheck) {
47        ice.NewValue = ice.CurrentValue;
48      }
49      // if this item was directly clicked then check if it is selected as well
50      var item = Items[ice.Index];
51      if (!item.Selected) {
52        // when the item was not selected previously
53        // clear any selection
54        List<ListViewItem> selectedItems = new List<ListViewItem>(SelectedItems.OfType<ListViewItem>());
55        selectedItems.ForEach(x => x.Selected = false);
56        // select only the clicked item
57        item.Selected = true;
58      }
59      base.OnItemCheck(ice);
60    }
61
62    private bool suppressItemCheckedEvents;
63    public bool SuppressItemCheckedEvents {
64      get {
65        return suppressItemCheckedEvents;
66      }
67      set {
68        suppressItemCheckedEvents = value;
69      }
70    }
71    protected override void OnItemChecked(ItemCheckedEventArgs e) {
72      if (!suppressItemCheckedEvents)
73        base.OnItemChecked(e);
74    }
75
76    public void CheckItems(IEnumerable<ListViewItem> items) {
77      suppressItemCheckedEvents = true;
78
79      foreach (var item in items)
80        item.Checked = true;
81      suppressItemCheckedEvents = false;
82    }
83
84    public void UncheckItems(IEnumerable<ListViewItem> items) {
85      suppressItemCheckedEvents = true;
86
87      foreach (var item in items)
88        item.Checked = false;
89      suppressItemCheckedEvents = false;
90    }
91
92  }
93}
Note: See TracBrowser for help on using the repository browser.