Windows Forms

Q:  In Windows Forms, how do I reliably scroll a text box to the end?

A:  Normally calling SelectionStart(cntrol.TextLength) followed by ScrollToCaret.  AppendText is good for general use, but will not work if the control is hidden.  Courtesy this source.


Q:  How do I implement DragMove to move a Windows Forms form when a the user drags a control?

A:  This requires three event handlers:  MouseDown, MouseMove, and MouseUp, plus 2 instance members:  moveMode and moveStartLocation
   MouseDown:  moveMode = true
   MouseUp: moveMode = false
   MouseMove:  if (moveMode) {Location = new System.Drawing.Point((this.Location.X - moveStartLocation.X) + e.X, (this.Location.Y - moveStartLocation.Y) + e.Y); Update();}


Q:  In Windows Forms, when my ComboBox selection changes, the binding is not updated until focus leaves the field.  How do I make the binding update immediately?

A:  This is by design in Forms; there is no way to reconfigure when the binding is updated.  To resolve this, install a handler on the SelectedValueChanged event that updates the bindings:
foreach (Binding currBinding in ((System.Windows.Forms.Control)sender).DataBindings)
{
    currBinding.WriteValue();
    currBinding.ReadValue();
}
Yes, this is kinda yuk, but is due to binding limitations in Windows Forms - it is not as sophisticated as WPF.

Q:  In Windows Forms, how do I use an object collection that requires a formatter method?

A:  Add a .Format handler and set the ListControlConvertEventArgs .Value member to the string to display.  On call, this member is also the object selected.  For example:
private void guiSelectedDevice_Format(object sender, System.Windows.Forms.ListControlConvertEventArgs e)
    e.Value = nameConverter.Convert(e.ListItem, typeof(string), null, null);
Comments