Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

c# - Why do winform controls move on their own when I save the form

I am having an issue with button controls moving when I close my form in the editor in VS 2012. I am working in C# exclusively. This is diving me nuts as I have to reposition the controls every time I open the designer.

Form Size 995, 625

Button             Location
------             --------
Save button        617, 575
Delete button      701, 575
Clear button       785, 575
Cancel button      869, 575

Buttons are anchored Bottom,Right

Form Maximum size is 0,0

There is a TabControl above these buttons. I have had this issue before and I am pretty sure it involved a form with a TabControl as well.

There are a couple layers of inheritance below this form, but both are defined smaller. The buttons in play here are inherited from the form just below this one. On that form they are anchored Top,Left so I would think my controls would be moving up and left if they were being effected by that forms positioning. They consistently move down and right.

Steps that reproduce.

  • Position the controls as above. (No further edits)
  • Save the form. (no visible change yet)
  • Close the form.
  • Reopen the form and the controls are located as follows:
 Button              Location
 Save button         852, 664   (off visible form)
 Delete button       936, 631   (off visible form)
 Clear button       1020, 630   (off visible form)
 Cancel button      1104, 664   (off visible form)

These locations vary. Sometimes they are still on the form, or partially on-form just in a lower position and no longer aligned. They are always lower and to the right as if they were being effected by a changing form size during the closing process.

If I make the form larger in the designer, they just keep moving down and right staying off-form.

I have resorted to positioning them programmatically for now, but would rather understand what is happening so as to prevent it in the future.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This happens often if you have a base form (or user control) where you set some anchors for the controls and in derived designer your form has a different size.

A possible solution is to forget anchoring and to use docking instead. You can achieve everything by using the Dock, Margin and Padding properties; however, it can be strange for the first time that you might need to use many invisible panels (for example when you want to use two lines of controls below each other). This is a similar approach as using Stack/Dock panels in WPF.

enter image description here

Additionally, you can play with the TableLayoutPanel as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...