Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3444 was 3430, checked in by swagner, 14 years ago

Added ReadOnly property to all items of HeuristicLab.Data (#969)

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