looks like customcontrol gets double drawed

Richnl

Well-known member
Joined
Mar 20, 2007
Messages
93
Programming Experience
Beginner
I have a collapsible groupbox, and
It only looks strange when I add it to a form from the toolbox

There are double borders on the left and right side.
This is different from what it looked like in
the testcontainer, everything looked ok then.

Does anyone have any experience with this?
I have put it in a zipfile

Thanks in adv, Richard
 

Attachments

  • CollapsibleGroupBox.zip
    16.1 KB · Views: 12
Last edited by a moderator:
You are inheriting the GroupBox control and also including a groupbox as one of it's child controls. You should decide to either inherit from the base control called "Control" and create your own specialized groupbox or inherit the groupbox control and modify its behavior. The reason you see double borders is because the control itself is a group box (Inherit GroupBox, this is the outside borders) and you set the width of the child groupbox to the width of the main groupbox minus 8 (this is the inside borders).

Search on codeproject.com for examples of collapsible groupboxs, there are several there.
 
thank you Paszt, I figured it out for myself after a goodnightsleep
Your explanation makes it more clear also

Search on codeproject.com for examples of collapsible groupboxs, there are several there.
This sample is from the code project, but it was made with .net 1.1
It inherited from container so
I had to figure out wat to use instead.

I rather not touch samples that are made with 1.1, because in some cases they use to much code . I only get confused

But anyway, It works ok now, -I hope - (never say those things to fast!)

Correction, not working completely
If I collapse the box the textboxes that stay inside are still visible
Is there some event I can adress too, or do I have to make a for each control loop to make them invisible?
 
Last edited:
I now have a control inherited from usercontrol
The problem now is that I can't loop thru the box to set the child controls visibility lile I could with a normal groupbox.
I think I need to make a collection and fill it in the controll added event van de customcontrol.

If anyone can supply me with some code, that would be great?
 
Last edited:
The problem with using a usercontrol as the base is that you can't drag controls into the usercontrol during design time. You'll need to use either System.Windows.Forms.ContainerControl or System.Windows.Forms.ScrollableControl, there are many other possibilities, these are just examples. You can also use other classes that inherit from these classes (the panel for example). The reason you may want to use the panel control is for simplicity, the panel can draw it's own border. I personally would use the containerControl and draw the border myself so I could replicate the rounded corners of the groupbox and do away with the overhead associated with a panel control.

If you use one of the controls mentioned above, you'll notice you don't have to worry about the visibility of the child controls as the containerControl and ScrollableControl handle that themselves.

If you want to do away with the overhead of the above controls, you can use the base class System.Windows.Forms.Control. In this case you will have to deal with setting the child controls visibility and drawing the border. The "Control" class has a property called Controls which is a collection of controls contained within the control (the control's child controls). If you have ever looked at the code generated by the designer after adding a control to a form, you'll notice a line of code where the form's child control is added to the Controls collection. (Note that the Form class uses the Control class as a base as do most controls). If a control is created (in code for instance) and not added to a control's "Controls" collection, the control will not be drawn.
 
I personally would use the containerControl and draw the border myself so I could replicate the rounded corners of the groupbox and do away with the overhead associated with a panel control.
So,I get to keep all the code, but I have to remove the groupbox from the designer surface(the one I dragged out off the toolbox).
And when ever I am resizing, I have to call Invalidate, causing it to repaint itself again with the help of the property's for the collapsed/uncollapsed size respectivly?

If you use one of the controls mentioned above, you'll notice you don't have to worry about the visibility of the child controls as the containerControl and ScrollableControl handle that themselves.
I get a big error now saying the labels are not serializeble, what the heck does that mean?
Anyway, I changed containercontrol back to usercontrol and put this on top
<Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
PublicClass CGroupBox
Inherits System.Windows.Forms.UserControl 'ContainerControl
It seems to work also.
Could you explain whats going on here?


I don't know exactly how it works(how did the paint method get the sizes?), but with only the code below, it draws a rectangle (very cool).
How are the rounded corners being made?

VB.NET:
imports system.drawing
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CollapseGroupBox_Paint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.PaintEventArgs)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = e.Graphics [/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] theRec [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Rectangle = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle() [/SIZE]
[SIZE=2]theRec = ClientRectangle[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] theEdgeGrayColor [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Color = Color.FromKnownColor(KnownColor.Gray)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] thePen [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Pen = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Pen(theEdgeGrayColor) [/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] theEndPosition [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 20 + 1 [/SIZE]
 
[SIZE=2][COLOR=#008000]' manually draw the collapse box[/COLOR][/SIZE]
[SIZE=2]g.DrawLine(thePen, theRec.X + 8, theRec.Y + 5, theRec.X, theRec.Y + 5)[/SIZE]
[SIZE=2]g.DrawLine(thePen, theRec.X, theRec.Y + 5, theRec.X, theRec.Bottom - 2)[/SIZE]
[SIZE=2]g.DrawLine(thePen, theRec.X, theRec.Bottom - 2, theRec.Right - 1, theRec.Bottom - 2)[/SIZE]
[SIZE=2]g.DrawLine(thePen, theRec.Right - 2, theRec.Bottom - 2, theRec.Right - 2, theRec.Y + 5)[/SIZE]
[SIZE=2]g.DrawLine(thePen, theRec.Right - 2, theRec.Y + 5, theRec.X + theEndPosition, theRec.Y + 5)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Last edited:
Back
Top