Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleValueView.cs @ 3350

Last change on this file since 3350 was 3350, checked in by mkommend, 14 years ago

implemented first version of View.ReadOnly and adapted some views to the new mechanism (ticket #973)

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