Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5891 was 5891, checked in by mkommend, 13 years ago

#1460: Corrected StringConvertibleValueView and adapted StringConvertibleValueTupleView and derived views.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Data.Views {
30  [View("StringConvertibleValue View")]
31  [Content(typeof(IStringConvertibleValue), true)]
32  public partial class StringConvertibleValueView : AsynchronousContentView {
33    public new IStringConvertibleValue Content {
34      get { return (IStringConvertibleValue)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public override bool ReadOnly {
39      get {
40        if ((Content != null) && Content.ReadOnly) return true;
41        return base.ReadOnly;
42      }
43      set { base.ReadOnly = value; }
44    }
45
46    public bool LabelVisible {
47      get { return valueLabel.Visible; }
48      set { valueLabel.Visible = value; }
49    }
50
51    public StringConvertibleValueView() {
52      InitializeComponent();
53      errorProvider.SetIconAlignment(valueTextBox, ErrorIconAlignment.MiddleLeft);
54      errorProvider.SetIconPadding(valueTextBox, 2);
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
59      base.DeregisterContentEvents();
60    }
61
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ValueChanged += new EventHandler(Content_ValueChanged);
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      if (Content == null) {
70        valueTextBox.Text = string.Empty;
71      } else
72        valueTextBox.Text = Content.GetValue();
73    }
74
75    protected override void SetEnabledStateOfControls() {
76      base.SetEnabledStateOfControls();
77      valueTextBox.Enabled = Content != null;
78      valueTextBox.ReadOnly = ReadOnly;
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.Select();  // select label to validate data
91
92      if (e.KeyCode == Keys.Escape) {
93        valueTextBox.Text = Content.GetValue();
94        valueLabel.Select();  // select label to validate data
95      }
96    }
97    private void valueTextBox_Validating(object sender, CancelEventArgs e) {
98      string errorMessage;
99      if (!Content.Validate(valueTextBox.Text, out errorMessage)) {
100        e.Cancel = true;
101        errorProvider.SetError(valueTextBox, errorMessage);
102        valueTextBox.SelectAll();
103      }
104    }
105    private void valueTextBox_Validated(object sender, EventArgs e) {
106      if (!Content.ReadOnly) Content.SetValue(valueTextBox.Text);
107      errorProvider.SetError(valueTextBox, string.Empty);
108      valueTextBox.Text = Content.GetValue();
109    }
110
111    private void valueLabel_VisibleChanged(object sender, EventArgs e) {
112      if (valueLabel.Visible) {
113        valueTextBox.Location = new Point(56, 0);
114        valueTextBox.Size = new Size(valueTextBox.Size.Width - valueLabel.Size.Width, valueTextBox.Size.Height);
115      } else {
116        valueTextBox.Location = new Point(19, 0);
117        valueTextBox.Size = new Size(valueTextBox.Size.Width + valueLabel.Size.Width, valueTextBox.Size.Height);
118      }
119    }
120
121  }
122}
Note: See TracBrowser for help on using the repository browser.