Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Collapsible GroupBox With Icon

2.40/5 (4 votes)
18 May 2009CPOL3 min read 48.2K   721  
A collapsible GroupBox with icon support.

CollapsedGrpBxWithIcon.JPG

Introduction

This GroupBox collapses and supports using multiple icons including embedded icons for each condition, either full size or collapsed. This is my first article on CodeProject, and I felt that with all of the wonderful contributors on this site providing examples of how to, that now it is my turn to give a small contribution.

This project was born from two projects that are on CodeProject:

With out these two authors, this article and this project would not exist. I will refer any arrangements of licensing back to the license as posted by them. Please read both of their articles and you will get a very good understanding of what is going on in the code. I do not feel that it should be repeated here - after all, I would simply be explaining what they have already explained. Besides, it would take the fun out of seeing what was actually done, and might also remove the learning, and since learning is the purpose here, that would not be a good thing.

Background

As I read the articles, I found that both had overridden the OnPaint method, but one thing lacked in the CollapsibleGroupBox which was that it could have a null icon image, or simply only one icon image: a plus or a minus. I wanted something that I could add more pizazz to. Fancy icons for full sized, collapsed, or even mouse over, mouse down etc.. I have embedded the icons as part of the resources so that they are easily packaged as standard icons. Although you can also add them through the property manager in Visual Studio, they have their defaults. This is one short coming you will need to fix - I always default - if you want icon only and no text, or no icon and text only, that is up to you to supply the properties and the logic to disable or enable text/icons on the GroupBox. In reality, this could even be made into an image across the top and not just an icon, making the control much fancier.

Using the code

The code in this article is in C#. If you want it in VB NET, please use one of the free online converters and let it teach you about C# and or VB.NET. That is how I learned C#. Sorry I am not a spoon feeder.

In order to use the attached code, you must create a new project file called CollapsibleGroupBox. Then, add the two code files to the project, and additionally, add a resources file named resources with the embedded icons aptly named plus, minus, and standard. Compile the project. Add the Custom Control to your toolbox and then drag to a Form - you should be good to go.

Points of interest

I learned a little bit about the OnPaint method, the Rectangle method, and also about getting objects from embedded resources. It was fun to modify the original code and combine what I felt was the best of the two GroupBoxes into one, just a little niftier.

History

This is the first release. When I get more time, I will try to release the whole project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)