C# Winforms Resizing and Controls - c#

Say I have a Winform with 3 adjacent Textboxes, all within a Panel that covers the majority of the form. How can I configure it so that when I resize the form, the controls also resize, but do not bleed into each other? I've tried the different anchoring options, however the textboxes always tend to run into each other?
It's probably something really simple that i've missed.
Thanks.

One easy way to accomplish this is to put the three textboxes into a TableLayoutPanel that has 1 row and three columns.
Settings:
Set the width of each column to 33%
Set the anchor properties of the TableLayoutPanel to Top, Left, Right (or similar).
Set the Dock property of each TextBox to Fill.

Have a look at the TableLayoutPanel - you should be able to get the sort of behaviour you want using one of those with 3 columns and a text box in each one.

Related

Visual studio, fill space

In my form I put three charts. I want to fill all available space, and tried to set dock fill, but in this case the charts overlaps each other. I want, instead, to have every chart next to others.
How can I fill all the space, without overlaps?
As mentioned in the comments you want to use a FlowLayoutPanel or TableLayoutPanel. Below are two examples of TableLayoutPanel which may be better because you can more easily control the desired layout.
The examples below use panels instead of grids but the idea is the same for whatever control you want to put inside the TableLayoutPanel.
In both examples, the Dock property of the TableLayoutPanel and all 3 components is set to Fill. This will cause everything to resize automatically as the form resized. Additionally, there is a Rows property and Columns property on the TableLayoutPanel which will allow you to set either pixels are percentages of the table a cell should consume.
Example 1: 3 panels side by side
Example 2: 2 panels above a third panel. In this case you set the ColumnSpan property to 2 on Panel 3

Make DataGridView stretch to middle of WinForms form?

I have a WinForm form that has two DataGridView controls paced on it such that they are stacked, one above the other against the right hand side of the form.
I would like a way of setting them so that when I expand the form, they expand height-wise with it, as well as width-wise. I managed width-wise by anchoring them to the left and right sides and anchoring the top one to the top and the bottom one to the bottom. However, from here I'm not sure how to get them to use up the space in the middle that appears when the form maximizes...Maybe an image will make my meaning clearer:
Normal Size:
Maxmized; I'd like the grids to expand to take up the full height of the form between the two of them as the red arrows show:
If this question is blindingly obvious I apologise and can only say I didn't really know how to phrase it properly and so found searching for it on Google unhelpful!
You have two options:
TableLayoutPanel or
SplitContainer
The former lets you create a table of many columns and/or rows with various sizing options from absolute and percent to autosize. This is very powerful for layout; but in other respects TLPs are somewhat restricted as the 'cells' are only virtual..
A SplitContainer offers only two panes but lets you treat each with all the things you can do to a container: add one or more controls, anchor or dock them, give each pane a BackColor and make use of its event model.
So if you need just two controls of equal size that adapt to the form size like you showed in the question, a SplitContainer is maybe the better option.
Set the splitter to fixed and make it smaller, anchor the SplitContainer to all sides and drop the DGVs into their panes and Dock them to Fill.
You could also make the splitter moveable to allow the user to resize the panes; if you do that do make the splitter width larger..
Also make sure that the FixedPanel is set to None so that height changes are shared.
Hint: If you want a few more panes to share the space you can nest several SplitContainers.. But for larger numbers do consider switching to TLP!

How to place two DatagridView vertically in Winform?

I would like to place two DataGridViews (Auto Binding ) vertically along with their labels in Winform. So it should fit with in area and look nice.
It should display on following order:
DataGridView Label1
DataGridView1
DataGridView Label2
DataGridView2
It should adjust Scroll and maximize behavior automatically. I have tried with Panels and FlowLayoutPanel but was not successful. I know how to do in WPF with relative layout, but no clue how to do in winform.
The problem seems to be mainly keeping the sizes of the two DGVs in synch.
For this you can use a SplitContainer, which will do that automatically:
Set Orientation = Orientation.Horizontal
Set IsSplitterFixed = true
Set FixedPanel to None
Set its Anchors to your liking, maybe to all four sides
Put your two DataGridViews in the two SplitContainer.Panels
The both DGVs to Dock=Fill
Now both DGVs will stay at the same size, sharing the SplitContainer size equally, or to be precise with the original ratio.
If you want Labels to sit above each DGV, simply put them in place but instad of Dock=Fill choose four Anchors for the DGVs.
If you want your Buttons to stay under the SplitContainer simple Anchoring will probably do..
Note that by nesting more such SplitContainers you can keep three or more Controls synched with the same sizes..
I solved the problem using TableLayoutPanel control.
Created four rows:
Row1: SizeType:Absolute value:20
Row2:SizeType:AutoSize
Row3: SizeType:Absolute value:20
Row4:SizeType:AutoSize
And
DatagridView1.AutoSize = true;
DatagridView2.AutoSize = true;
tableLayoutPanel1.AutoScroll=true;
Now, it adjust Scroll and maximize behavior automatically as per different size of DataGridViewRows
Thanks LarsTech and TaW and others for your support !!

How to enable components to resize according to the change in the form size?

I made a simple button based form for a particular resolution, say, 800*480.
I want the buttons to automatically resize themselves when used on a higher resolution.
I have six buttons of equal size placed as shown and I have used the following anchor properties
[TOP,LEFT] [TOP,RIGHT]
[LEFT] [RIGHT]
[BOTTOM,LEFT] [BOTTOM,RIGHT]
I want the buttons to increase their size as well. On the higher resolution, the screen looks empty as all the buttons shift toward the periphery of the screen
If I use the following config, the buttons overlap each other
[TOP,LEFT,RIGHT] [TOP,RIGHT,LEFT]
[LEFT,RIGHT] [RIGHT,LEFT]
[BOTTOM,LEFT,RIGHT] [BOTTOM,LEFT,RIGHT]
What should I do?
I am using Visual C#
Add a TableLayoutPanel to your form, with 3 rows and 2 columns.
Set Dock property to Fill
Put each of your buttons in a cell of the TableLayoutPanel and set their Dock property to Fill
You need to set all of your buttons' "Anchor" properties to Top, Bottom, Left, Right.
This keeps them in place, but also resizes them. Make sure the buttons' "AutoSize"-property is set to false.

TableLayoutPanel slow because of autosize and/or nested TableLayoutPanels?

I have some TableLayoutPanel, where the first "layer" has 1 column and ten rows, some of this rows contain either a UserControl or another TableLayoutPanel with 2 or 3 columns and some rows. One or two of them contain another TableLayoutPanel, but that's it. So that's a maximum of 3 "levels" of nested TableLayoutPanels. Most of these are set to autosize, because some UserControls might change their size. When a form containing such a nested TableLayoutPanel, the UserControls "flicker", it looks like they are loading very slowly.
Do I use too much autosizing?
Or is my Panel too nested?
I don't think the flicker has to do anything with 'auto-sizing' or 'nested panels'.
Please refer another 'S-O' link : How to avoid flickering in TableLayoutPanel in c#.net
Suspend the layout until you've added all your controls on.
TableLayoutPanel panel = new TabelLayoutPanel();
panel.SuspendLayout();
// NOW add controls (including nested-controls) -- do autosizing etc
panel.ResumeLayout();
Also look at using Double Buffering. You'll have to create a sub-class of the TableLayoutPanel. See an example.
Hope this helps.

Categories