title: Windows Forms: "System.ArgumentException: Parameter is not valid." coming from System stack
body: <p>The unhandled exception handler on one of our in-house WinForms apps is regularly reporting this error, and variations of the same, from use in production (we have it configured to email us whenever an unhandled exception occurs). However it's not reproducible and occurs at random intervals on different users machines, which are all XP SP3. </p>
  
  <p>It appears to be related to Masked text boxes in datagrids, but it only seems to appear in the same three controls, out of dozens on many screens. These controls don't have any Font specified.</p>
  
  <pre><code>System.ArgumentException: Parameter is not valid.
     at System.Drawing.Font.GetHeight(Graphics graphics)
     at System.Drawing.Font.GetHeight()
     at System.Drawing.Font.get_Height()
     at System.Windows.Forms.Control.set_Font(Font value)
     at System.Windows.Forms.DataGridViewComboBoxEditingControl.ApplyCellStyleToEditingControl(DataridViewCellStyle dataGridViewCellStyle)
     at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll)
     at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message&amp; m)
     at System.Windows.Forms.Control.ProcessKeyMessage(Message&amp; m)
     at System.Windows.Forms.Control.WmKeyChar(Message&amp; m)
     at System.Windows.Forms.Control.WndProc(Message&amp; m)
     at System.Windows.Forms.DataGridView.WndProc(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam,
  IntPtr lparam)
  </code></pre>
  
  <p>The thing that really has me stumped is that the stack trace is completely within the System namespace, so there's something wrong with a font, but we have no idea what it is. "Parameter is not valid" doesn't give a lot of information about what is invalid about the parameter (this is from the underlying GDI library).</p>
  
  <p>We also get a similar exception that travels through one of our classes, which has allowed me to trap the error:</p>
  
  <pre><code>System.ArgumentException: Parameter is not valid.
     at System.Drawing.Font.GetHeight(Graphics graphics)
     at System.Drawing.Font.GetHeight()
     at System.Drawing.Font.get_Height()
     at System.Windows.Forms.Control.set_Font(Font value)
     at MyApp.MaskedTextBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyle
  dataGridViewCellStyle)
  </code></pre>
  
  <p>The code in question is merely this:</p>
  
  <pre><code>public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle){
    this.Font = dataGridViewCellStyle.Font; 
    // set other things
  }
  </code></pre>
  
  <p>I wrapped that line in a try/catch block and called a ToString() on the passed Font, and got the following: "[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,GdiCharSet=1, GdiVerticalFont=False]", so I can't figure out what's going on. Any ideas?</p>