Mojo Hand Mojo Hand - 6 months ago 36
C# Question

Control anchor property not working when form starts maximized

Here's my problem: I have a winform with controls on it. Many of these controls have their Anchor property set to Top|Right. The size of the form in the designer is set to 1680x1050. If my resolution is set to 1680x1050 then it always comes up correctly.

If I change to a smaller resolution(say 1600x900) and the form is set to open as Maximized, then none of my controls move themselves to maintain their distance from the right edge. The controls on the right edge are all sticking out off the form a little. But, if I then unmaximize the window, and I can resize the window and all the controls will maintain their current, incorrect distance from the right edge.

If I set the form to start as normal(not maximized) then it opens up with all the controls in the right place, and everything stays in the right place if I resize the form.

This has been a very frustrating problem. Do any of you kind souls have advice for me?

Answer Source

Id recommend just using my proposed solution of:

 theForm.WindowState = FormWindowState.Maximized;

My guess is that the property "Maximized" that is set during the initialization call of the form and may be causing the problems. (It's hard to say without seeing the project code). The Load even gets called after some of the more important events, so if there is some sort of existing problem with that property, it is avoided using the FormWindowState.Maximized code.

If you wish to post the actual code of what you think may be causing the problems, I'll edit this answer to help.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download