Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifierView/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SymbolicExpressionTreeNodeChangeValueDialog.cs @ 8250

Last change on this file since 8250 was 7411, checked in by bburlacu, 13 years ago

#1763: Improved ValueChangeDialog and overall behavior. Implemented pruning operation on background thread. TODO: Test, improve. This is a work-in-progress.

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.ComponentModel;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
29  public partial class ValueChangeDialog : Form {
30    public string Caption {
31      get { return this.Text; }
32      set {
33        if (InvokeRequired)
34          Invoke(new Action<string>(x => this.Caption = x), value);
35        else
36          this.Text = value;
37      }
38    }
39    public string OriginalValue {
40      get { return originalValueTextBox.Text; }
41      set {
42        if (InvokeRequired)
43          Invoke(new Action<string>(x => this.NewValue = x), value);
44        else
45          originalValueTextBox.Text = value;
46      }
47    }
48    public string NewValue {
49      get { return newValueTextBox.Text; }
50      set {
51        if (InvokeRequired)
52          Invoke(new Action<string>(x => this.NewValue = x), value);
53        else
54          newValueTextBox.Text = value;
55      }
56    }
57    public TextBox NewValueTextBox {
58      get { return newValueTextBox; }
59    }
60
61    public ValueChangeDialog() {
62      InitializeComponent();
63      originalValueTextBox.TabStop = false; // cannot receive focus using tab key
64    }
65    public ValueChangeDialog(string caption, string originalValue, string newValue)
66      : this() {
67      Caption = caption;
68      OriginalValue = originalValue;
69      NewValue = newValue;
70    }
71
72    protected virtual void okButton_Click(object sender, EventArgs e) {
73      Close();
74    }
75
76    private void newValueTextBox_KeyDown(object sender, KeyEventArgs e) {
77      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
78        newValueLabel.Select();  // select label to validate data
79
80      if (e.KeyCode == Keys.Escape) {
81        newValueTextBox.Text = String.Empty;
82        newValueLabel.Select();  // select label to validate data
83      }
84    }
85    private void newValueTextBox_Validating(object sender, CancelEventArgs e) {
86      string errorMessage;
87      if (!Validate(newValueTextBox.Text, out errorMessage)) {
88        e.Cancel = true;
89        errorProvider.SetError(newValueTextBox, errorMessage);
90        newValueTextBox.SelectAll();
91      }
92    }
93    private void newValueTextBox_Validated(object sender, EventArgs e) {
94      errorProvider.SetError(newValueTextBox, string.Empty);
95    }
96
97    private static bool Validate(string value, out string errorMessage) {
98      double val;
99      bool valid = double.TryParse(value, out val);
100      errorMessage = string.Empty;
101      if (!valid) {
102        var sb = new StringBuilder();
103        sb.Append("Invalid Value (Valid Value Format: \"");
104        sb.Append(FormatPatterns.GetDoubleFormatPattern());
105        sb.Append("\")");
106        errorMessage = sb.ToString();
107      }
108      return valid;
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.