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.
Related
I created a Form with fixed size, containing a fixed sized TableLayoutPanel. Controls are anchored to the TableLayoutPanel using the Anchor property. However controls are cropped after moving from Desktop to laptop.
I have tried setting MinimumSize, AutoSize and AutoSacling in Form and TableLayoutPanel, but controls are still cropped.
Suggestions?
You should try using Dock property of TableLayoutPanel.
Change its value to fill (Dock = Fill), this way your TableLayoutPanel will be drawn within the form border.
Another suggestion is, you should divide your main tablelayouttable like a grid and put one control inside its individual cell. Set their Dock property to Fill and you will see the result.
Hope it helps. Good Luck.
i'm currently modifying an existing C# WinForm project. I try to arrange some controls inside a GroupBox. However on runtime, they seem to be aligned differently and the Groupbox has a lot more space. Is there some option checked or is this the standard behavior? Any hints are highly appreciated! Thanks!
Here is what it looks like, as you can see there is no way except trial and error to arrange the checkboxes. On runtime there is easily enough space to have four colums in one row, in designer i can hardly fit three without having them overlap.
To prevent the groupbox to resize according to its content, you should make GroupBox.AutoSize to false.
GroupBox groupBox = new GroupBox();
groupBox.AutoSize= false;
check to Size property, and check if someone change it, for the checkbox Location to stay the same in different sizes of the form use anchor property and set it to left or left top.
it's basically must be the size, if the autoSize is off. check what's the starting size, and the size after the form is shown. it shouldn't be the same, but if it is, you can set it to smaller
Check the Anchor property on the checkboxes. Looks like some of them might be anchored to the right.
Not sure it applies to these checkboxes, but AutoScaleMode can impact the scaling of a form. Set to None to ensure it doens't get scaled.
hii
I am a fresher in the c # so i want to know how to resize the datagrid(any other control)aith respect to the form size.
I just added one datagrid in the form then what i have to do?please help me...from the very basic please
For a dynamic layout that scales with your form size you have various options (depending on the complexity of your layout).
The first ones are
Anchoring
Docking.
But you can also work with advanced container controls like
TableLayoutPanel
FlowLayoutPanel
SplitContainer.
Some more informations i already post in an older question.
If you have more concrete problems about how to solve a specific layout problem you should post a new question with your exact problem.
But at a first tip i can say that it is never necessary to use the Resize event and do some size changes on yourself. There is always a solution that can be solved with the above elements.
You have to ways of dong it:
Using a Dock. It works fine and is very easy to use but its limit is that you can stick it to a one side of parent container. So if you want to streach control only in width you will fail.
Using Anchor. It require more configuration but you can specify all four(top, right, down, bottom option separably.
Regards
Szymon
Go to the properties window and scroll down to "Docking" and choose to dock the control in the parent container. This will give you various options about how you want the control to dock. You should put some containers in there of some sort, maybe, if you've got buttons or things you'd like to have show up above/below/next to the grid. Also, if your grid goes behind your other controls, select the control and bring it to the front.
edit:
You need some containers in which to put your buttons / drop-downs. You could use a flow-layout panel (which wouldn't resize its child controls), or you could use a plain panel or the table-layout panel. The table one will let you dock your child controls within each cell of the table, and you can set your columns & rows to auto-size to a percentage of the entire table width. That way everything will autosize accordingly.
I'm developing an app for Windows Mobile 5.0 and above, with C# and .NET Compact Framework 2.0 SP2.
I have a WinForm with two panels inside (upperPanel and bottomPanel). I want that upperPanel always fill 2/3 of form's height, and bottomPanel fills 1/3 of form's height. Both panels will fill completly form's width.
I've used this:
upperPanel.Dock = Fill;
bottomPanel.Dock = Bottom;
But upperPanel fills the form completly.
How can I do this? I want, more o less, the same gui on differents form factors and on landscape or protrait mode.
Thank you.
What you need to do is to put the bottom panel on first and set its Dock property to Bottom. Then set the panel's height to be 1/3 of the form's height. Finally, add a second panel and set its Dock property to Fill. The key here is that you want to add the control that will fill the remaining area to be added last. Alternatively, you can play around with the Bring to Front and Send to Back commands in Visual Studio to get the designer to cooperate.
You may also need to hook the OnSizeChanged event for the form and re-set the height of the bottom panel to account for layout changes. It's been a little while since I did compact framework programming, so I'm not sure.
Right click on the upperPanel and select Bring To Front. However, I don't think this will give you the result you want. When you resize, the bottom panel will remain the same height, while the upper panel will stretch to fill the form.
Using your docking settings, with this code might do the trick:
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.bottomPanel.Height = Convert.ToInt32((double)this.Height / 3.0);
}
Set both panels to "not anchored". That is: Remove Dock-Value and clear the Anchor property. Then, move the controls so they are sized the way you'd like them to be sized.
After that, upon resizing the form, they should resize relatively.
EDIT
Oops, just tried it and sure it doesn't work. I mixed this up with a solution that automatically keeps controls centered within the window...
Well, I'd guess you then have to create a handler for the form's Resize event and manually align the controls after the form has been resized.
Go to Tools, Other Windows, Document Outline. Find the two panels, and swap the order of them. The control that has DockStyle.Fill has to come first for it to be docked correctly. (or last.. never sure which one it is, but it is one of them :p)
This won't solve the always 1/3 and 2/3 issue though... cause the bottom panel will have a fixed height (unless I am mistaken). I think maybe the TableLayoutPanel supports this though...
Update: As noted in the comments, that panel doesn't exist in the compact framework. So, I suppose the easiest solution to this problem would then try to use the docking, but update the height of the bottom panel whenever the size of the form changes.
If you want this to work perfectly you'll need to add some code to the Resize event of the Form which then specifically works out the relative sizes and places the controls in the correct place after a resize.
If you're not worried about losing precision and the forms aren't going to move much you can avoid this by using some relatively smart anchoring. Essentially you're going to have to select a "grower" (the part of the form that gets bigger, the bigger the form gets). In this scenario I would probably anchor the top part to Top | Left | Right and the bottom part to Top | Left | Right | Bottom. This would mean that the lower part of the form will get bigger if the form is expanded. In most cases this is acceptable. If it isn't use the Resize event and some code.
The easiest way to do this is to nest panels. Just set up panels for top bottom and fill. Then use panels within those panels to do the same. The only issues I've had therein are datagrid resizing, which is always a pain anyway. in that case, you have to use some code to resize the datagrid control on the form resize event.
I would like to add a point to #jasonh answer.
For the panel that occupies 2/3 of the form, you will have to set the AutoScroll property of the panel to true.
This will enable the panel to display scroll when the control size exceed the visibility to the user and also ensure the visibility of the smaller panel which is 1/3 of the forms height.
You can get the required design by using nested panels along with few setting with Anchoring and Docking Properties.Follow the following steps:
1) Add the Form and put a Panel1 on it. Set its Dock Property as 'Fill' and ResizeMode as 'Grow&Shrink'.
2) Add Second panel2 and set its Dock Property to 'Bottom', Set the Height and set the Anchor property to 'Top,Left'.
3)Add Third panel and set its Dock Property to 'None', Set the Height and set the Anchor property to 'Top,Bottom,Left,Right'.
Save and Compile. Now all the panels Would maintain their relative Positioning With resizing.
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.