What's preventing me from resizing (downsizing) my windows form object? - c#

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.

Related

Visual Studio Form Designer Changes Layout When Reopened Because Of MinimumSize

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;
}

Is there a programmatic variable for borders around windows form application?

I am trying to set the maximum size a windows form can be set to, which may be different depending on what data needs to be displayed.
Is there a way to get the size of the borders around your windows form without having to calculate it yourself?
Currently, I have used the difference of the Size and ClientSize properties, like:
windowDressingWidth = this.Size.Width - this.ClientSize.Width;
windowDressingHeigth = this.Size.Height - this.ClientSize.Height;
Which works, but seems a bit backwards. Is there some sort of SystemInformation or similar variable that stores this (like SystemInformation.HorizontalScrollBarHeight does for the scroll bar height).
The closest I've found is SystemInformation.FrameBorderSize, which is 8 on my system, and that is half of my windowDressingWidth (16), so that might be for each side.
In the case of this program, it has no menu, but (on my system) has the aero borders all around, and the thicker one on top for the title and the Maximize, Minimize, and close controls.
The only other one you could need is SystemInformation.CaptionHeight
I should note that this is a bad idea. Always set the ClientSize property instead, that automatically ensures that the window is large enough to provide the client area you need. Also the way the designer works, when you set the form's Size property in the Properties window then the designer actually records the ClientSize value in the InitializeComponent() method. Which ensures your form still works on another machine where the user changed the preferences. Like the caption font and button sizes. Or a machine that has Aero turned off.

How to run or adjust Windows application efficiently in any screen resolution in C# 4.0?

How to run or adjust Windows application efficiently in any screen resolution in C# 4.0?
I have done it in 1600 x 1200 resolution but if I try in lower resolutions only some part of the forms are visible.
How to solve this problem? I have searched a lot and got to know about Anchor & Dock will be useful but also to came to know that these should be used from the beginning of designing but I have completed my application while this resolution is now causing the problem when I install the application in any system
The quick and dirty method of making sure your controls remain visible would be to place a Panel on the form, set Dock = Fill and AutoScroll = True, then place every other control in your form inside it. Your controls won't get cut off, but your program won't win any awards for asthetics. If the program is brand new, that's really not the way you want to start things off.
You could redesign it to make use of Anchors and Docking, as you mentioned. You could also place items such as a row of Button controls or a series of TextBox controls in a FlowLayoutPanel, which will take care of repositioning them as you resize the form.
But if adjusting for screen resolutions is important to you, a better way would have been to use WPF from the start. Controls are automatically resized and repositioned as needed, based on their container control.
The real question should be:
How do I want the controls to resize themselves with their parent?
The answer is that you need to specify anchors. Anchors are used to tell your controls how they should react on resize, and what the concerns should be.
Lets say you have a form with two [Cancel] and [OK] buttons. They are usually seen fit at the bottom right of your window. But the default Anchor property is set to Top, Left, so on your form's resize, they stick to the Top, Left corner where they belong according to the default settings. This won't be any trouble if your maximize your form, thus you'll have your button probably in the middle of the screen. But at least, you will see every controls adequately.
But what if resize your form smaller and smaller? Do you still want them to stick at the Top, Left, or Bottom, Right would be more useful? My guess is that you should set the Anchor property to Bottom, Right, for those two.
This might come in handy to have different Anchor property settings depending on how you want your control to react to your form resize. Let's take three TextBox controls aligned horizontally with each other. Perhaps your longer field will be your object Description property located on horizontal-center of your form. Then, when you risize, you have to think what would make more sense on resize. If it is to make it longer on resize in order to fill your form width with all your control, then perhaps you want the DescriptionTextBox to get wider and wider, and the contraray should also be true, on form's resize, you probably want this field to be resized smaller too. Then, to make this happen, you have to set the Anchor property to Left, Right, so that the edge of your DescriptionTextBox control remains at the same very distance of your form's edge at any time.
Another thing is of concern in case of resizing to smaller window, is its MinimumSize property. One shall agree that there is use to have a form of size 34x34 pixels. So, setting your MinimumSize property to a certain size which makes sens for the form to exist, you will avoid display glitches of controls getting one over another.
For more details on the Anchor property: Control.Anchor Property
For more details on the MinimumSize property: Control.MinimumSize Property

Can't resize the form by dragging its borders

