Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.MainForm.WindowsForms/3.3/Dialogs/DefineArithmeticProgressionDialog.cs @ 11761

Last change on this file since 11761 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 3.9 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
21
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.Globalization;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.MainForm.WindowsForms {
30  public partial class DefineArithmeticProgressionDialog : Form {
31    private bool allowOnlyInteger;
32
33    public double Minimum { get; private set; }
34    public double Maximum { get; private set; }
35    public double Step { get; private set; }
36
37    public IEnumerable<double> Values {
38      get { return EnumerateProgression().Reverse(); }
39    }
40
41    public DefineArithmeticProgressionDialog() {
42      InitializeComponent();
43      Icon = HeuristicLab.Common.Resources.HeuristicLab.Icon;
44      Minimum = 0; Maximum = 100; Step = 10;
45    }
46    public DefineArithmeticProgressionDialog(bool allowOnlyInteger)
47      : this() {
48      this.allowOnlyInteger = allowOnlyInteger;
49    }
50    public DefineArithmeticProgressionDialog(bool allowOnlyInteger, double minimum, double maximum, double step)
51      : this(allowOnlyInteger) {
52      Minimum = minimum;
53      Maximum = maximum;
54      Step = step;
55    }
56
57    private void DefineArithmeticProgressionDialog_Load(object sender, System.EventArgs e) {
58      minimumTextBox.Text = Minimum.ToString();
59      maximumTextBox.Text = Maximum.ToString();
60      stepSizeTextBox.Text = Step.ToString();
61    }
62
63    private void textBox_Validating(object sender, CancelEventArgs e) {
64      var textBox = (TextBox)sender;
65      double value = 0;
66      if (allowOnlyInteger) {
67        int intValue;
68        if (!int.TryParse(textBox.Text, out intValue)) {
69          errorProvider.SetError(textBox, "Please enter a valid integer value.");
70          e.Cancel = true;
71          return;
72        } else {
73          value = intValue;
74          errorProvider.SetError(textBox, null);
75        }
76      } else {
77        if (!double.TryParse(textBox.Text, NumberStyles.Float, CultureInfo.CurrentCulture.NumberFormat, out value)) {
78          errorProvider.SetError(textBox, "Please enter a valid double value.");
79          e.Cancel = true;
80          return;
81        } else errorProvider.SetError(textBox, null);
82      }
83      if (textBox == minimumTextBox) Minimum = value;
84      else if (textBox == maximumTextBox) Maximum = value;
85      else if (textBox == stepSizeTextBox) Step = value;
86      okButton.Enabled = IsValid();
87    }
88
89    private bool IsValid() {
90      return Minimum <= Maximum && Step >= 0;
91    }
92
93    private IEnumerable<double> EnumerateProgression() {
94      double value = Maximum;
95      bool minimumIncluded = false;
96      int i = 1;
97      while (value >= Minimum) {
98        if (value.IsAlmost(Minimum)) {
99          yield return Minimum;
100          minimumIncluded = true;
101        } else yield return value;
102
103        if (Step == 0) break; // a step size of 0 will only output maximum and minimum
104        if (allowOnlyInteger) {
105          value = (int)Maximum - i * (int)Step;
106        } else {
107          value = Maximum - i * Step;
108        }
109        i++;
110      }
111      if (!minimumIncluded) {
112        yield return Minimum;
113      }
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.