UserControl Dock/Anchor not working - c#

Here's a complete simple case:
I created a UserControl. And added a Button on it Anchored top-right.
I Dock.Filled the UserControl in Form1.
The Button is not in the correct place when executing (though it is in the Designer).
Screenshots:
UserControl Designer:
Form Designer:
Executing:
Executing, resized. As you can see, that doesn't help:
I tried changing my computer's text size from 125% to 100% but that didn't help either. Windows 10, VS 2017, .Net 4.7.1. I also tried setting the UserControl's Dock in its Load and ParentChanged event handlers, and its constructor. No change.
So how do I get the button to stay anchored?
EDIT (it seems to be somehow related to text size being different than 100%)
After a lot of fiddling I see now that after I change to 100% size text (In the Settings app under System-Display. Including logging off and on), clean and rebuild the solution, and then change back to 125% it works for some time. I can't find the exact limit for that.

#ispiro, could you please try to disable DPI awareness on your VisualStudio process and see if it helps. Here is a blog post that specifies the registry key to set:
https://code4ward.net/2016/11/29/visual-studio-winforms-designer-on-highdpi/

Related

Show C# WinForm on different screens

On my workstation I have developed a WinForm application. With panels, buttons and dropdowns.
After that I applied the application to my laptop.
On my laptop the presentation of the application was not correct. The elements overlap and the buttons are warped.
Can you tell me how to handle this?
I apologize for my english. This text was written with Google translator.
It is a bit difficult to tell from the distance, but I guess the window uses a different size on the laptop and the controls' docking and anchoring are not configured to do what you want. I suggest this and this tutorial on how to configure the properties; this is preferrably done in the designer instead of code for a fixed layout.
I've seen this behavior running Windows with a "display size" setting greater than 100%. Try resetting this value in your display settings:
Make sure to develop your forms on a computer with the system DPI setting set to 100%. Visual Studio will automatically convert your form coordinates depending on this system setting (it's a bug according to me). A quick way to fix a form is to do the following:
Make sure the system DPI setting is set to 100%.
Move a button in the form one pixel to the left. Then move it back again.
Save. Compile. Run.
1.] Either use Anchor or Dock
2.] Or try to fix the minimum and maximum size of your form and disable maximize button

in Visual studio 2015 the designer change property, like size, of a controls in a win form

i have a windows form with a lot of controls like datagrid, panel, groupbox, tab control ecc... In VS2015 every time i open the form in design mode, the designer made some little change at size, point or location properties of some control as shown in the screenshot from the diff of svn.
screenshot of a diff
and i did not change anything only save and re-open in designer. When i do this, the designer also change the position of some block of code.
Usually i work with the *.designer.cs in read only mode, but when i have to make changes to the UI it is not possible to work in this way... so any one knows how to solve this issue?
Thanks a lot
Set Form's AutoScaleMode property to None.
Or you can use some panels to organize your controls on form, like create aTableLayoutPanel and add rows and columns and dock controls in each cell.

Control positions in Runtime not consistent with designer

Let me explain put it in just a few simplest lines.
I made a new C#/WPF application project in VS 2013 ultimate (x64).
On MainWindow I insert (for example) a button at bottom-right, actually anywhere..
See image below, but the position of the buttons at runtime does not match what it looks like when I am designing
As you can see from the marked up spots in the 2 images, the controls are mispositioned.
I tried to change some XAML attributes, setting min/max sizes, and so on and on. I am running a fresh-install Windows 7 Ultimate (x64) OS, with "Windows 7" aero theme.
I played around MainWindow/Grid properties (tried almost every one), also when I loaded up some custom theme (for example: Expression Dark) issue persists.
Don't get excited so much. Have some control on your temper.
It seems that you have not set the Right Margin of the button you have circled Red in the above image. Try to set the appropriate margins from the Property Window of the corresponding button and see what happens!

GetWindowPlacement/SetWindowPlacement not working in WinForms for high DPI

I've got a legacy WinForms app and I want to save the window position and size across sessions. I've been using GetWindowPlacement and SetWindowPlacement during the FormClosing and Load events. The problem I'm getting is that at higher DPI settings (Such as Medium, size at 125%) the sizes keep inflating. I'll call SetWindowPlacement on it with a certain size, but when GetWindowPlacement is called, those numbers come back 25% bigger, even though the window was the same size all along. The same sort of problem exists when saving the size of a resizable element within the form.
Now this works fine if I create a new WinForms project: The size stays stable even when running at the higher DPI. I'm guessing there's some legacy setting in the bowels of the project or some arcane Form setting that's messing it up, but I can't find out where.
I've called IsProcessDPIAware on both projects and both are true. Does anyone know what might be causing this?
Sounds like you are somehow triggering scaling, as selected by the AutoScaleMode property of the form. The difference between your two projects would be the AutoScaleDimensions property, visible in the Designer.cs file.
Not sure why this would cause a problem, but the Form class already uses GetWindowPlacement() internally, RecreateHandleCore() and UpdateWindowState() methods. To get real help, I assume you'll need to post a repro project somewhere.
I found this offending setting in the form's .resx file:
<data name="$this.AutoScaleBaseSize" type="System.Drawing.Size, System.Drawing">
<value>5, 13</value>
</data>
When this was present, VS would automatically change the AutoScaleBaseSize to work for your DPI, but no one else's. For everyone else, the form would constantly grow or shrink.
Choosing AutoScaleMode = Font in the designer properties panel caused VS to kick in and "modernize" the font scaling settings. Now it works for all DPIs.

