How to create collapsible panels as custom controls in Winforms? - c#

The closest I can think of using is the GroupBox control which has a custom drawing at the top identifying the panel.
Is it doable? I have done independent custom controls like buttons, sliders, color pickers, but not totally sure if this sort of drawing can be done on a GroupBox?
I would need the grouping of a GroupBox or a TabPanel.
Are there any alternatives on the net that I can use?
Just exploring the alternatives before I start doing it.

A technique we used was to create multiple panels docked to the top of a frame and set their height to zero. Then when we wanted to display a panel we set its height correctly. Winforms automatically reflows other panels to accommodate the new one.
You can also create a recurring timer to trigger every few hundred milliseconds that increases the panel's height by a few pixels until it reaches full size. This creates a nice animation of the panel expanding into place.

See how others have done this. Code Project's got a bunch. Try searching there.
I used this one a while ago and liked it:
Windows XP style Collapsible Panel Bar

Why not just create a composite control? Something to the effect of.. 2 panels or a label and a panel or whatever suits your needs. Drawing it wouldn't be all that difficult but seems overly complicated for your desired functionality. Am I completely missing the mark?

Related

Best Way to make a Windows Forms scalable?

What would be the best way to make a WinForms application fully scalable, for example when the Form resizes?
In WPF i would use something like a Viewbox and/or a UniformGrid, but something like this doesn't exists in WinForms.
Is there an easier (and maybe faster) way to rescale controls on a from after resizing it, instead of resizing them all by calculating their new Size/Location etc.?
Thanks in advance
In Windows Forms, you use the Anchor and Dock properties for each control.
Here's an article about using them: http://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/
You should also look at FlowLayoutPanel and TableLayoutPanel
you can use anchor and dock, depending on your need:
Anchor - the edges of the container to which a control is bound and determines how a control is resized with its parent.
Dock - which control borders are docked to its parent control and determines how a control is resized with its parent.
for further read: Dock and Anchor
Have a look at the Anchor property found on pretty much any control. This allows you to lock a control to any (or all) of the four borders of a window.
Once one distance is anchored (e.g. Top or Right), the control will always try to keep that distance, no matter how you resize your window.
For example, you'd set Anchor to Bottom and Right for a button that is supposed to always stay in the bottom right corner of a window. A text box, that should always fill the window from left to right would use Left and Right.
Similar things can be achieved utilizing Dock, but a docked control will always try to fill as much space as possible (there are different strategies available, like "fill everything from here and upwards) based on its container. Depending on your use case, this can however be a lot harder to control (and I usually only use it if I want a single control to fill a full window, e.g. a TextBox).
If you need more complex alignment, like widths scaled on some kind of ratio (e.g. 30%), then there are several different containers available.

C#: How to draw very large controls in a scrollable, zoomable panel?

Imagine a bar graph with horizontal bars that may be very wide. I have a Panel on a Form where I want to display these bars and scroll and zoom them. The Form, and thus the Panel, can be resized. The bars are dynamically created from a database. Each time the user zooms in or out, all bars have to be created anew to adjust their sizes on the Panel.
I use Label controls to create these bars, but the problem applies to all other controls as well: If I zoom in far enough, my bars will eventually exceed the magic 16 bit border of control sizes (>65536 pixels). This makes it impossible to simply create all the controls on the panel at start and let the panel handle the scrolling.
My idea: Clear the Panel of all bar controls and create only the ones that are visible in the current view window, according to the current position of the scroll bars and the zoom level. The bars exceeding far from the visible view will be cut short just outside the Panel, so their maximum size is limited by the Panel size.
My questions:
At which Panel event(s) should this clear/create process take place best?
There could be thousands of controls, so it should be as seldom as possible.
Is there a better way to handle this? Maybe I got it all wrong from the start.
This problem arises not only with huge controls but also when smaller controls are very far apart (>65536 pixels) on a Panel, so I think a good solution may be helpful for many projects.
I wouldn't like to have to create / destroy controls, or hide / resize controls just for their click events. It's quite easy to create a UserControl and override the OnPaint method to draw the bars, and override the OnClick or OnMouseXxx events.
Since you already know the positions of the bars in "virtual space", it's easy to map the location of the mouse cursor to a bar (or a click outside a bar).
I know you said winforms is Mandatory, but I really think you should look into the wpf viewbox. You can host a wpf element in winforms. So everything else can be forms related, and you have a panel that hosts and displays your controls. I could write up a quick example that might demonstrate this for you, but if you have no intent of going this way I really don't want to waste my time.
You could create a metafile (vector graphics), show that in an image control, and manually determine which logical element is clicked.

Trouble using Windows forms to design GUI

I'm trying to write a simple GUI that renders a number of images using the Graphics' object primitives. What I want to have is a series of areas that I can paint to in isolation of the other areas, so that each painting "canvas" has it's own origin within the global coordinate frame of the top-level form.
So far I have tried adding several panels to a FlowLayoutPanel. However, they seem to be getting placed one on top of the other, as only one onPaint method is being called. I can override the Form's onPaint to invalidate the other panels, which are then painted, but not displayed.
Besides setting the sizes, and initialising the FlowLayoutPanel, is there something I'm missing? Is there a better way of doing this?
Code: http://pastebin.com/30Uf9AGF
based on the names of your classes, it looks like you are designing a game ... maybe you want to take a look at Microsofts XNA framework?
however, the problem with the code you provided is, your layoutPanel is not sized correctly, therefore its child-controls are not visible on the main form... since painting is only done for visible items ... there is no painting for most of your FloorDrawPanels ...
try changing the size of your layoutPanel or setting its dock mode to fill
You don't set the size of the FlowLayoutPanel. It will default to 200 x 100 with a Margin of 3. You fill it with controls that are 100 x 100. Given the margin, only one of those controls can ever be visible at the same time. It is therefore no surprise that you only ever get one paint event, Windows only asks visible controls to paint themselves.
Not quite sure what was intended, start by making the FLP bigger. And set its AutoScroll property to true so that the user can scroll the other controls into view. Using the designer would have been a quick way to find this out btw.

scroll bar TableLayoutPanel c#

I have a TableLayoutPanel that has several TableLayoutPanels inside it. The amount changes dynamically and will almost always be too many to be able to fit inside the form.I need it to have a scroll bar so I can view the entire component.
I have tried setting the autoscroll property on the main panel to true and docking it and/or setting a maximum size. What the controler does instead, is to try and fit ALL of the Panel inside the form therefore changing the size of its inside components and squeezing them all together instead of creating a scroll bar to scroll through my Panel.
Do you guys know what I might be doing wrong?
Thanks.
Jose
PS: I am using VS 2010
I had the same issue one day and found out that the problem was that I had a "MinimumSize" set to the TableLayoutPanel. That caused the control to keep a minimum height no matter what the Dock and/or Anchor constraints, preventing the AutoScroll feature to work properly. Since that feature is based on a simple check on the Y coordinates of all children controls of a control against that control's height, the scrollbar was not appearing despite the fact the the TableLayoutPanel's child controls were "disapearing" out of sight due to its parent control clip area.
So in other words, check the MinimumSize property of TableLayoutPanel and make sure it's empty.
Maybe this will help.
if it still doesn't work, try put a panel and then put the tableLayoutPanel into that panel. Set to true the autoScroll property of the panel.
I had the same thing happen on my project. All my controls were squished inside the TableLayoutPanel. In my case, I was rendering many of the controls as ColumnStyle.Percent. Switching this to ColumnStyle.Autosize was the fix I needed.
I assume you've set these properties as well, but just in case my TableLayoutPanels also use the following settings:
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
AutoScroll = true;
Late answer, but I've been fighting with a complex layout recently and was looking for a solution to get my scrolling working on a screen with far too many fields. Honestly, better design should avoid this problem 99% of the time but sometimes your hands are just tied.
The Problem
The problem seems to be that if you're nested too deeply with multiple grids, groups, and panels they stop reporting properly to parent controls that their contents have overflown the size of the current screen. Actually, the problem is probably that the controls are all set to dock fill and do fit within their parent control, so the top level control doesn't know that the contents of its great-great-great-great-grandchild controls don't fit.
Solution
The trick to fix this seems to be manually forcing the top most panel to scroll at a certain minimum size:
MainPanel.AutoSize = true;
MainPanel.AutoScrollMinSize = new Size(400, 500);
TableLayoutPanel scrolling is full of bugs.
The solution to make it work correctly is here.

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

Categories