tags:

views:

363

answers:

2

I have a GroupBox control that has a bunch of controls inside, but when I use the .Controls property, it's empty.

Is there another property that stores these controls?

EDIT: Here is the groupbox code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace CustomControls
{
    public partial class CustomGroupBox : GroupBox
    {
     public CustomGroupBox ( )
     {
      this.OutlineColor = Color.FromArgb ( 5, 5, 5 );
      this.Font = new Font ( "Tahoma", 8.25F, FontStyle.Regular );
     }

     Color outlineColor;

     [DefaultValue ( typeof ( Color ), "5, 5, 5" )]
     public Color OutlineColor
     {
      get { return outlineColor; }
      set { outlineColor = value; Invalidate ( ); }
     }

     [DefaultValue ( typeof ( Font ), "Arial, 8.25pt" )]
     public override Font Font
     {
      get { return base.Font; }
      set { base.Font = value; }
     }

     protected override void OnPaint ( PaintEventArgs pe )
     {
      //painting
     }
    }
}
A: 

Are you sure the controls have been added at the time you're inspecting the property? The GroupBox stores child controls in the Controls property, just as any other container control would.

Edit

I can't really begin to speculate about the custom control. Obviously it would be possible to create a control with the behavior that you describe, but I can't fathom a reason for it. Just creating a sample Windows Forms project and dragging two buttons into it and calling MessageBox.Show(groupBox1.Controls.Count.ToString()) from another button presents a 2, as expected.

Given the code that you've posted for the GroupBox, I can't see any reason for this to happen aside from the controls genuinely not being in the GroupBox. If you inspect the Parent property of one of the controls, what is the value?

Edit 2

If you're saying that you have a UserControl that contains your custom GroupBox and you're trying to place the UserControl on a Form and put controls insde the GroupBox from there, then that won't work. The designer on the form only "knows" about the UserControl; it doesn't know (or care) that it contains a GroupBox (for one thing, the designer doesn't have access to that variable by default, and neither do you, since it's private unless you explicitly change the modifier).

In order to do this exactly as you want to, you're going to have to do the necessary lifting to make your UserControl a container, but that is a non-trivial amount of work. I would suggest just placing the GroupBox on the form directly.

Adam Robinson
Yeah I added them in the designer and accessing them in the Click event for the group box. But hat's a custom control that derives from the standard groupbox, maybe that's why? Also when I move the groupbox, all the controls move as well.
Joan Venge
Ok I added code for the groupbox. Looks simple, right?
Joan Venge
Ok that's strange. The parent of the controls inside my groupbox returns the form itself, whereas other controls inside the standard group returns the group. I don't know what's wrong so I will try with a new custom groupbox that has no code.
Joan Venge
Another update: I just tried it with my custom groupbox and it worked, ok what's the difference? Well I use that groupbox in a composite control and there it doesn't work. Is there a reason why this wouldn't work composite controls.
Joan Venge
Ok it looks like when I move my composite control, the included controls don't move. So it looks like the grouping functionality is gone.
Joan Venge
A: 

I see the controls property filled in my groupboxes. Then just grpMyGroupBox.Controls.Item(index).Name to get the name of the controls. Doesn't make much sense why you wouldn't see them in there.

Matt