.NET form - resize location

I am working on an application that shows its forms as a series of modals stacked on top of each other. All shapes are placed in the center of the screen. When the user moves the form around the screen, only the top one moves, the others stay in the center and cannot move as they are locked by the topmost dialog.

I'm trying to code some formatting code that moves all forms to a new location, as well as code that will open new forms at the current off-center location.

But something strange happens when doing the latter - opening a new shape at an off-center position using the location of some other shape (bottommost shape). If I set the Location property on the form programmatically, it resizes - the form gets smaller. The Size property changes from (240, 320), as set in the designer's GUI, to the smaller ClientSize (234, 294), which is present in the code generated by the designer. I only found one post on the net referencing this issue, but there are no answers to it. If I leave only the Location property, the form displays correctly with its original size.

Form's StartPosition is set to Manual, FormBorderStyle is FixedSingle, AutoScaleMode is either DPI or Font (I don't know how to set this parameter).

Any ideas? Thank.

+2


source to share


1 answer


I will answer my own question. The behavior seems to have to do with setting the Location property while the form is not yet rendered by the OS, that is, it does not yet have a generated handle and its IsHandleCreated is False. I was debugging the .NET code itself, and there was a Debug.Assert call with "Don't use this until CreateHandle" or something like that as an assert parameter somewhere in the call stack around the code, which was problematic. This gave me the first hint.

Putting code in an event handler for the Form.Load event resolves the issue. At the time Load fires, the handle has already been created and everything is working.



Now that I know the reason, I have searched the net a bit for initializing the constructor handler and loading events, and the relevant discussions suggest that you need to defer processing of controls until loaded. Which kind makes sense since .NET controls are wrappers around objects outside the OS (and they must be created to be used), but at the same time doesn't make sense because the constructor code seems to have access to the controls before the handle is created just fine. If that makes sense.

TL; DR: open access control and use until the Form.Load event occurs.

+3


source







All Articles