title: C#: data binding a single, custom class to form controls (checkbox?)
body: <p>I'm writing a desktop application in Visual Studio 2008 / C#</p>
  
  <p>I finished (mostly) writing an engine which generates a working schedule for the week for a small company; a form of a course-scheduling problem</p>
  
  <p>Right now I'm designing a form in which the user can determine the initial parameters, or criteria, for the engine to adhere to (as some settings are optional)</p>
  
  <p>I've got a class named EngineParameters, which holds all of those settings.</p>
  
  <p>For the purpose of databinding, I created a <strong>bndEngineParameters</strong> class, which encapsulates all the relevant fields with getters and setters</p>
  
  <pre><code>public class bndEngineParameters
      {
          private engineParameters _parameters;
  
          public bndEngineParameters(engineParameters ep)
          {
              this._parameters = ep;                
          }
  
          public bool avoidGrouping
          {
              get { return _parameters.avoidGrouping; }
              set { _parameters.avoidGrouping = value; }
          }
  
          public bool avoidWeekends
          {
              get { return _parameters.avoidWeekends; }
              set { _parameters.avoidWeekends = value; }
  
          }
  
          public bool keyFlow
          {
              get { return _parameters.keyFlow; }
              set { _parameters.keyFlow = value; }
          }
  
          public bool keyFlowAssistants
          {
              get { return _parameters.keyFlowAssistants; }
              set { _parameters.keyFlowAssistants = value; }
          }        
      }
  </code></pre>
  
  <p>It's not complete - there will be int values (maximum number of hours one can work etc.); I want those bool values to be bound to checkboxes on my form</p>
  
  <p>And it's at that trivial task where I surprisingly ran into problems</p>
  
  <p>Using "Add New DataSource" wizard, I created a binding source</p>
  
  <pre><code>private System.Windows.Forms.BindingSource bndEngineParametersBindingSource;
  </code></pre>
  
  <p>I then bound the <strong>Checked</strong> property of my Checkbox to the respective property of my binding source:</p>
  
  <p><a href="https://i.stack.imgur.com/DMpW7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DMpW7.jpg" alt="alt text"></a>
  </p>
  
  <p>I implemented a local variable <strong>boundParameters</strong> so that I get an access to the parameters set by the user</p>
  
  <pre><code>  public partial class formGenerateRota : Form
      {
  
          public bndEngineParameters boundParameters;
  
          // (...)
  
          public formGenerateRota()
          {
              InitializeComponent();
          }        
  
          private void formGenerateRota_Load(object sender, EventArgs e)
          {
              boundParameters = new bndEngineParameters(new engineParameters());
              bndEngineParametersBindingSource.Add(boundParameters);            
          }
  
          // (...)
  }
  </code></pre>
  
  <p>And what? Nothing happens. There is an bndEngineParameters object under <strong>bndEngineParametersBindingSource.Current</strong> (in run-time of course), but the <strong>avoidWeekends</strong> value never changes (when I check the checkbox on and off), and the <strong>bndEngineParametersBindingSource_CurrentItemChanged</strong> event is never fired</p>
  
  <p>What's wrong?</p>
  
  <hr>
  
  <p>SORRY! it does change, but only after the checkbox loses focus (after validation).</p>
  
  <p>I'm stupid sometimes</p>
  
  <p>If I'm doing something wrong anyway (I'm not any good with data binding), I'd very much appreciate if you point it out of course!</p>