Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs @ 12771

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

added null checks for content in CellValidatingEvent of StringConvertibleArray and -MatrixView (ticket #968)

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