c# - How to arrange elements in WindowsForm like Windows Explorer - c#

I'm populating a self-made Windows Explorer which simulate the Microsoft Windows Explorer. This is the layout of my Windows Form:
What I want it to be is:
But when I maximized the windows, it looks like this:
The problem is that the treeView's width got increased, too. How to stop that ?
Any ideas? I've tried many ways but nothing works.

Try to add a second splitcontainer around the groupbox and the splitcontainer you already have. Then set the panel at the bottom as the fixed panel.

You can set the SplitContainer.FixedPanel property to disable automatic resizing of one of the panels. This way the specified panel will remain the same width/height even when the container itself is resized.

You could use Anchoring (each control will have that in the Properties menu), and select what you want it "anchored" to. That should allow your controls to stetch to fit the form.
It can be fiddly, so you have to anchor each control and think about how to wish to set it. EG to make a control extend if you increase the height of your form, click the top and bottom anchors.

Related

WinForms UserControl gets cut off when running

I'm having a lot of difficulty with WinForms and sizing of user controls. We've got a DataGrid, with some buttons underneath it, in a custom User Control.
Everything is anchored correctly. As in, when you resize the control, the buttons stay anchored to their proper sides, and the data grid fills the rest.
It even works when adding it to our main form:
Then, when we run the app, it gets cut off:
I've tried:
Docking
Anchoring
Changing the AutoSize property of the control, and the control as used in the main form
But nothing is getting it to act like it does in the designer.
Any help is really appreciated.
Use Split Container and put dataGridView on top and bottom insert your buttons and set Panel that is in bottom Fixed.
Another option is to use TableLayoutPanel
More information :
Designing the Layout of Windows Forms using a TableLayoutPanel, with auto-expand panels
There were obvious issues with the WinForms app, WRT scaling and fonts. We switched over to WPF and everything is working as expected.

Resizing a panel c# win forms

If you imagine a win form with a line drawn vertically down the middle. On the left i have a graph, and when you click the graph certain forms open on the right in an mdi type panel.
I am trying to figure out how to logically get this to look like a proper application should but am failing!
The whole form loads in a maximised view. I first set the panel width to 0 then when i add a form i check if the panels width is less than the forms, if it is then change the panels width to that of the forms.
This doesn't look great tho tbh, resizing makes strange things happen and i see a lot of grey. Does anyone have any ideas?
Use a SplitContainer on your main form. Ensure it's Dock property is set to Fill
Put your graph stuff on the left panel, and your other stuff on the right side.

How do I make multiple controls in a Windows Form automatically resize with the window?

I'm new to Windows Forms in Visual Studio, and I am wondering how to automaticly resize controls to the window size.
Say, I have 2 controls in a panel, a List Box and a Button. I want the button to dock to the bottom, and I want the List Box to fit the rest of the space. when the window resizes, the button should be at the bottom (as expected with docking), and the list box should stretch down to the button.
Is there a way to do this without any code?
Thanks.
Dock is pretty easy to use, but I recommend using the Anchor properties instead. Resize your form to a reasonable size in the Designer. Then, place your controls to look the way you want. Then, decide which controls should resize with the form and set the Anchor property as follows:
If you want the control to resize with the form in width, set the Right anchor.
If you want to resize height, set the Bottom anchor.
If you want the control to stay right when the form resizes, unset the Left anchor.
If you want the control to stay bottom when the form resizes, unset the Top anchor.
The problem I have with Docks is that they sometimes act funny when controls are not declared in a specific order, and to get the effect you want, sometimes you have to create extraneous panels just to hold controls.
It really gets messy when you want to maintain the aspect ratio of each control. One way, which is not really up to the mark if you want to get into fixing the details, is to use TableLayoutPanel and use Dock and Anchor wisely to achieve what you want.
Use the dock and fill options on the controls. Look under properties for each object, and containers if they are in any.
You can use SplitContainer
Google for examples. Here is one
Try setting your ListBox's Dock property to Fill.
You'll need to watch for one thing though: by default the ListBox will size itself to display whole list items. If you resize the control so that it displays a partial item it will adjust itself so it will display a complete item. This can make the control appear to lose its 'Dock'ing behavior. The solution for this is to set the ListBox's IntegralHeight property to false, which specifies that the control not resize itself to fit complete items.

