GUI/ParameterForm.cs
author moel.mich
Sat, 27 Feb 2010 20:08:13 +0000
changeset 64 15181001ee61
child 86 b4f0f206173d
permissions -rw-r--r--
Added sub-hardware support and basic enumeration for mainboards.
moel@63
     1
using System;
moel@63
     2
using System.Collections.Generic;
moel@63
     3
using System.ComponentModel;
moel@63
     4
using System.Text;
moel@63
     5
using System.Windows.Forms;
moel@63
     6
using OpenHardwareMonitor.Hardware;
moel@63
     7
using OpenHardwareMonitor.Utilities;
moel@63
     8
moel@63
     9
namespace OpenHardwareMonitor.GUI {
moel@63
    10
  public partial class ParameterForm : Form {
moel@63
    11
moel@63
    12
    private IReadOnlyArray<IParameter> parameters;
moel@63
    13
    private BindingList<ParameterRow> parameterRows;
moel@63
    14
moel@63
    15
    public ParameterForm() {
moel@63
    16
      InitializeComponent();
moel@63
    17
    }
moel@63
    18
    
moel@63
    19
    public IReadOnlyArray<IParameter> Parameters {
moel@63
    20
      get {
moel@63
    21
        return parameters;
moel@63
    22
      }
moel@63
    23
      set {
moel@63
    24
        parameters = value;
moel@63
    25
        parameterRows = new BindingList<ParameterRow>();
moel@63
    26
        foreach (IParameter parameter in parameters)
moel@63
    27
          parameterRows.Add(new ParameterRow(parameter));
moel@63
    28
        bindingSource.DataSource = parameterRows;
moel@63
    29
      }
moel@63
    30
    }
moel@63
    31
moel@63
    32
    private class ParameterRow : INotifyPropertyChanged {
moel@63
    33
      public IParameter parameter;
moel@63
    34
      private float value;
moel@63
    35
      public bool isDefault;
moel@63
    36
moel@63
    37
      public event PropertyChangedEventHandler PropertyChanged;
moel@63
    38
moel@63
    39
      private void NotifyPropertyChanged(String propertyName) {
moel@63
    40
        if (PropertyChanged != null) {
moel@63
    41
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
moel@63
    42
        }
moel@63
    43
      }
moel@63
    44
moel@63
    45
      public ParameterRow(IParameter parameter){
moel@63
    46
        this.parameter = parameter;
moel@63
    47
        this.value = parameter.Value;
moel@63
    48
        this.isDefault = parameter.IsDefault;
moel@63
    49
      }
moel@63
    50
moel@63
    51
      public string Name {
moel@63
    52
        get { return parameter.Name; }
moel@63
    53
      }
moel@63
    54
moel@63
    55
      public float Value {
moel@63
    56
        get { return value; }
moel@63
    57
        set {            
moel@63
    58
          this.isDefault = false;
moel@63
    59
          this.value = value;
moel@63
    60
          NotifyPropertyChanged("Default");
moel@63
    61
          NotifyPropertyChanged("Value");
moel@63
    62
        }
moel@63
    63
      }
moel@63
    64
moel@63
    65
      public bool Default {
moel@63
    66
        get { return isDefault; }
moel@63
    67
        set {
moel@63
    68
          isDefault = value;
moel@63
    69
          if (value)
moel@63
    70
            this.value = parameter.DefaultValue;
moel@63
    71
          NotifyPropertyChanged("Default");
moel@63
    72
          NotifyPropertyChanged("Value");
moel@63
    73
        }
moel@63
    74
      }
moel@63
    75
    }
moel@63
    76
moel@63
    77
    private void dataGridView_RowEnter(object sender, 
moel@63
    78
      DataGridViewCellEventArgs e) 
moel@63
    79
    {
moel@63
    80
      if (e.RowIndex >= 0 && e.RowIndex < parameters.Length)
moel@63
    81
        descriptionLabel.Text = parameters[e.RowIndex].Description;
moel@63
    82
      else
moel@63
    83
        descriptionLabel.Text = "";
moel@63
    84
    }
moel@63
    85
moel@63
    86
    private void dataGridView_CellValidating(object sender, 
moel@63
    87
      DataGridViewCellValidatingEventArgs e) 
moel@63
    88
    {
moel@63
    89
      float value;
moel@63
    90
      if (e.ColumnIndex == 2 &&
moel@63
    91
        !float.TryParse(e.FormattedValue.ToString(), out value)) {
moel@63
    92
        dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = 
moel@63
    93
          "Invalid value";
moel@63
    94
        e.Cancel = true;
moel@63
    95
      }
moel@63
    96
    }
moel@63
    97
moel@63
    98
    private void dataGridView_CellEndEdit(object sender,
moel@63
    99
      DataGridViewCellEventArgs e) {
moel@63
   100
      dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = "";
moel@63
   101
    }
moel@63
   102
moel@63
   103
    private void okButton_Click(object sender, EventArgs e) {
moel@63
   104
      foreach (ParameterRow row in parameterRows) {
moel@63
   105
        if (row.Default) {
moel@63
   106
          row.parameter.IsDefault = true;
moel@63
   107
        } else {
moel@63
   108
          row.parameter.Value = row.Value;
moel@63
   109
        }
moel@63
   110
      }
moel@63
   111
    }
moel@63
   112
moel@63
   113
    private void dataGridView_CurrentCellDirtyStateChanged(object sender, 
moel@63
   114
      EventArgs e) {
moel@63
   115
      if (dataGridView.CurrentCell is DataGridViewCheckBoxCell ||
moel@63
   116
        dataGridView.CurrentCell is DataGridViewComboBoxCell) 
moel@63
   117
      {
moel@63
   118
        dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
moel@63
   119
      }
moel@63
   120
    }
moel@63
   121
  }
moel@63
   122
}