C# Winforms: GroupBox not resizing - winforms

I have two GroupBoxes on the left side of one of my TabControls, call them GroupBox A (top left), and GroupBox B (bottom left). The GroupBoxes do not resize like I would hope.
Example:
When I resize the main form that has the TabControls with my mouse, or maximize it, or move it to a screen with lower resolution, GroupBox B keeps its width / height. This causes GroupBox B to draw over GroupBox A, kind of like a 'always on top' effect.
Desired:
Would like both GroupBoxes to resize according to one another / proporitionally and fit the area they are given.
Ideas?

A tableLayoutPanel can help with this.
Add a tableLayoutPanel and size it to fit your needs.
Anchor the table to Top, Bottom, Left, and Right
Put GroupBoxA into the upper left cell of the table
Put GroupBoxB into the lower right cell of the table
For both GroupBox size them accordingly and anchor them to all 4 sides.
Now they will grow and shrink proportionately with the app.
Additionally you can add more of your controls to the table. If you need a control to span multiple rows or columns use the RowSpan/ColumnSpan property.

If your GroupBox is inside another control like a tab or something, then do as below:
In my case I had a GroupBox inside a tab and I called the below methods in the InitializeComponent() method to force the Groupbox to adjust to the tab size.
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.tabPage2.ResumeLayout(false);
this.tabPage2.PerformLayout();
And If you have multiple GroupBoxes, you need set the anchoring accordingly.

I faced a similar problem, I just had used the split container for both GroupBoxes, anchored the split container at top, bottom, left an right of my main form, and both GroupBoxes too, at top, bottom, left and right of their containers.

Slightly off topic from the original question, but my issue was that my MaximumSize field of my Group Box was not set, or was too small so I could not resize the height!

Altought the question is pretty old, someone might still find it useful...
I had the same problem and found a working solution -> instead of changing the width/height of the GroupBox, change its minimum width/height in the Form's resize method
edit: fixed typo
private void Form1_Resize(object sender, EventArgs e) {
groupBox1.MinimumSize = new Size(this.Width /2, this.Height);
}

Related

Can I drag a gridsplitter using an image that floats above it?

I have a vertical gridsplitter with a column width of 10 pixels. I've floated an icon over the top of it, and using a multibinding, this icon stays centred on the gridsplitter (so it follows the gridsplitter as it's dragged left and right).
Is there a way I can (a) allow the user to the drag the icon only left and right within the grid (I'm guessing by tracking the delta values?), and (b) bind the grid column widths to its position, so that the gridsplitter effectively follows it?
I can see a very rough solution in my head, but I'm wondering if I'm overcomplicating things - maybe there's a simple way to do this through binding?

Changing the anchor point of the scrollviewer (WPF)

I am trying to do something a little bit specific, and I can't seem to find if it is even possible. So I thought I would lay down the question here.
What I'm looking for is a way to change the anchor point of the scrollviewer from the top to the bottom (however you look at it). I'll explain what I mean.
By default when the control contained in your scrollviewer gets taller, so will your vertical scrollbar scollableheight.
But what happens is, the vertical scrollbar will stay at the same scrolloffset from the top, while what I want is that the vertical scrollbar keeps it's position relative to the bottom. The reason I want this behaviour is because I am creating a list that the user can expand more of by scrolling to the top. but then when I add the new items it looks like the scrollviewer scrolls up to the newly added items. (although it is actually the content the is pushed down) So if the scrollviewer would have the vertical scrollbar anchored to the bottom, the visible part of the content would stays at the same position to the user because the distance from that part to the bottom didn't change.
I hope you can understand what I'm trying to do.
I allready found one way of doing this, but it is not the desired option to me.
Option 1) Apply a Scaletransform of -1 in the Y-axis to the scrollviewer to flip it upside down. Then apply another ScaleTransform of -1 in the Y-axis to the content of the scrollviewer to make the content right side up.
But the problem with this technique is that for one, when you scroll using your mousewheel the scrollviewer scrolls inverted as normal. And second, I'm using 2 scaletransformations just to change the behaviour of the scrollviewer, and not even for eye candy. So that seems a bit excessive to me.
Option 2) anyone?