Resizing componenets on a c# form

typically in java if you have a layout manager of somesort, when you resize the page then the components in that panel will resize accordingly. I think my app is missing some sort of layout manager to control resizing as at the moment everything is just static
Is there a way to make it resize on the changing of the form size? say the user makes the page bigger, then the componenets adjust and so on.
Thanks
.NET has layout managers as well.
Personally, I prefer the TableLayoutPanel for my WinForms apps.
Once you layout the Table (using a combination of static/dynamic sized rows/columns) you add your child controls to the table cells. Once you add your controls, you can dock or anchor the controls to the cell so that they are automatically adjusted when the window is re-sized.
Two main options:
Anchoring. Set your control to "anchor" to the sides of your form. This means that if the form resizes, the control will stay a constant distance from that side. So, if you anchor Top, Left and Right, then your control will stay in the same position, but resize horizontally with the width of the form. Play with it. It'll be obvious.
Docking. Set your control to "dock" to a side of the form, or the center. This is usually done with containers, and it will make the widget take up that entire portion of the form no matter how large it gets.
In Windows Forms you make use of the Control.Anchor property, which will cause the control to adjust accordingly when the window resizes.
To do this with windows forms you use the Anchor and Dock properties of the control
See this for a guide on how to use them

C#: Anchored Panel won't resize properly

I've a strange problem regarding auto sizing of a panel in a user control. This panel is anchored to all 4 sides of the user control.
But the anchoring not always works as it should:
If the user control is resized by resizing the form, the right and bottom anchors don't work. But if the control is resized by using a splitter which is in the form, it works correctly.
All other controls which are also affected by resizing work correctly, just this panel won't do.
I can't see the difference between resizing through a splitter and through a form.
Setting the size manually in the OnResize of the UserControl only works if the resizing is done by moving the splitter. If the resizing occurs by resizing the form, the heigth and the width of the panel won't change to the new values.
Edit: To make it more clear: The user control is added to a TabPage and set to Dock=DockType.Fill.
Edit2: To make it more complicated: This is the hierarchy the control is used:
The Form contains a splitter with 2 controls (A and B). (B) contains a user control (C) with a splitContainer. The split container->panel1 in (C) contains a tabcontrol (D). This tabcontrol (D) contains a user control (E) (in a tabpage) . This user control (E) has a splitcontainer (F) in which another usercontrol with a tabControl (G) is placed. This tabcontrol (G) has tabpages, and in this pages is the described user control (H) placed.
If I place the control (H) directly to the panel in the splitcontainer (C) everything works fine. Since all controls which are added to a splitcontainer or a tabpage are set to docktype=fill, I cannot see why the last control has this problems.
Is there any limitation in the Windows Forms framework which limits the amount of child controls?
Edit: I have uploaded a TestApplication with which you can reproduce this issue.
http://rapidshare.de/files/49092516/TestApplication.zip.html
It occurs only under Vista. Win XP and 7 work properly...
If you resize the form, the textBox with the "..." button won't resize, but if you resize via the vertical splitContainer, it works.
There is a bug with deep nesting where the kernel runs out of stack space causing resizing, docking and anchoring to fail silently.
See https://web.archive.org/web/20140818095718/http://support.microsoft.com/kb/953934
You have talked about both "anchor" and "dock", which can serve similar purposes but act differently. Your problem may be that you are trying to do both, and this is interfering with the behavior you want. If not, perhaps try switching from the one to the other.
It sounds like you've double checked the ansectery of the control, but did you examine it with: View->Other Windows->Document Outline (a very handy tool I only recently discovered)?
Finally, while it doesn't directly solve your problem, it sounds like you have a very complicated UI, and your users may benefit if you were to spend some time to consider if you could simplify the UI. Of course, I don't know your users or the purpose of the app, and what you have may be the best, but it sounds like it might be time to ask this question.
Good luck.
The problem is not the depth of nesting; controls can be nested arbitrarily deep. Anchoring and Docking generally do not play nicely with each other. I usually favor docking over anchoring, but, whichever solution you choose, you should probably avoid mixing them.

Categories