I have a problem when working with a form that has a fixed size.
When I resize Visual Studio windows, the form resizes as well, even when being locked. It is a huge inconvinience because I can't get an idea of how my design would end up.
I am working with a fixed size aplication, that has a MDI child form.
I need the form to have FormBorderStyle to None, so I can't set it to something fixed.
Image of the form with desired size
Image of the form with deformed size
Set the MinimumSize and MaximumSize properties of the form to the desired size. This makes the from size fixed.
Note, this trick also works for single-line TextBox controls, if you want to set the height to a different height than the one automatically determined from the font size.
Related
I have a Winform I want to resize. Not after launching; I want to resize it in the designer by dragging the edges.
I am aware that I can change the size manually in the form properties. This is not what I want to do. I want to quickly size the default size of the form with my mouse.
The problem appears to be that I cannot grab the edges of the form for some reason. I repeat, this is NOT DURING RUNTIME. It is not that I have disabled/enabled Autosize/Resizing.
It's almost like the edge grabs are a single pixel wide and I just can't hit it. I don't know. I've looked for accessibility settings. The last thing I want to do is reinstall MSVS. It's just maddening.
I've attached a photo. The little boxes on the bottom, right, and bottom-right corner are my targets.
you can try to click the form title before resizing. I think you may have a panel (or any container control) which is full docked in the form, that's why when you click the form body, you selected the panel instead of the form.
Another issue that I encountered was the windows font size was set to 120% (or more), so go to windows display setting and set the font size to 100%.
I am developing a Winform application which should occupy the entire screen, the target device having 1920x1080 resolution.
The main control window is defined as follows:
MaximumSize 1920, 1080
Size 1920, 1080
MinimumSize 1918, 1078
StartPosition CenterScreen
To fill the screen I have added the following to the _Load method:
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Activate();
When I run the application the Control Window begins at the top left, as it should, but the rightmost 5th and the bottom 5th (approximately) of the form is off the screen. The controls that should appear in the cut-off regions are at appropriate locations (e.g. a label at Location 1712, 551, which should be within the boundaries of a 1920x1080 screen).
Can anyone explain? If I knew why this is happening, I'm sure the solution would be obvious.
I have added the following to the _Load method
Using the Load event handler is the problem. You are changing the layout metrics of the window after it was created. A window is maximized by giving it a negative location and a size that's larger than the screen by twice the border width. So that the border isn't visible.
But your code changes FormBorderStyle and now that location and size is not correct anymore. Since the window no longer has a border. So the window is too large and part of the client area is off the screen.
You must move the FormBorderStyle assignment into the constructor of the form so the window is created with the correct border. Setting the property with the designer is simplest.
It is possible your Windows installation is actually scaling the form. This can be done using the DPI settings (a cut-off of 1/5th sounds like a DPI setting of 125%), or by Windows 10 when it is self-adjusting.
The best thing to do is not rely on screen measurements to be correct. Instead, try to use as much panels that are capable of auto-sizing, like TableLayoutPanel and others. In that way you won't need to keep calculating, instead the framework will do that for you.
So I create a form layout and try to set a MinimumSize on it only to find out the WinForms has a fit! So here is what it looks like when I start out with MinimumSize property set ONLY for the width...
As you can see it looks great, with no extra whitespace, the form is the size I want etc...
Then, I try to close the form and open it back up, I have a small oddity (the MinimumSize height was set to 0 and now WinForms has made it 38) but whatever it still works as expected..
OK well that odd "38" on the MinimumSize-Height is weird but whatever, let me try to set my prefered MinimumSize-Height to 420 (Same size as the form).
Save, Close form designer and reopen... WTF? Everything has gone awry! My three buttons at the bottom have all been pushed up, so has the text box and the treeview control I have above it, as well as the left listview height has been resized. As a matter of fact, looking at it it seems as if the controls are all the correct size still. The variable that has changed is the form size. It has somehow gone from 420 to 442???
Anyone have any idea why this keeps happened? What I can do to fix it or I guess work around it? Please keep in mind I have the buttons, and textbox anchored to the bottom. The treeview needs to stay anchored to the top and grow with the resizing of the window.
Thanks!
This is a designer bug of course. It is induced by a feature in Winforms that is generally a trouble-maker for the designer, it never actually stores the Size property. Something you can see in the auto-generated code in the form's Designer.cs file. Instead, it stores the ClientSize and calculates Size based on that value. A very necessary feature, the height of the caption bar is unpredictable, it depends on user config.
What triggers the bug in your case is setting the ControlBox property to False. That also disables the Icon and that gums up the outer size calculation. Something to do with the order in which properties are assigned, I think, the Form class ought to implement ISupportInitialize but doesn't. The value 38 for MinimumSize.Height is a side-effect, that's the height of the caption bar on your machine. You can't make the window smaller than that. A constraint that's also applied at design time, another quirk.
The workaround is simple. Set ControlBox property back to True and just set its value in your Form constructor:
public Form1() {
InitializeComponent();
this.ControlBox = false;
}
I have developed an application in C# .NET 3.5 and I would like it to maintain the same window size and font size even when the system DPI is changed.
I've set AutoscaleMode to None in the main form. The form and controls are not resized but the size of all the texts is upscaled.
Is there a way I can prevent texts to upscale or how can I control them to reset them to the size they have at 96dpi?
Thanks.
A potentially easy solution is to change the font sizes of your controls proportionally to the changes in DPI. When the DPI changes, scale up/down your font sizes accordingly.
Another possible option is to "hard-render" all of the text on your controls as images. Then set scaled-up or scaled-down copies of the images as the BackgroundImage values of your controls. If you do this, be sure to set your controls' BackgroundImageLayout properties appropriately.
I've got a windows form object that contains 3 objects, a treeview, a richtextbox, and a tabcontrol. They are not docked into the windows form, but they are anchored (top+left).
I've written the code to resize them when a form-resize event handler gets called, but it only seems to be working for an increase of form size, that is to say, I can't resize the form to a smaller size. This includes times when I first increase the main windows form and then attempt to return it to its original size.
The sizes of the three objects are manually set after each Form resize with the code below:
treeView1.Height += (this.Height - oldHeight);
richTextBox1.Width += (this.Width - oldWidth);
tabControl1.Width += (this.Width - oldWidth);
tabControl1.Height += (this.Height - oldHeight);
oldHeight = this.Height;
oldWidth = this.Width;
None of the objects have a set minimum size (they are all at 0,0 throughout the resizing process)
What is preventing the form from being resized to a smaller size?
Autosize (which was set on the main Form object) was preventing the window from decreasing to a size smaller than the objects contained within it. As the objects within the main Form increased on each expansive resize, the main Form was unable to shrink after any resize growth. By disabling Autosize on the main Form object, I was able to regain full control of resizing.
If the above does not solve it, check that the form minimum size is not set to a value larger than you need.
Right click your Winform in Visual studio -> select properties ->AutoSize will be set True -> Change it to False
OR
Include this line in your code
this.AutoSize = false;
There may be one more way to correct the behavior of a form which cannot be resized by normal mouse selection.
Related to the discussion above, I discovered (using VB.net in VS2010) that one of my standard Windows forms would not resize with mouse selection. It is as though the FormBorderStyle was set to Fixed . Even changing FormBorderStyle to SizableToolWindow did not give the expected mouse-sizable behavior.
Here are some form settings from the form design Properties:
Autosize: false
AutoSizeMode: growonly
Doublebuffered: true
Enabled: true
FormBorderStyle: Sizable
ImeMode: NoControl
Locked: false
AutoScaleMode: Font
AccessibleRole: default
All of the visible or code Property settings of this form, and all of its Designer property settings too, were identical to other forms in the same project that would properly allow mouse resizing. Cleaning the solution, and Rebuilding it also did not fix the problem of the frozen form. Clicking Maximize did work, and so did click Minimize. The form size could be set by code as expected. Only mouse resizing of the form did not function properly.
I discovered that the desired mouse-selection resizing could be again enabled by setting, in code, by setting the parameter:
myForm.AutoScaleMode = Windows.Forms.AutoScaleMode.Inherit
This parameter had been set to .Font in the design of the form, which was also used in other forms that worked properly.
Then... strangely... changing it back from .Inherit to .Font in code also allowed the form to resize properly.
That setting in code (either to .Inherit, or to .Font) seemed to be the critical element to correct the form resizing trouble, in this case. It seems there are hidden parameters which the system does not show the user that somehow interfere with the expected operation of a form.
Just change default MinimumSize in form's properties to a number other that zero (like 10)
Just put all your Controls into a Panel and set Dock property of the Panel to Fill. I believe it works even with Autosize set to true.
Check the Min Widths and Min Heights
Designer view:
As you can see, if you set the Min width and Min height, while having auto size set to false, then the buttons are now the size you want it to be :)
I followed all the answers here, none worked for me. I went ahead and added the padding and it worked.
head over to properties tab selecting the label and add padding.
I was able to resolve this for myself (found this question while looking for my own answer) by setting the FormBorderStyle to SizableToolWindow. It appears the Sizable border style has some sort of default minimum width baked into it (I couldn't get it below 136), while SizableToolWindow does not.