winforms adjust controls to push up vertically when others are invisible - c#

I've create a c# winforms form,
It has a bunch of labels positioned and a flowlayoutpanel.
on certain occasions i set one of the labels and the flowlayoutpanel to visible =false.
As a result i want all labels beneath them to be pushed up - at the moment there is a gap where they were.
Also, I'd like the flowlayoutpanel to grow and shrink depending on the number of items it has.
at the moment it is just the size i set it to be in the designer.
please can you help with these 2 issues.
Thanks

If I got you correctly, I would suggest using a TableLayoutPane with two rows. The top row will contain a docked panel with all the controls that may be hidden. The bottom row will contain a docked panel with all the rest.
Set the top row's SizeType to AutoSize and the bottom row's to 100%.
When you want to hide the controls, set the top panel's Visible property to false. Now, because the top row is AutoSized it will shrink to nothing, causing the bottom row to "jump" up.

The TableLayoutPanel does the pushing. Maybe you can use that if there is no better answer in next time.

First problem:
You may use some simple panels to divide your form, give them the dock.fill property. when you'll hide a panel programmatically, the other panels will fill the empty space left.
Second problem:
You have to set the Autosize property to true.

Related

Win Forms Dock fill with gap

Fore note: I know you cannot use a margin with dock, but I am trying to figure a way around this.
I have two objects, a GroupBox (containing loads of buttons and stuff that will always be the same size no matter how big / small the form) and a WebBrowser. The former will take up roughly 100 pixels at the top, and the latter will take up the rest of the space. I have tried multiple ways to get around this, including Panels, GroupBoxs, changing Anchors and Docks, but nothing is working. I know there is a simple solution for this, but I cannot work it out. Could someone point me in the right direction for what I should be using?
P.S. New to WinForms so not very knowledgeable of things.
Start with a TableLayoutPanel control on your Form and set its Dock() property to Fill. Now change the ColumnCount() property to 1, and leave the RowCount() property at 2.
Add your GroupBox to the Top Row and adjust its size. Add your WebBrowser control to the Bottom Row and set its Dock() property to Fill.
Finally, select the TableLayoutPanel, find the Rows() property, and click on the "..." dots to the right. Select Row1 and change its Size Type to AutoSize.
Done!
Alternative Approach...
Add your GroupBox to the Form and set its Dock() property to Top. Add your WebBrowser control and set its Dock() property to Fill. Note with this approach, however, that the GroupBox will extend to fill the full width of the Form.

how to keep objects in place when window is resized in C#

How can I keep the objects of my window (buttons, labels, etc) in center when the window is resized?
Currently, I have 3 buttons in a Windows Form. When I maximize the window, the buttons stay at the top left section of the window. I want them to be in the center as they were when the window was not maximized.
Any help?
You should set the Anchor properties of the object to none,
This will keep it in the middle.
Set the Anchor property of your controls correctly. By default your control is anchored to Top,Left. If you clear this property (anchor to nothing, essentially), your button will remain centered.
(It may seem like you want to anchor to all four sides, but in reality what this will do is resize your button to fill the form!)
To keep your layout fixed and in the middle do this:
On your Form add TableLayoutPanel.
Set it's Dock property to Fill.
Create 3 rows and 3 columns.
Edit rows and columns - set 50% for first and last column and row.
Set fixed size for middle row and column.
Place Panel or anything else you like in 2nd row and 2nd column. It will always be in the middle.
If you are using the visual designer of Visual Studio (And you have no reason not to), the property of your control you seek to manage how they are placed inside a form is "Anchor".
By default, when you create a new control, it is set to "Top-Left", which mean they would stay in a fixed position to the top left of your form. You can change that to anchor them to anything.
You can also disable the anchors and control their position by overriding the Resize method of the form.

How can I add a bunch of labels to a C# form and then scroll the form vertically?

I have a form in C# (WinForm). It looks like this:
(LOGO)
blank space for labels that I add
through code (I can fit 10 labels in
this space)
(close button)
The blank space can hold about 10 labels.
I am stumped on how I would make this form scrollable if I want to add 20 labels? If I add 20 labels via code, then the 11th label will overlap with my close button and the 12th+ label(s) will run off the end of the form.
How do I make just the blank space portion of my form scrollable where I am creating the labels? I don't want to use a listbox.
Thanks.
You should try using either a TableLayoutPanel or a FlowLayoutPanel as a container for your Label controls.
A TableLayoutPanel will allow you a finer level of control over where your labels are positioned. Like an HTML table, you specify the exact cell position (using row and column coordinates) of each control.
By contrast, a FlowLayoutPanel will handle the positioning of its contents automatically, either in a vertical or horizontal layout configuration. The positioning is determined by the order in which you add the controls, allowing you to achieve a dynamic layout with a minimal amount of fuss.
Either will allow you to add your label controls to it at run-time and size itself appropriately. In order for layout panel to be scrollable, make sure that you set its AutoScroll property to "True".
Maybe a FlowLayoutPanel with AutoScroll set to true and FlowDirection set to TopDown.
Place all controls inside a panel and use scrollbar control.
Understand .NET Scrollbars
You could use a FlowLayoutPanel.
Add as many labels you need and enable AutoScroll on the FlowLayoutPanel.

c# winforms evenly distribute 3 controls

i have 3 custom controls, that i want to place in a resizable window next to each other. when the window is being resized, these controls should also resize, each taking 33% of the available width.
i tried to use a table layout, but it seems the table only grows, but never shrinks.
thanks for any help on this!
You can use a TableLayoutPanel, with 3 columns each of size "33% percent". Then you put each of your control in the panel, and set the anchor property as you wish (for example right-left if your control should resize themselves only on the horizontal plane).
Your TableLayoutPanel should also have its property Dock set to True so that he can occupy all your window and resize accordingly.

Multiple column scrolling, resizeable panel?

I have this:
Each list is its own WrapPanel and they are all on another WrapPanel which is in a ScrollViewer. If I don't set the height myself for the main WrapPanel it assumes I want the WrapPanel as high as it can go giving me only one column whereas I want as many columns as needed to fill the window.
If I set the Width and Height of the WrapPanel that holds everything to fixed numbers, but I want it to change when the user resizes the window.
In your example screen shot and description I see a tab control whose anchor is set to Top, Left, Bottom, and Right. The tab page with AutoScroll set to true. Within the tab page I see a FlowLayoutPanel. The FlowLayoutPanel has its AutoSize property set to true. I also see a set of other panels/user controls each of which contains a title and a series of check boxes.
You can`t achieve this with standard controls. You can try to create your own custom WrapPanel implementation. But, actually, looking at original WrapPanel sources I think this will be quite tricky. You see, what you want, is basically to measure how many columns can fit in the current window, while every element in column can be of any size. The way I see that algorithm, it will require N*N iterations to get the final result. So you may have problems with performance.

Categories

Resources