title: c# control value problem at form load
body: <p>like coffee for an insomniac, i have another question to post.</p>
  
  <p>in a form's load event, i create and initialize controls based on attributes of a class passed as an argument into the form's constructor. the comboboxes and checkboxes fail early (method "initializeControls()") into the form creation but not later (method "resetData()"). i fail to understand as both are called during event "SomeForm_Load(sender, e)". did i do something as basic as use the wrong event? as stated in my earlier question posts, i'm new to c#. any guidance appreciated. feel free to tell me if i use far too much reflection, too, :D.</p>
  
  <p>the pseudo code, for the gist of my problem:</p>
  
  <pre><code>public class SomeForm : Form
  {
    #region fields
    private int _id { get; set; }
    private int _id2 { get; set; }
    #endregion fields
    ...
    public SomeForm()
    {
      InitializeComponent();
    }
    public SomeForm(int id, int id2) : this()
    {
      _id = id;
      _id2 = id2;
    }
  
    #region init
    private void SomeForm_Load(object sender, EventArgs e)
    {
      method1(); // fails
      method2(); // works
    }
    private void method1()
    {
      var ds = new []{1,2};
      CheckBox cb = new CheckBox();
      cb.DataSource = ds;
      cb.SelectedValue = _id; // &lt;== this is the problem. these two
      this.Controls.Add(cb);  // &lt;== steps should be switched.
    }
    private void method2()
    {
      cb.SelectedValue = _id2;
    }
    ...
  }
  </code></pre>
  
  <p>the real code:</p>
  
  <pre><code>public class SomeForm : Form
  {
    ...
    #region fields
    ...
    private MultiState.Update _child { get; set; }
    protected object data { get; set; }
    private Type _masterType { get; set; }
    private List&lt;PropertyInfo&gt; _mpks = new List&lt;PropertyInfo&gt;();
    private User _user { get; set; }
    #endregion fields
    ...
    public SomeForm()
    {
      InitializeComponent();
    }
    public SomeForm(User user, Type amaster, object aobject, Dictionary&lt;string, object&gt; astate) : this()
    {
      data = aobject;
      _masterType = amaster;
      dataState = astate;
      _user = user;
    }
  
    #region init
    private void SomeForm_Load(object sender, EventArgs e)
    {
      initialize();
    }
    private void initialize()
    {
      tsData.Visible = false;
      if (data != null)
      {
        initializeControls(); // FAILS!! without exception
        dataId = DataService.GetPrimaryKeyValue(data);
        resetData(); // SUCCEEDS
      }
    }
    private void initializeControls()
    {
      ...
      ComboBox cb = new ComboBox();
      cb.Enabled = fdEnabled; // correctly read from linq datacontext custom attribute
      cb.FormattingEnabled = true;
      cb.Location = new Point(x, y);
      cb.Name = _CP_COMBOBOX + pi.Name;
      cb.Size = new Size(_WIDTH_CODE, _HEIGHT_SINGLE);
      cb.TabIndex = i;
      cb.TabStop = true;
      cb.Leave += new EventHandler(this.ctlEdit_Leave);
  
      // set drop-down
      cb.DataSource = domain; // correctly populated from service class
      cb.ValueMember = "Id";
      cb.DisplayMember = "Label";
  
      // set default
      if (fdDefault != null)
      {
  </code></pre>
  
  <p>THE ASSIGNMENT BELOW FAILS!! the desired value calculates correctly (wth, when uncommented), but it is simply ignored without any exception.</p>
  
  <pre><code>      //object wth = GetDomainKeyAsIdentifierSafe(domain, fdDefault, _user); // correctly parsed
        cb.SelectedValue = GetDomainKeyAsIdentifierSafe(domain, fdDefault, _user); // HUGE FAIL!!
      }
      ...
    }
    #endregion init
    ...
    #region persistence
    ...
    protected string resetData()
    {
      string rc = "";
      if (!isDataNew()) // this form only modal
      {
        //resetDataState();
        #region bind-object-vs-set
        foreach (Control control in this.Controls)
        {
          try
          {
            if (control.Name.StartsWith(_CP_TEXTBOX))
            {
              MethodInfo mi = data.GetType().GetMethod(DataService.LINQ_GET + control.Name.Substring(_CP_TEXTBOX.Length));
              object value = mi.Invoke(data, null);
              control.Text = value == null ? "" : value.ToString();
            }
            else if (control.Name.StartsWith(_CP_CHECKBOX))
            {
              #region bind-object-vs-set-cbx
              bool ck = false;
              string scontrol = control.Name.Substring(_CP_CHECKBOX.Length);
              MethodInfo mi = data.GetType().GetMethod(DataService.LINQ_GET + scontrol);
              object value = mi.Invoke(data, null);
              if (value != null)
              {
                if (value.GetType() == typeof(bool))
                {
                  ck = (bool)value;
                }
                else if (value.GetType() == typeof(Nullable&lt;bool&gt;))
                {
                  Nullable&lt;bool&gt; nvalue = (Nullable&lt;bool&gt;)value;
                  if (nvalue.HasValue)
                    ck = nvalue.Value;
                  else
                    ck = AtsService.GetDefaultBoolean(data.GetType(), scontrol);
                }
              }
              CheckBox cbx = (CheckBox)control;
              cbx.Checked = ck;
              #endregion bind-object-vs-set-cbx
            }
            else if (control.Name.StartsWith(_CP_COMBOBOX))
            {
              MethodInfo mi = data.GetType().GetMethod(DataService.LINQ_GET + control.Name.Substring(_CP_COMBOBOX.Length));
              object value = mi.Invoke(data, null);
              ComboBox cb = (ComboBox)control;
  </code></pre>
  
  <p>THE ASSIGNMENT BELOW MYSTERIOUSLY WORKS!!</p>
  
  <pre><code>            cb.SelectedValue = value == null ? FormService.NOSELECTION_ID : value; // but this one works!!
            }
          }
        }
      }
    }
    ...
    #endregion persistence
  }
  </code></pre>
  
  <p>edit: added bigger flags at success and fail points.</p>