Form's AutoSize: False.
Form's AutoSizeMode: Grow and Shrink.
Issue: I cant resize my form by dragging its borders, I can only do that by dragging that grip thing on the bottom right side of the form.
Is there any other property on the form that I sohlud change to fix this problem?
Here is also a screen shot of the hierarchy of controls on the form...maybe setting on lower level controls on the form is causing this?
Because this is not in the answers, I'll write this here.
The problem seems to be caused by the form's AutoSizeMode being on GrowAndShrink, and not GrowOnly, which is the default setting. Resetting to GrowOnly fixed the issue.
(confirmed on MSVS2013 with .net 4.5 on Win7)
Make sure the FormBorderStyle is set to Sizable, and that the SizeGripStyle property is set to Auto or Hide.
I have the same problem if maximum size was set. Please set it to 0 or to bigger than now. After that you can move the border wherever you want. Change FormBorderStyle or SizeGripStyle can't help if maximum size is too small to new settings.
Tried everything in the above (and Microsoft's forums) couldn't get it to resize. Finally just opened another instance with a form I hadn't dorked up and compared. Here's what needed set. All are mentioned above but not as a combination.
In the form's property window (or in the code).
Autosize: False = allows resizing both dimensions (True = only width adjusted).
AutosizeMode: GrowOnly = allows both growing and shrinking.
FormBorderStyle: Sizeable.
I have the same problem, I can not manually change the size of form or controls in manual mode. Tried all the above, checked prior forms in the project, I could resize them.
Then, closed VS 2010, reopened the project, and I can resize the form and controls ....
" Did you try the power switch ... ;)"
int height = 960;
int width = 1280;
this.ClientSize = new System.Drawing.Size(width, height);
this way you can get a fixed form size...otherwise visual studio automatically changes it
I finally figured out what was causing this for me. This had been a problem for years! None of the things listed here helped, and indeed, all of them were already set as suggested.
It turned out that I couldn't make a window smaller in width if any control was anchored to the right edge, or smaller in height if any control was anchored to the bottom edge.

Docking and Anchoring on a Windows Form application

I'm developing an app for Windows Mobile 5.0 and above, with C# and .NET Compact Framework 2.0 SP2.
I have a WinForm with two panels inside (upperPanel and bottomPanel). I want that upperPanel always fill 2/3 of form's height, and bottomPanel fills 1/3 of form's height. Both panels will fill completly form's width.
I've used this:
upperPanel.Dock = Fill;
bottomPanel.Dock = Bottom;
But upperPanel fills the form completly.
How can I do this? I want, more o less, the same gui on differents form factors and on landscape or protrait mode.
Thank you.
What you need to do is to put the bottom panel on first and set its Dock property to Bottom. Then set the panel's height to be 1/3 of the form's height. Finally, add a second panel and set its Dock property to Fill. The key here is that you want to add the control that will fill the remaining area to be added last. Alternatively, you can play around with the Bring to Front and Send to Back commands in Visual Studio to get the designer to cooperate.
You may also need to hook the OnSizeChanged event for the form and re-set the height of the bottom panel to account for layout changes. It's been a little while since I did compact framework programming, so I'm not sure.
Right click on the upperPanel and select Bring To Front. However, I don't think this will give you the result you want. When you resize, the bottom panel will remain the same height, while the upper panel will stretch to fill the form.
Using your docking settings, with this code might do the trick:
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.bottomPanel.Height = Convert.ToInt32((double)this.Height / 3.0);
}
Set both panels to "not anchored". That is: Remove Dock-Value and clear the Anchor property. Then, move the controls so they are sized the way you'd like them to be sized.
After that, upon resizing the form, they should resize relatively.
EDIT
Oops, just tried it and sure it doesn't work. I mixed this up with a solution that automatically keeps controls centered within the window...
Well, I'd guess you then have to create a handler for the form's Resize event and manually align the controls after the form has been resized.
Go to Tools, Other Windows, Document Outline. Find the two panels, and swap the order of them. The control that has DockStyle.Fill has to come first for it to be docked correctly. (or last.. never sure which one it is, but it is one of them :p)
This won't solve the always 1/3 and 2/3 issue though... cause the bottom panel will have a fixed height (unless I am mistaken). I think maybe the TableLayoutPanel supports this though...
Update: As noted in the comments, that panel doesn't exist in the compact framework. So, I suppose the easiest solution to this problem would then try to use the docking, but update the height of the bottom panel whenever the size of the form changes.
If you want this to work perfectly you'll need to add some code to the Resize event of the Form which then specifically works out the relative sizes and places the controls in the correct place after a resize.
If you're not worried about losing precision and the forms aren't going to move much you can avoid this by using some relatively smart anchoring. Essentially you're going to have to select a "grower" (the part of the form that gets bigger, the bigger the form gets). In this scenario I would probably anchor the top part to Top | Left | Right and the bottom part to Top | Left | Right | Bottom. This would mean that the lower part of the form will get bigger if the form is expanded. In most cases this is acceptable. If it isn't use the Resize event and some code.
The easiest way to do this is to nest panels. Just set up panels for top bottom and fill. Then use panels within those panels to do the same. The only issues I've had therein are datagrid resizing, which is always a pain anyway. in that case, you have to use some code to resize the datagrid control on the form resize event.
I would like to add a point to #jasonh answer.
For the panel that occupies 2/3 of the form, you will have to set the AutoScroll property of the panel to true.
This will enable the panel to display scroll when the control size exceed the visibility to the user and also ensure the visibility of the smaller panel which is 1/3 of the forms height.
You can get the required design by using nested panels along with few setting with Anchoring and Docking Properties.Follow the following steps:
1) Add the Form and put a Panel1 on it. Set its Dock Property as 'Fill' and ResizeMode as 'Grow&Shrink'.
2) Add Second panel2 and set its Dock Property to 'Bottom', Set the Height and set the Anchor property to 'Top,Left'.
3)Add Third panel and set its Dock Property to 'None', Set the Height and set the Anchor property to 'Top,Bottom,Left,Right'.
Save and Compile. Now all the panels Would maintain their relative Positioning With resizing.

Categories

Resources