Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleDataView.cs @ 2971

Last change on this file since 2971 was 2932, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • replaced Changed in IItem by ToStringChanged
File size: 3.7 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
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;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Data.Views {
35  [View("StringConvertibleData View")]
36  [Content(typeof(IStringConvertibleData), true)]
37  public partial class StringConvertibleDataView : ContentView {
38    public new IStringConvertibleData Content {
39      get { return (IStringConvertibleData)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public StringConvertibleDataView() {
44      InitializeComponent();
45      Caption = "StringConvertibleData View";
46      errorProvider.SetIconAlignment(valueTextBox, ErrorIconAlignment.MiddleLeft);
47      errorProvider.SetIconPadding(valueTextBox, 2);
48    }
49    public StringConvertibleDataView(IStringConvertibleData content)
50      : this() {
51      Content = content;
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
56      base.DeregisterContentEvents();
57    }
58
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.ValueChanged += new EventHandler(Content_ValueChanged);
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      if (Content == null) {
67        Caption = "StringConvertibleData View";
68        valueTextBox.Text = string.Empty;
69        valueTextBox.Enabled = false;
70      } else {
71        Caption = Content.GetValue() + " (" + Content.GetType().Name + ")";
72        valueTextBox.Text = Content.GetValue();
73        valueTextBox.Enabled = true;
74      }
75    }
76
77    private void Content_ValueChanged(object sender, EventArgs e) {
78      if (InvokeRequired)
79        Invoke(new EventHandler(Content_ValueChanged), sender, e);
80      else
81        valueTextBox.Text = Content.GetValue();
82    }
83
84    private void valueTextBox_KeyDown(object sender, KeyEventArgs e) {
85      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
86        valueLabel.Focus();  // set focus on label to validate data
87      if (e.KeyCode == Keys.Escape) {
88        valueTextBox.Text = Content.GetValue();
89        valueLabel.Focus();  // set focus on label to validate data
90      }
91    }
92    private void valueTextBox_Validating(object sender, CancelEventArgs e) {
93      string errorMessage;
94      if (!Content.Validate(valueTextBox.Text, out errorMessage)) {
95        e.Cancel = true;
96        errorProvider.SetError(valueTextBox, errorMessage);
97        valueTextBox.SelectAll();
98      }
99    }
100    private void valueTextBox_Validated(object sender, EventArgs e) {
101      Content.SetValue(valueTextBox.Text);
102      errorProvider.SetError(valueTextBox, string.Empty);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.