WinForms autosize vertically while leaving width adjustable? - winforms

Is it possible to use WinForms layout engine to set up a form in such a way that the height is adjusted completely automatically to fit the content, but the width would be adjustable by the user?
As an example, consider the following somewhat contrived form:
TLP: TableLayoutPanel, with 1 row and 2 columns.
label1: Label, contained in the left column. Set Text to something long, AutoSize to false, Dock to Fill.
button1: Button, contained in the right column. Set AutoSize to true.
Now set the row to "AutoSize", the first column to "100%" and the second column to "AutoSize".
Finally, set the whole TLP to AutoSize true, AutoSizeMode to GrowAndShrink.
At this point, the TLP could reasonably be resized horizontally, but WinForms doesn't seem to allow this.
The idea is that the form containing this TLP would grow or shrink vertically based on the width, which the user can change by resizing the window as usual.

Hopefully you have got the solution by now. But in case anybody having a same query, You can set width of Maximum Size Property to the Actual Width of the Form and the Height of MaximumSize to 0.

This is entirely possible. To do this, you have to override SetBoundsCore on your form. In this you can then control the height such that it doesn't change when a resize might want it to. Just store the height you want it to be in a variable and then have your override always set it to that. This way, when you auto-size the height, you save the new height aware before setting form Height and SetBoundsCore will honour that new height.

Related

Change DataGrid width on resizing its columns

I have a DataGrid with a custom theme. Right now, if I resize a column and make it wider, it tries to squeeze other columns to make space for this column.
Instead, is it possible to increase the width of the entire DataGrid with the increase in width of a column. It's OK if it shows a horizontal scrollbar at the bottom.
EDIT: Just to clarify, each column has a MinWidth set to it. So if the overall width > window width, I see a scrollbar. But in cases where I have a datagrid <= window width, and I expand one of its column, the width of the datagrid should theoritically increase at the point when the other columns have shrunk to their MinWidths. At that point, instead of letting the DataGrid grow in width, it doesn't let me expand the column any further. Instead, I want the DataGrid to expand and accommodate the increased space.
It sounds like one of your columns has a Width="*", which means that column is assigned to all remaining space and grows/shrinks as needed instead of letting the DataGrid grow/shrink as requested and showing it's HorizontalScrollBar.
The datagrid's default behaviour is the one you need! If your Datagrid is not showing a horizontal scrollbar maybe because it's contained in a control that's constraining its width. Try to put it inside a Grid, and be sure that you haven't set the 'Width' property of any control (including the Window) that is containing it.
that's what column's MinWidth is designed for. Just set it for the columns, which you don't want to shrink. to limit the max width of the grid use the ScrollViewer:)

Silverlight Get actual width of Grid that is set as Auto

Say I have a Grid whose Width is set as Auto. Inside this grid I want to put a new grid that is a square. To do this I want the new grid's width and height to be equal. I thought I could just set the Width and Height to the Parent Grid's Width. The problem is, the Parent grid's width is double.NaN since it is set to auto.
My question, is there a way to get the ActualWidth of this parent grid, so that I know how wide it is? If not, is there another way to create a Rectangular Grid that fits inside the parent grid?
You have to use the property of ActualWidth
var width = LayoutRoot.ActualWidth;
I am not sure how well it works with windows phone 7 but see if this article helps you.

How to determine width of a windows forms controls whose width was increased because it is Anchored?

I currently have a Textbox on a Windows Forms, and I need to dynamically add a PictureBox box control at the right of the Textbox.
I create the PictureBox programmatically and I when setting the location of the PictureBox, i'm setting like this:
pBox.Location = new Point(tbControl.Location.X + ctrl.Width, ctrl.Location.Y);
So i'm setting the picture box to be located at the X location of the textbox PLUS the width of the textbox. However, since the textbox has an anchor property set to right, its width increases to fill the space between itself and the form border.
Problem is, that even though the textbox's width is visually bigger than the actual value of Textbox.Width. the Width property is not taking into account the extra width of being anchored.
I already tried properties like Textbox.Bounds.Width, Textbox.ClientSize.Width, Textbox.DisplayRectangle.Width, etc. with no luck. All of those properties return the original Width of the control without taking into account the resized width due to the Anchor property.
Does anyone know how I can determine the real size of the textbox? Thank you
The Width property always tracks the current width of a control, whether it is anchored or not. However, the TextBox is going to grow when you make the container larger and that will make it overlap the PictureBox. You have to anchor the PB to the right as well.
These should be returning the adjusted size. Either you are referring to the wrong textbox, or you are doing the query before the size has actually changed.

Spreading controls to fill space as a dialog resizes

I have a dialog with column down the right side filled with buttons. The dialog is built with Windows Forms. I have a mockup at the following link:original dialog
(I would have included it but apparently i'm not allowed to use image tags)
I would like for the buttons in the right column to resize themselves to fill the remaining vertical space when the dialog resizes. It doesn't particularly matter to me whether or not buttons simply increase in size or whether the buttons remain the same size while the gaps between them increase. I'm simply want the buttons to go from the top to the bottom. (I have a mockup for this as well but apparenlty i can only include one link)
I've tried hosting the buttons in a FlowLayoutPanel but they do not increase as the dialog stretches, I only get whitespace at the bottom after I run out of buttons. I also tried a TableLayoutPanel and had the same result but I may have misused it. Does anyone have any ideas how I could accomplish this?
Thanks in advance,
Jeremy
To get you started. Use the TableLayoutPanel, set its Anchor property to top, bottom, left, and right. Set the rows and columns to percentages as needed. I suggest each control have it own cell. Note that each control in a "cell" can have its Dock and Anchor property set as needed.
You can do this with a TableLayoutPanel. Create a column for the buttons, with each button having it's own row / cell in the column. Set each row to be an even percentage for height (if there are 10 buttons, each row would be 10%), and dock the TableLayoutPanel to the right side of the screen. Then, put the buttons into their rows and set them to full docking. Then, when the dialog expands, the TableLayoutPanel will expand to fill the entire right side of the screen, each row will adjust proportionally, and each button would expand to fit the new row size.
You may have to adjust this a bit to fit your needs, especially in how it relates to the other content in the window.

MaxAutoWidth in WPF

Is there a good way to limit the Auto Width of an element or Grid Column?
I want to limit the Auto Width only, but allow any user changes.
For example, I want to allow GridSplitter to arbitrarily resize the column, but the initial (Auto) Width should not exceed a specific value. Same for the elements -- I want to allow resizing, but limit the Width they have if never resized.
MaxWidth works exactly as opposite -- it does not affect Auto Width of Grid Columns (it might be a bug), but limits the resized width.
Is there any good solution that works for (almost) any element or Grid part?
Try binding the MaxWidth to the Width and use a IValueConverter to react to changes from Auto to specific values.
In the grid column set the width you want the column to start at. Any changes to the column width made by the user using the grid splitter will overwrite the width you start with.

Resources