How to re-size add/remove from list controls in window's form to provide anchoring like behavior

I want to anchor below datagridviews to top, left, right & down in a way that they don't over lap when the size of form is increased or decreased. Dock and Anchor both don't seem to provide any solution for this.
You probably need to have a TableLayoutPanel handle that. Three columns, middle column is fixed (Absolute), the outside columns would be based on percentage (50% each).
The DataGridView controls would then be dock-filled into each side column.
Or just handle the layout yourself in the form's Resize event.

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.

How to get controls in WPF to fill available space?

Some WPF controls (like the Button) seem to happily consume all the available space in its' container if you don't specify the height it is to have.
And some, like the ones I need to use right now, the (multiline) TextBox and the ListBox seem more worried about just taking the space necessary to fit their contents, and no more.
If you put these guys in a cell in a UniformGrid, they will expand to fit the available space. However, UniformGrid instances are not right for all situations. What if you have a grid with some rows set to a * height to divide the height between itself and other * rows? What if you have a StackPanel and you have a Label, a List and a Button, how can you get the list to take up all the space not eaten by the label and the button?
I would think this would really be a basic layout requirement, but I can't figure out how to get them to fill the space that they could (putting them in a DockPanel and setting it to fill also doesn't work, it seems, since the DockPanel only takes up the space needed by its' subcontrols).
A resizable GUI would be quite horrible if you had to play with Height, Width, MinHeight, MinWidth etc.
Can you bind your Height and Width properties to the grid cell you occupy? Or is there another way to do this?
There are also some properties you can set to force a control to fill its available space when it would otherwise not do so. For example, you can say:
HorizontalContentAlignment="Stretch"
... to force the contents of a control to stretch horizontally. Or you can say:
HorizontalAlignment="Stretch"
... to force the control itself to stretch horizontally to fill its parent.
Each control deriving from Panel implements distinct layout logic performed in Measure() and Arrange():
Measure() determines the size of the panel and each of its children
Arrange() determines the rectangle where each control renders
The last child of the DockPanel fills the remaining space. You can disable this behavior by setting the LastChild property to false.
The StackPanel asks each child for its desired size and then stacks them. The stack panel calls Measure() on each child, with an available size of Infinity and then uses the child's desired size.
A Grid occupies all available space, however, it will set each child to their desired size and then center them in the cell.
You can implement your own layout logic by deriving from Panel and then overriding MeasureOverride() and ArrangeOverride().
See this article for a simple example.
Well, I figured it out myself, right after posting, which is the most embarassing way. :)
It seems every member of a StackPanel will simply fill its minimum requested size.
In the DockPanel, I had docked things in the wrong order. If the TextBox or ListBox is the only docked item without an alignment, or if they are the last added, they WILL fill the remaining space as wanted.
I would love to see a more elegant method of handling this, but it will do.
Use the HorizontalAlignment and VerticalAlignment layout properties. They control how an element uses the space it has inside its parent when more room is available than it required by the element.
The width of a StackPanel, for example, will be as wide as the widest element it contains. So, all narrower elements have a bit of excess space. The alignment properties control what the child element does with the extra space.
The default value for both properties is Stretch, so the child element is stretched to fill all available space. Additional options include Left, Center and Right for HorizontalAlignment and Top, Center and Bottom for VerticalAlignment.
Use SizeChanged="OnSizeChanged" in your xaml and the set the sizes you want in the code behind.
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
TheScrollViewer.Height = MainWin.Height - 100;
}
Long term it will be better for you.
When your manager comes along and asks "make that a bit bigger" you won't to spend the afternoon messing about with layout controls trying to get it to work. Also you won't have to explain WHY you spent the afternoon trying to make it work.

Resources