visual studio 2005 designer moves controls and resizes Form

When i open a form in visual studio 2005 (c#) the designer automaticaly resize the form and move/resize controls without touching the designer at all. The source file is changed and when i close the designer i'm asked to save the *.cs file.
I tried to look into visual studio options without any success.
any ideas?
visual studio setup or something?
thanks,
Tal
I have been working on this problem for most of today and found some interesting things: The main source of the problem seems to be relying on anchoring. If I use docking to position my controls, instead of anchoring, my problems seem to go away. I found a couple of blog posts from 2003(!), which detail how you might use docking instead of anchoring, and explain how anchoring can break the Windows Forms designer. It seems like this problem might be over 7 years old!
Here are the posts:
http://weblogs.asp.net/rweigelt/archive/2003/09/24/28984.aspx
http://weblogs.asp.net/rweigelt/archive/2003/10/17/32407.aspx
http://weblogs.asp.net/rweigelt/archive/2003/10/23/33181.aspx
This is due to AutoScaleMode-property. Your forms have probably been designed with a different DPI or Font settings than you have now in Windows display settings. AutoScaleMode-property has 4 different possible values : Dpi, Font, Inherit or None. In Dpi or Font mode, your forms and controls will be automatically resized depending on windows display settings.
So, set the AutoScaleMode-property to None in all your forms and controls and they won't be automatically resized anymore. Try to design your forms in order to let sufficient space in every controls so that text will fit even if text size is set to 125%.
I found a work around.
not sure what happens behind but i changed my display properties. and it works fine.
here is the sequence: display propertis->settings tab->advance.
in the the advance dialog i changed the "DPI Settings" from Large (120dpi) to Normal (96 dpi)
Had the same problem with controls anchored top, left and right within complex TabControls. The visual studio forms designer was increasing the width of all nested controls each time I would open the form.
I found a simple workaround thanks to this post. I simply added a panel to each tab and set their dock property to fill. All existing controls within the tabs were moved inside those panels. This works, even if the controls are anchored top, left and right.
Works at least for Visual Studio 2013 and 2015.
This is one you should live with. Even in VS2008 such things happen from time to time. It is mostly depends on form content (controls, positions, etc), and there is no option in VS to disable such behavior.
When you open your form in designer, vs runtime rebuilds visual appearance from code behind. And sometimes it made changes at this moment. Also when you are simply adding one control to form, designer fully rebuilds codebehind and resource files. This is well known issue, and seems that MS won't fix it, because they move in WPF direction.
So several points to simplify your life:
Move to VS2008, designer were more consistent, but still shuffle controls in .designer.cs file
Place your code in one of the source repositories, so if you accidentally saved such form, you can restore it from repositary.
Setting the form Min and Max size settings to the current size was a good work around for me. This prevented VS from resizing it.
I had a trivial form with few controls on it, where the OK and Cancel at the bottom were being shifted up as soon as the form was opened in the VS2013 designer. The same behaviour was observed in VS2015.
The accepted answer here of DPI did not solve the issue for me, nor were there any issues on the size of the form/padding/margins.
Removing the controls that are shifted and adding them back into the form solved the problem for me, as suggested by ptutt here:
Visual Studio designer moving controls and adding grid columns when form is opened
While I appreciate ryantum's suggestion and links of using docking [with panels], as also referred to in the link above with Roland's blog post here https://weblogs.asp.net/rweigelt/28984, with something so trivial I'd rather just make it go away with removing/adding back in.
I found locking the controls from the format menu was a simple and effective solution. VS2013
I had this problem with VS 2015.
I used dock panels with the controls that moved unexpectedly as their childs.
By default the controls will be aligned to the left, but you can change the orientation.
My buttons stopped moving.
I had this issue, too. Every time I opened the designer, every box with anchor "right" was moved about 20 Pixels to the left. Additionally, the bottom of every box with anchor "left" was about 200 pixels outside the form.
This form has many controls and should not shrink on smaller displays, so it was set to autoscroll, the form itself was smaller in the designer than the shown minimum size (historically...). I just set the size to the minimum size so that no scroll bars appeared in the designer and the anchors worked as expected without screwing up the postitions.
I read the first post from ryantm's answer which led me to the solution. Apparently it has something to do with the order .Net executes events such as setting the size of a form.

Categories