Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ConstrainedIntDataView.cs @ 1853

Last change on this file since 1853 was 1529, checked in by gkronber, 15 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30
31namespace HeuristicLab.Data {
32  /// <summary>
33  /// The visual representation of the class <see cref="ConstrainedIntDataView"/>,
34  /// symbolizing an int value with some constraints.
35  /// </summary>
36  public partial class ConstrainedIntDataView : ViewBase {
37    /// <summary>
38    /// Gets or sets the int value to represent visually.
39    /// </summary>
40    /// <remarks>Uses property <see cref="HeuristicLab.Core.ViewBase.Item"/> of base class <see cref="ViewBase"/>,
41    /// but also own data storage present.</remarks>
42    public ConstrainedIntData ConstrainedIntData {
43      get { return (ConstrainedIntData)Item; }
44      set {
45        base.Item = value;
46        constrainedDataBaseView.ConstrainedItem = value;
47      }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of class <see cref="ConstrainedIntDataView"/>.
52    /// </summary>
53    public ConstrainedIntDataView() {
54      InitializeComponent();
55    }
56    /// <summary>
57    /// Initializes a new instance of class <see cref="ConstrainedIntDataView"/> with the given
58    /// <paramref name="constraintIntData"/>.
59    /// <note type="caution"> No CopyConstructor! <paramref name="constraintIntData"/> is not copied!</note>
60    /// </summary>
61    /// <param name="constraintIntData">The int value to represent visually.</param>
62    public ConstrainedIntDataView(ConstrainedIntData constraintIntData)
63      : this() {
64      ConstrainedIntData = constraintIntData;
65    }
66
67    /// <summary>
68    /// Removes the eventhandler from the underlying <see cref="ConstrainedIntData"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
71    /// </remarks>
72    protected override void RemoveItemEvents() {
73      ConstrainedIntData.Changed -= new EventHandler(ConstrainedIntData_Changed);
74      base.RemoveItemEvents();
75    }
76    /// <summary>
77    /// Adds an eventhandler to the underlying <see cref="ConstrainedIntData"/>.
78    /// </summary>
79    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
80    /// </remarks>
81    protected override void AddItemEvents() {
82      base.AddItemEvents();
83      ConstrainedIntData.Changed += new EventHandler(ConstrainedIntData_Changed);
84    }
85
86    /// <summary>
87    /// Updates the controls with the latest int value.
88    /// </summary>
89    protected override void UpdateControls() {
90      base.UpdateControls();
91      if (ConstrainedIntData == null) {
92        dataTextBox.Enabled = false;
93      } else {
94        dataTextBox.Enabled = true;
95        dataTextBox.Text = ConstrainedIntData.ToString();
96      }
97    }
98
99    private void dataTextBox_Validating(object sender, CancelEventArgs e) {
100      e.Cancel = false;
101      int value;
102      try {
103        value = int.Parse(dataTextBox.Text);
104        ICollection<IConstraint> violatedConstraints;
105        if (!ConstrainedIntData.TrySetData(value, out violatedConstraints)) {
106          if (Auxiliary.ShowIgnoreConstraintViolationMessageBox(violatedConstraints) == DialogResult.Yes)
107            ConstrainedIntData.Data = value;
108          else
109            e.Cancel = true;
110        }
111      }
112      catch (Exception) {
113        dataTextBox.SelectAll();
114        e.Cancel = true;
115      }
116    }
117
118    private void ConstrainedIntData_Changed(object sender, EventArgs e) {
119      Refresh();
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.