This question already has answers here:
How to make the UI responsive in Winforms [closed]
(2 answers)
Closed 4 years ago.
We have developed POS system using windows application ,but there is a requirement to make it responsive ,which is not possible.
We have tried to use Flow panel but 100% responsive not done.
we have another option to convert it into WPF but it will require more time.
any one have suggestion to make windows form application which adjust according to screen resolution.
You can evaluate TableLayoutPanel and ensure that columns and rows are defined as percentage instead of absolute values. You would also need to work out docking and anchoring for individual controls.
If you are looking for layout to switch from, say, horizontal to vertical, then you will need to work with resizing event of the form.
Pretty much, you can't make a WinForms app respond well to different resolutions: it's possible, but it's a heavy amount of work and probably won't be too wonderful when you are finished.
The problem is that although all controls can be Anchored and Docked so they resize automatically, that doesn't have any effect on text within the controls: so you end up with a large button with tiny unreadable text in the middle, or a tiny button with huge unreadable text in the middle.
While it's possible to get round this by handling the Resize event for each control and working out what font size to use based on the new display area, that's not trivial and generally takes a fair amount of trial and error, plus the odd "fudge factor" thrown in to deal with strange cases.
You can do it, but it's serious work - WPF handles it a lot better!
The other solution is to redesign your UI to work well at multiple resolutions in the same way tat Visual Studio does: a central "work space" with all the tools in panels which float or dock round the edges. But for a POS system, that probably isn't practical!
I am working on a Windows Forms project. It contains a tab controller with multiple pages and multiple controls on each.
It appears that relatively recently, after some form changes, that each time I build and run the solution the form resizes/shrinks.
So if I set the size of the form height to 768, once I click 'Start' to build and run it, I can actually catch a glimpse of it resizing itself during the process and then the form loads 21 pixels shorter than the height value it was at build for.
If I then keep building and running my project, the form will decrease by 21 pixels each time, making it smaller and smaller with every build.
We think it might have been introduced when we added the 'DataGridView' controller to one of the tabs, but we have yet to prove if that's the case.
Is there a reason why this would be happening, and what could be doing this?
Why would it resize itself during build run time?
This is an annoying bug, and I have suffered similar behavior myself. However, there maybe a couple of workarounds, however be warned though, these may or may not help and sound a little hacky.
Solution 1
If your control isn't docked, docking may help.
Solution 2
You might be able to change your DPI settings to eliminate the problem, i.e.:
Display Properties → Settings tab → Advanced. In the the Advanced dialog I changed the "DPI Settings" from Large (120 dpi) to Normal (96 dpi).
Solution 3
This is maybe due to AutoScaleMode-property. Your forms may have been designed with a different DPI or font settings than you have now in Windows display settings. Try setting the AutoScaleMode-property to None in your form and offending controls, and they won't be automatically re-sized anymore.
The Problem
Apparently there is a bug in Visual Studio 2015. It is not calculating the Size properly when certain circumstances are met. In my case I was using the following...
(Name): Form1
AutoScaleMode: Font
AutoSizeMode: GrowOnly
DoubleBuffered: True
Font: Verdana, 8.25pt
FormBorderStyle: FixedDialog
Icon: (Icon)
MaximizeBox: False
MinimizeBox: False
MinimumSize: 600, 170
Size: 600, 170
StartPosition: CenterParent
Text: MyTitle
Now... If you close this form and open it back up the Size is still exactly the same (600, 170). If you change the following property...
ControlBox: False
Then you closes the form and open it back up you will notice the Size has now been changed to (610, 203).
My guess is that the bug is not accounting for two things. One would be the form title bar HEIGHT. The other would be the title bar icon WIDTH. This would be the offset of a WIDTH of 10 pixels and a HEIGHT of 33 pixels.
The Solution
Well you actually have three workarounds. One would be to offset the MinimumSize. Another would be to actually show the ControlBox. The last one would be to fix the issue in code by setting the ControlBox property after the form is initialized.
Offsetting The MinimumSize:
So the first option would be to offset what you want the MinimumSize to be by (w:10, h:33). So for example, If you want the MinimumSize to be (600, 170) then you need to set it to (590, 137). That would actually produce the size you expect to see.
Showing the ControlBox:
Simply change the following property...
ControlBox: True
Correcting the issue with code:
You will need to change the following property at design-time...
ControlBox: True
Then you will need to set the ControlBox property to False after the form is initialized.
public Form1()
{
InitializeComponent();
this.ControlBox = false;
}
This Works for me,
on form designer.
Copy the values of your form size.
Past it in both Minimum and Maximum Size property.
Enjoy
I had the same problem. My solution:
My PC's system is Windows 10. The resolution of the monitor was 125%, and I set it to 100%. Then I set the size of the form, not changed.
You can see the resolution settings in this picture:
In Turkish "Scale" is "Ölçekle". There are resolution options on the bottom ("ölçekle ve düzenle").
I figured it out!
If you click on the form in "Design View" → "Properties" → "MinimizeBox"
and change "True" to "False".
OK, so first in response to Saruman's suggested solutions..
None of the controllers in the application where had docked values. A good few were anchored, but none appeared docked. I docked a couple of the main containers I could find, and it didn't seem to make much difference. Admittedly I didn't dock every single controller, but I then moved onto solution 2...
I wasn't sure where to find the DPI settings. Whether somewhere in Visual Studio, or on my machine. So I moved onto proposed solution 3...
On the Form initialize, I added 'this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;'. This then seemed to automatically place it above a 'this.ClientSize = new System.Drawing.Size(11264,730)' method, which piqued my interest.
With a little further debugging, it appears that when the application builds and intializes, the 'this.ClientSize' property is already set at a very low, more or less, '230x200'. I am not sure where this value comes from, but I wonder if it has anything to do with the initial resizing before it then tries to set it again to something bigger...
Regardless, I came across another suggestion to possibly double check my form's minimum width and height properties, and noticed they were set to 0x0, I'm assuming by default.
So I set the minimum values to be the same as my form's size value, and on subsequent builds it appears to be keeping its size now! Or at least, it isn't shrinking any more from what I can tell.
I'm not sure if setting the minimum form size is an acceptable solution for this bizarre behaviour, but so far it seems to be keeping the application size consistent on each build we do, which is a relief at the moment.
If any one has further knowledge on the ClientSize property and if I am right to be concerned about its initial low size, would be great to hear it. :)
Upgrading to .NET framework 4.7+ will resolve the issue of text being cropped. Furthermore if that isn't applicable add the following line to the app.config file
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
<add key="EnableWindowsFormsHighDpiAutoResizing" value="false" />
</System.Windows.Forms.ApplicationConfigurationSection>
This will disable the scaling performed by windows thus the application size won't change. For more details visit Auto Scaling in Windows Forms
Faced the same problem. Fixed it by changing 'AutoSizeMode' to 'GrowOnly'
If you are experiencing this issue in Visual Studio 2019, a simple fix is to set the AutoSize property of your form to True, and then to place a small, invisible control, such as a Panel, in your form at the extreme lower-right of the form. Size the panel to something like 20,20.
When the form automatically resizes upon running, Windows will calculate the form size to include all of your form's controls, which will include the small Panel you've placed in the lower-right.
I think that this is the simplest, easiest approach to have your forms sized appropriately without fighting Windows automatic scaling.
And we have a late contribution (if someone, like me, ends up here with a problem similar to mine) :)
My problem proved to be a shortcoming in VS. Having a "high dots per inch" (HDPI) monitor as my main screen was what caused my problem. Having different zooms on different monitors messes things up for VS. Running the executable on other machines (sometimes) shrunk the form so that (roughly) the bottom quarter wasn't visible.
Read about it here: https://learn.microsoft.com/en-us/visualstudio/designers/disable-dpi-awareness?view=vs-2019
But in short - here are the three suggested solutions:
Restart Visual Studio as a DPI-unaware process
Add a registry entry
set HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers to the path to the devenv.exe (for VS 2019 normally C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe)
Set your display scaling setting to 100%
The first is the suggested solution in the article, but that messed up other things in the Form editor for me, so I ended up turning down the resolution and setting the zoom to 100% for all monitors.
I have just come across this problem setting up a form in Visual Basic in VS2008. I found the solution was to leave the form’s minimum size property at 0, 0 at design time and set it with code in the formload sub.
I know there will be folks who will vote this question down or ask for close. But if there is any kind of information or code I can provide you to know more about my program I will let you know. So please keep reading and see if you have had a similar problem.
I am running a Win7 64bit with .Net Framework ver 4.5.
I have created a Winform application. And to create my form elements I have taken advantage of a library called Metroframework that gives the program a metro look and feel. It contains some standard controls and as well as user controls that inherit from the original Form class. This is the opening view of my program.
However, for some strange reason, when I came to install my program on two other computers (one running Win7 and the other Win8), I noticed that some of my form elements have changed their location and some have just disappeared or just have been displaced. This has frightened me knowing the amount of time I have spent to put this elements in place.
Everything looks fine on my own computer both in dev environment and after building my application in release version. At first I thought this is a screen resolution problem on the other two devices, but that was not the case either. And even if it was, why should this happen?
Can anyone please help me solve this problem? I will share any part of my code you need. But I really have no idea where the source of this problem is!
The main influence on the layout variations on different machines is theForm.AutoScaleModeproperty.
In theory the default setting should work fine but I found that sometimes it is best to switch it off completely, that is going from Dpi or Font to None..
MSDN explains a little about the intended effect.
BTW: The is also a ContainerControl.AutoScaleMode property, so you could choose different modes for some parts of your forms as described here:
The AutoScaleMode property specifies the current automatic scaling
mode of this control. Scaling by Font is useful if you want to have a
control or form stretch or shrink according to the size of the fonts
in the operating system, and should be used when the absolute size of
the control or form does not matter. Scaling by Dpi is useful when you
want to size a control or form relative to the screen. For example,
you may want to use dots per inch (DPI) scaling on a control
displaying a chart or other graphic so that it always occupies a
certain percentage of the screen.
To remain true to the pixel-precise layout use:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
The last value in the AutoScaleMode enumeration is Inherited which most often would be chosen for nested containers.
Update: The choice of ContainerControlsis rather limited:
System.Windows.Forms.Form
System.Windows.Forms.PropertyGrid
System.Windows.Forms.SplitContainer
System.Windows.Forms.ToolStripContainer
System.Windows.Forms.ToolStripPanel
System.Windows.Forms.UpDownBase
System.Windows.Forms.UserControl
Maybe the most useful is the UserControl. Note that it doesn't expose the AutoScale property in its instances but only in the class definition. Also note that you can't add controls to an instance in the desiger, but you could assemble them in maybe a Panel and then set a UserControl (with AutoScale=Font) to be the Panel's Parent.. You'll need to allow for some extra space in any case, though..
I have created a panel in c# application that holds rows of 5 textboxes.
Textboxes are added to panel dynamically. It is 500 pixels in width and each textbox is 100 pixels wide.
First textbox is at x-position 0, second at 100, third at 200 and so on.
So the 5 textboxes should fill the panel horizontally. These are shown correctly at my computer but at another computer these textboxes appear as if their width is reduced and they do not extend to end of the panel. They leave blank space at the end of panel.
Can anyone tell me why is this difference in display of textboxes?
There could be a few different reasons for this. Depending on if you are using WinForms (which I am assuming) or WPF. There is a system DPI that can be changed in windows. Windows Vista and 7 take advantage of this more. The other issue could be with the windows themes (play with the handicap themes). How to check your system DPI
I think that the problem is in displaying textboxes in computers with diferent Windows themes.
With Classic windows controls it compute space in different way than when themes are turned on on computer.
Couple things to try, make sure the AutoSize property is set to false on the Panel and the Textboxes. Also, make sure the PreferredSize is also set.
I was having a similar problem, and found this link:
C# WinForms messed up control positions?
where changing my main form's AutoScaleMode attribute from Font to None or Inherit solved the problem (NB: changing to Dpi did not solve it).
I have an application I have been working on for a while in VS2008 developing in Windows XP and it has some panels placed in specific spots so the borders line up and look nice and pretty. Now that I have switched to developing in 7, as far as I can tell everything else is in the same place but it moves both panels over a little bit and one up and one down and messes up my nice borders. Since it still works correctly in XP I'm assuming this is a 7 problem or a VS problem with 7. Anyone have an idea whats going on or if its fixable?
So I figured it out. Turns out the client area wasn't narrower, however the titlebar which is part of the dialog box border was two different sizes in XP and 7. Since the panels were in an mdi child, where the border was not shown because it was underneath the parent, the size of the titlebar part of the border was making a difference in the location my panels were shown relative to the parent. To solve this I set FormBorderStyle to none on the child and re-positioned the panels to be in the correct spot without that titlebar. It now looks the same in XP and 7 since that variable bar size is gone.
Windows in Windows 7 have wider borders.
Your form probably has a fixed size that is based on a Windows XP border width.
Therefore, in Windows 7, the form's client area will be narrower.
If this is in fact the problem, you can solve it by setting the form's ClientSize property in the constructor the the value it currently has in XP.
If this is not the problem, please post more details.
Do you need to have your panels pixel-positioned? .NET 2.0 introduced the FlowLayoutPanel and TableLayoutPanel for resizeable positioning of elements.
The TableLayoutPanel is the more useful of the two. You create rows and columns, which can autosize or size proportionally to each other. You dock the TableLayoutPanel in your form or anchor it to all four sides. Then the user can resize your form and everything resizes with it.
Even if your panels are of a specific size, you can anchor them to a side or a corner so that they stick to the side even if the user resizes the form.