Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs @ 3838

Last change on this file since 3838 was 3764, checked in by mkommend, 15 years ago

adapted view captions (ticket #893)

File size: 6.1 KB
RevLine 
[2973]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.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Data.Views {
[3048]31  [View("StringConvertibleArray View")]
32  [Content(typeof(IStringConvertibleArray), true)]
[3228]33  public partial class StringConvertibleArrayView : AsynchronousContentView {
[3048]34    public new IStringConvertibleArray Content {
35      get { return (IStringConvertibleArray)base.Content; }
[2973]36      set { base.Content = value; }
37    }
38
[3430]39    public override bool ReadOnly {
40      get {
41        if ((Content != null) && Content.ReadOnly) return true;
42        return base.ReadOnly;
43      }
44      set { base.ReadOnly = value; }
45    }
46
[3048]47    public StringConvertibleArrayView() {
[2973]48      InitializeComponent();
[2974]49      errorProvider.SetIconAlignment(lengthTextBox, ErrorIconAlignment.MiddleLeft);
50      errorProvider.SetIconPadding(lengthTextBox, 2);
[2973]51    }
52
53    protected override void DeregisterContentEvents() {
54      Content.ItemChanged -= new EventHandler<EventArgs<int>>(Content_ItemChanged);
55      Content.Reset -= new EventHandler(Content_Reset);
56      base.DeregisterContentEvents();
57    }
58
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.ItemChanged += new EventHandler<EventArgs<int>>(Content_ItemChanged);
62      Content.Reset += new EventHandler(Content_Reset);
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content == null) {
[2974]68        lengthTextBox.Text = "";
[2973]69        dataGridView.Rows.Clear();
70        dataGridView.Columns.Clear();
[3764]71      } else
[2973]72        UpdateData();
[3362]73      SetEnabledStateOfControls();
[2973]74    }
[3350]75    protected override void OnReadOnlyChanged() {
76      base.OnReadOnlyChanged();
[3362]77      SetEnabledStateOfControls();
[3350]78    }
[3362]79    private void SetEnabledStateOfControls() {
[3454]80      lengthTextBox.Enabled = Content != null;
81      dataGridView.Enabled = Content != null;
82      lengthTextBox.ReadOnly = ReadOnly;
83      dataGridView.ReadOnly = ReadOnly;
[3350]84    }
[2973]85
86    private void UpdateData() {
[2974]87      lengthTextBox.Text = Content.Length.ToString();
88      lengthTextBox.Enabled = true;
[2973]89      dataGridView.Rows.Clear();
90      dataGridView.Columns.Clear();
[2974]91      if (Content.Length > 0) {
[2973]92        dataGridView.ColumnCount++;
93        dataGridView.Columns[0].FillWeight = float.Epsilon;  // sum of all fill weights must not be larger than 65535
[2974]94        dataGridView.RowCount = Content.Length;
95        for (int i = 0; i < Content.Length; i++) {
[2973]96          dataGridView.Rows[i].Cells[0].Value = Content.GetValue(i);
97        }
98        dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
99      }
100      dataGridView.Enabled = true;
101    }
102
103    private void Content_ItemChanged(object sender, EventArgs<int> e) {
104      if (InvokeRequired)
105        Invoke(new EventHandler<EventArgs<int>>(Content_ItemChanged), sender, e);
106      else {
107        dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(e.Value);
108        Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
109        dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
110      }
111    }
112    private void Content_Reset(object sender, EventArgs e) {
113      if (InvokeRequired)
114        Invoke(new EventHandler(Content_Reset), sender, e);
115      else
116        UpdateData();
117    }
118
119    #region TextBox Events
[2974]120    private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
[2973]121      int i = 0;
[2974]122      if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
[2973]123        e.Cancel = true;
[2974]124        errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
125        lengthTextBox.SelectAll();
[2973]126      }
127    }
[2974]128    private void lengthTextBox_Validated(object sender, EventArgs e) {
[3430]129      if (!Content.ReadOnly) Content.Length = int.Parse(lengthTextBox.Text);
[2974]130      errorProvider.SetError(lengthTextBox, string.Empty);
[2973]131    }
[2974]132    private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
[2973]133      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
[2974]134        lengthLabel.Focus();  // set focus on label to validate data
[2973]135      if (e.KeyCode == Keys.Escape) {
[2974]136        lengthTextBox.Text = Content.Length.ToString();
137        lengthLabel.Focus();  // set focus on label to validate data
[2973]138      }
139    }
140    #endregion
141
142    #region DataGridView Events
143    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
144      string errorMessage;
145      if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
146        e.Cancel = true;
147        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
148      }
149    }
150    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
151      string value = e.Value.ToString();
152      e.ParsingApplied = Content.SetValue(value, e.RowIndex);
153      if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex);
154    }
155    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
156      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.