How to make ListBoxes in SplitContainers resize properly? - c#

To reproduce this problem,
Create a new WinForms project
Using the WinForms designer, add a SplitContainer
Set its orientation to horizontal
Set its dock to fill
In each of the containers of the SplitContainer, add a ListBox
Set its dock to fill
Increase its font size so that the behaviour in question can be observed more clearly
Add a few items to the list boxes
After those steps, the designer should look like this:
Now run the program
Resize the containers in the SplitContainer
You should see that with some sizes of the upper container, there will be a space between the two list boxes (sorry for my bad mouse-writing).
For other sizes of the upper container, there is little to no space.
I hypothesized that this is because list boxes can't show "half an item" so it reduces its size to not show that half of an item. Is this true?
I have thought of the following solution:
Restricting the resizability of the split container so the user can only resize in "steps" where each step is equal to the height of one item in the list box.
However, I don't quite like this because when the item height is large, the user experience feels unnatural (at least to me).
How can I make it so that the list boxes resize properly (i.e. leaving no gaps)?

You can set the IntegralHeight property to false to achieve the desired effect.
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.integralheight(v=vs.110).aspx
Gets or sets a value indicating whether the control should resize to avoid showing partial items.

Related

How to view inside of a panel control in C# windows form

I am trying to place a panel that I want to have multiple textboxes inside of. I want this panel to fit inside the form and the panel to have a scrollbar. I have autoscroll on and its working as intended, but I want to be able to add controls below the size of the panel. Is there a way I can "full screen" the panel and place items in it or even make the scrollbar a set length instead of just the amount needed to fit the contents. I want to be able to place things underneath what I have there.
Dragging items onto the panel doesn't allow enough room to make things tidy
As an option, you can set the AutoScrollMinSize, in your case, the height of it to a larger value like 1000 temporarily, which sets the virtual (Scrillable) height of the control to 1000. The property determines the minimum size of scrollable area.
As another option, you can AutoScrollMargin, in your case, the height of it, to something like 500 temporarily, which provides an extra 500 pixels space at bottom of the panel. The property determines the minimum margin that should be between the edge of the child control and the edge of the scrollable control.
Then when you are done with the design, just right click on the property and Reset the value of it, to let the control calculate the scroll size.
And I assume you are aware of some obvious workarounds like:
Design the form (including the panel) in a larger size, setting proper dock and and anchor properties for controls, later set the size of form to desired size at run-time, or at design time (after you are done with the initial designs).
Or another workaround, could be just dropping the controls in the panel, and then selecting them and moving them using arrow keys.

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!

Resize components inside user control while maximiziing

I am using c# and i am creating a simple design where i have a user control and some components inside it like treeviews and buttons. I am trying to fill a treeview with some nodes and drag-drop these nodes to other treeview and use the buttons to also copy nodes from side to another.
The problem i am having is that when i maximize the window containing this user control there is no effect on the inside components.
I have set the Dock property of the user control to Fill.
I have changed the anchor properties of the buttons and treeview inside the user control but the behavior wasn't as expected. For example i have tried to set the anchor property for the right treeview to be Top,Bottom,Left => and the result was a disaster
I have also done a lot of combinations for the anchor property of all the buttons but nothing gave me the right behavior. I just only need to maximize the window form and the controls will be maximized with the same proportion.
It sounds like you want a "3 column" interface where you have a TreeView on either side and Buttons in the middle to allow movement between the two. Assuming this is correct, you can accomplish your automatic resizing by using a TableLayout.
Essentially, it would be like this:
Add a TableLayout and edit the rows/columns such that there is a single row with 3 columns:
The first and last column would be sized at 50% (and would hold your TreeViews).
The middle would be an absolute size of (for example) 120. This would hold your Buttons.
Set the properties of this new TableLayout to Dock -> Fill the form. This will size the entire table to grow with your form.
Add your TreeView controls to the left/right columns and set them to Dock -> Fill the respective columns. Since these columns are dynamically sized, they will grow with the form.
In your middle column, add a Panel and set it to Dock -> Fill. We add a Panel here to hold the multiple Buttons you use for movement. This Panel will not grow in size because the middle column is sized absolutely.
Add your Buttons to the middle Panel.
Without a screenshot, I'm not completely sure what you are trying to achieve but I believe this is along the lines of it. The nice thing about this setup is there is zero code involved.

Hide a groupbox and also remove the space where it was present in winform

In my winform, I have three group boxes, based on selection of the
items in a combobox the second group box(at the center)
is hidden using groupbox.visible property.
The problem is when the second goupbox is hidden, there
seems to be an empty space in the area of the hidden group box,
I want to move the third group box to the place where second
group box is present. Can I use any other control instead of group box?
You could manually set Location and Size properties of your third groupbox or (I think better) set Dock property of both groupboxes to Top, so when your second gb becomes not visible, the third should scroll up to occupy free space.
You have a small number of options, assuming you are using just the default VS controls.
If you place these group boxes in a flow layout panel, when you make it invisible the layout panel will move other controls into it's previously occupied space.
If you dock the group boxes, making one invisible causes the others to occupy the space if the docking dictates.
If you cannot use layout mechanisms or controls, your only option is to modify the location or size of neighbouring controls to fill the space manually.
I very much advise trying to use the layout containers, or find a container online that handles the positioning for you. Position code buried in the form using potentially lots of "magic" numbers is definitely not maintainable.
you should solve it by chaning control that you're groupboxes are in. Their position can't be set to constants. The other solution is to change position of third groupbox when you change visibility of the second one.
A FlowLayoutPanel should work http://msdn.microsoft.com/en-us/library/system.windows.forms.flowlayoutpanel.aspx

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.

Categories