1

I have a C#.NET winforms project, and some controls are moving in design view whenever I build the project. Its only some of the controls (a panel with a label and datagridview in it, a button, a link button and a label) are all moving up on each build.

Has anyone seen this before or know how to fix it?

MAW74656
  • 3,449
  • 21
  • 71
  • 118
  • Do you have any add-ins installed? Can you copy the form into a new C# project and duplicate the behavior? – UnhandledExcepSean Mar 21 '12 at 19:46
  • @SpectralGhost -Some add-ins. Autocode v4.0 (disabled), Devart SQL COmplete (disabled), rockscroll (disabled), Smart Paster 2008 (enabled), TestDriven.Net 3.0 Personal (disabled), TestDriven.Net Reflector (enabled). – MAW74656 Mar 21 '12 at 19:49
  • Are the controls standard WinForms or 3rd party? Can you post reproducible demo code somewhere? – Phil Mar 22 '12 at 17:16
  • @Phil -See original question, I state what the controls are. – MAW74656 Mar 22 '12 at 18:47
  • @Phil -Copying the controls into a new form does not show the moving behavior. – MAW74656 Mar 22 '12 at 19:16
  • Does that mean you've fixed your problem? – Phil Mar 22 '12 at 19:20
  • @Phil -Nope. But i can't recreate it in a new form. So I have no idea what else to try. – MAW74656 Mar 22 '12 at 20:31
  • So if you create a copy of your form doesn't that fix it? I assume you have VS2008 SP1? Can you move to 2010? – Phil Mar 22 '12 at 20:35
  • @Phil -I suppose I could get the express edition, but that's not really solving the problem... – MAW74656 Mar 22 '12 at 21:09
  • Just so I understand your workflow... do you position the controls on the form using the Designer, then build, and the controls are moved to a different position? – Steve Wong Mar 23 '12 at 17:53
  • @SteveWong -Correct. The controls are NOT added dynamically or anything like that. – MAW74656 Mar 23 '12 at 18:18
  • Is the .Location property of the moving controls data bound to a configuration value? – Steve Wong Mar 23 '12 at 18:29
  • Try this: 1) position one label correctly in the Designer (say, "labelName"). 2) open YourForm.designer.cs, 3) find this.labelName.Location = (x, y). 4) build, 5) check this.labelName.Location = (x, y) again. Are they the same? Now open the form in the Designer again. – Steve Wong Mar 23 '12 at 18:32
  • I'm sure you have already checked, but Anchor/Dock property? – Steve Mar 24 '12 at 21:22
  • Ok my guess. Are you sure that you do NOT have the Build key command (ie Ctrl + Shift + B) mapped to another command or more than one command (Tools > Options > Environment > Keyboard)? – Jeremy Thompson Mar 26 '12 at 05:29
  • What happens if you lock the controls (Locked = True) do they still move then? – Jeremy Thompson Mar 26 '12 at 05:32
  • @JeremyThompson -Locked has no effect on the issue. – MAW74656 Mar 26 '12 at 14:20
  • @JeremyThompson -Don't have any custom key mapping. Also, usually don't just build, normally debug, then when I build use the menu. – MAW74656 Mar 26 '12 at 15:55
  • @Steve -The controls in question do NOT all have the same dock/anchor properties, and some of them do have the same values as controls which are not moving. – MAW74656 Mar 26 '12 at 15:57
  • @SteveWong -Control size/location is not bound to anything I know of. Just positioned by drag and drop in the designer. – MAW74656 Mar 26 '12 at 16:02
  • Do the controls stretch/shrink, or randomly move? Could be related to AutoScaleDimensions from Hans comment here: http://stackoverflow.com/questions/9872251/build-changes-button-size-property – Steve Wong Mar 26 '12 at 18:42
  • @SteveWong -They move only. Ok, my file show `this.AutoScaleDImensions = new System.Drawing.SizeF(8F, 16F)`, but a fresh project shows `...(6F, 13F)`. So... what does this mean? – MAW74656 Mar 26 '12 at 19:16
  • Maybe we're getting closer. What's the AutoScaleMode of the form? Maybe try setting that to Inherit (or None for a test)? – Steve Wong Mar 26 '12 at 19:34
  • @SteveWong -Its set to `Font`. – MAW74656 Mar 26 '12 at 19:58

1 Answers1

1

I think it is because of the AutoScaleDimensions. My guess is that your form was originally created on another machine.

Per MSDN.

"The AutoScaleDimensions property represents the DPI or font setting of the screen that the control was scaled to or designed for. Specifically, at design time this property will be set by the Windows Forms designer to the value your monitor is currently using. Then, when the form loads at run time, if the CurrentAutoScaleDimensions property is different from the AutoScaleDimensions, the PerformAutoScale method will be called to perform scaling of the control and all of its children. Afterwards, AutoScaleDimensions will be updated to reflect the new scaling size."

My guess is that for odd some reason when you build you project property (maybe some others) gets adjusted, but not on design time. I think about few possible reasons:

  • You work on multiple monitors and/or there is some odd stuff with your adapter.

  • There is some problem with auto-generated designer file. Maybe it
    edited manually somehow.

To fix I propose to do something I would do:

  • Recreate form from scratch if possible, by copy-pasting bits one-by-one.

  • If not take some merging tool and insert fresh form properties.

Also here is another interesting question on AutoScaleDimentions.

Community
  • 1
  • 1
Andriy Buday
  • 1,959
  • 1
  • 17
  • 40