I am looking for a work around for what seems like a bug in WinForms or Win32. I have a method for position a form before it is shown, but when I show it the height of forms with ControlBox set to false is reduced, cutting off some controls.
The steps to reproduce this are to make a form in the designer with ControlBox set to false (removes the close/minimize/maximize buttons) and Text is a non-empty string (the titlebar is not removed) and then in code after creating the form set the Location property to something before calling Show/ShowDialog.
The problem seems to be that the height is being changed to what it would be if Text was empty (no titlebar). If I edit *.designer.cs manually and set the value of ControlBox after setting Text the problem is gone. But manually changing the generated code does not seem practical or good for maintenance.
I’ve tried to set ControlBox to true in code before changing the location, I tried saving the height before setting location (the height returned is the shorter value) and I’ve tried invalidating the form before setting Location to make it pickup the fact that Text is filled out. None of these seem to correct the bad height value. In Reflector my attempts to trace what is going on quickly disappear into public static extern bool SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, int x, int y, int cx, int cy, int flags) after setting some form style values – I assuming some magic happens here to adjust the form size.
// Method is called after dialog is created but before Show() is called
public static void PlaceDialog(Form dialog)
{
dialog.Location = new Point(1, 1);
}
Any ideas on a work around I could use in PlaceDialog to fix this? The only one I’ve found to work so far is very ugly and breaks a number of forms events:
// Method is called after dialog is created but before Show() is called
public static void PlaceDialog(Form dialog)
{
if (dialog.ControlBox == false)
{
dialog.ControlBox = true;
dialog.Show();
dialog.ControlBox = false;
dialog.Hide();
}
dialog.Location = new Point(1, 1);
}
Try changing or adding the following: