Object size depending on window size? - c#

Is it possible to change size of winform and then according to that change also all objects on that form?
For example I have winform where I placed some datagridview, textboxes & labels. The window is for example 800x600. And then let's say he hit MaximizeBox and winform will change size to 1600x900, I would like then make all objects bigger according to that change. I don't want to scale down, so user can't change size below 800x600.
My question is if that's even possible to scale that somehow?

Every control has an Anchor property which can be set to follow the form's size top, bottom, left, and right.
https://msdn.microsoft.com/en-us/library/8y52cxte(v=vs.110).aspx
Forms also have a minimum height and minimum width property that will help you restrict the minimum size of the form.
You can do it via the designer or via code like this:
this.MinimumSize = new Size(800, 600);

Related

How can I get all controls on a form to expand out and increase proportionally in size when form is maximized?

I have a form that is pretty large in the IDE at design time (905 X 813).
I added this code to make the form full screen:
private void Form1_Load(object sender, EventArgs e)
{
TopMost = true;
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
...but the controls hug the Northwest corner and still leave a lot of "dead space", as the form is magnetized to the northwest corner, rather than its controls growing in size.
How can I get the controls to expand in all directions, filling the entire screen?
This doesn't need to take into consideration different monitor sizes or such - it is simply a mockup.
I tried to set the outer Panel's Anchor property to the middle/"all" setting, but it won't allow me - it stays on "Top, Left"...?!?
It's not too much clear to me... You can utilize ANCHOR, enabling LEFT and RIGHT to make the control grow to right direction, and disabling LEFT to make it "walk" through the form. Certainly doing a task on each control you will achieve the desirable position/size for them.
Anyway, you can manually index all positions and sizes of all controls using variables to get WIDTH and HEIGHT and calculate a kind of index. It is a big task to do but it enables you to control exactly each control position and height.
Bu if you want to redim the controls keeping all proportions (I mean, changing their FONT size), maybe this code may help you: http://www.codeproject.com/Tips/1025766/VB-NET-Dynamically-Resize-and-Reposition-All-Contr
Usually, in Wpf, I put all my controls in a Grid were every control takes spot(s) of its rows and columns. And I "lock" each control's size to its spot(s) by setting its size to Auto. Then I "lock" (size="Auto") the whole Grid with its parent, the Form. This way when the Form expands, the Grid expands, all its rows/columns expands, equally => Therefore all controls follow the expand equally.
In Winform, you could ise the TableLayoutPabel which is the equivalence of Grid in Wpf. Now, if you anchor to all 4 sides, the control will retain its size ratio according to the size of its parent, in this case its slot. So when the form grows, the slots grow, therefore forcing all controls to grow in SIZE.

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

How do you make buttons re sizable at run time?

I want to make it so that my buttons change size based on the text inside them. Kind of like a Label with it's height and width set to "Auto", but I would like to start with a pre-determined dimension.
Is there a way to place a button, size it, and allow for re-sizing based on run-time text changes? If so, how do I do this?
I've looked at this example:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/188c196e-90d8-4584-bc62-38d7e008cf5c/how-do-i-resize-button-text-upon-button-resize
It has to do with inserting a textblock on top of the button, but when the text adjusts sometimes the new text becomes too small because the text does not wrap for some reason...
Thank you.
You can set the MinWidth and MinHeight properties so that you start with a predetermined size and the button will be able to grow with text.
Unfortunately this would not allow the button to get smaller.
You will need to size it based on its content, then. That is, have no XAML defined size.
Elements on the page normally size themselves to their children.

Is there a way to leave the resolution of a C# Windows Form Application fixed?

I want to fix the resolution of my main form, I can set the number of pixels using
this.Height = 480;
this.Width = 640;
But I want some way to make it constant, so that even if the frame is enlarged, the number of pixels will not change, but their size.
By setting the option AutoSizeMode to GrowAndShrink I can prevent re-sizing of the form by dragging the edges of the form, but when the maximize button is pressed, the form becomes big and adds new pixels. I want to allow the re-sizing of the form and putting it in maximized or fullscreen mode without changing the number of pixels in the form, or in other words, enlarging the size if the current pixels, instead of adding new pixels.
Is there a way too do it?
Pixels are fixed units. You probably would like WPF since its a vector based system.

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

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.

Categories

Resources