Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms controls get dislocated everytime the form is viewed

Tags:

I'm stumped by some bizarre behavior in Visual Studio's forms editor. I have seen this on a couple of different forms in my application. Each time I open the form in Visual Studio's layout editor some controls will be in a different location than when I left them. Typically some buttons move up just a little bit from the lower right corner. But its not just buttons, in one case its a container panel that moves. I have to reposition them then save and close the form. I've confirmed that it is the layout editor actually changing the Location property when the form is opened because if I save and close the form with the buttons in the correct position they will be correct at runtime.

This is not a problem with the Anchor or Dock properties not being set correctly. The editor is actually changing the Location property of my control(s). I've looked at the .designer.cs file and I do not see anything unusual. I've tried deleting and recreating these controls but the problem persists.

Any ideas what I can do?

Its not a show stopper I just have to be very careful to fix the controls manually every time I open it in the winforms layout editor.

Edit: Visual Studio will actually checkout the file automatically to set the Location to what it stubbornly thinks it should be.

like image 548
Brian Ensink Avatar asked Aug 31 '09 15:08

Brian Ensink


2 Answers

I found the answer to this problem, but it just look like a bug for me. It has never been solved since 2003 !

In short : Visual Inheritance does not work well with Anchoring.

Complete answer here : http://weblogs.asp.net/rweigelt/archive/2003/09/24/28984.aspx

like image 92
Jalil Avatar answered Oct 02 '22 12:10

Jalil


The WinForms editor is a WYSIWYG which requires the editor to actually execute the layout code in order to show you exactly what the form will look like. While being extremely convenient, there are a number of chicken and egg problems that begin to wreak havoc on your editor.

A common issue is that of sizing. Sometimes control properties are ordered incorrectly (and, being auto-generated, you cannot fix this). The result is that some needed value is not set until after the property which needs it. A famous example is the SplitContainer and the MinSize of Panel2 (see http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/ee6abc76-f35a-41a4-a1ff-5be942ae3425). It's possible that you are experiencing a similar root issue, but the result is that the location of your controls are changing.

I would examine the order of your properties in the Designer and try to determine if that might be the root of the issue. If it is, you may need to set some property at runtime. In general though, there is a rarely a true "fix" - the resolution is more often than not a "workaround".

These kinds of issues were part of the motivation for creating WPF. The declarative nature of XAML helps to prevent these sorts of occurrences while still providing the WYSIWYG feel.

like image 23
JDB Avatar answered Oct 02 '22 12:10

JDB