views:

131

answers:

4

Hi,

I'm making a settings screen at the moment that will have a radiobutton group which will determine what controls are displayed. The different settings are contained within a UserControl.

I am dynamically creating this UserControl like so:

panel = new btSettings();
this.Controls.Add(panel);
panel.Location = new Point(15, 49);

Just wondering how I can access the fields within this control and design time when the object will only be created during run time?

Thanks.

A: 

You can't get not existing object on design time.

Andrew Bezzub
A: 

You'll have to programatically set up the control at run time. You could use design time to figure out what you want by statically adding the control, but youll have to programatically set up the control at runtime

phsr
+1  A: 

Answering your question...

Just wondering how I can access the fields within this control and design time when the object will only be created during run time?

If you need to work with the control at design time, I think the only way is to create a custom control and drag it into your form.

Javier Morillo
A: 

You will need to use indirect references to do what you are wanting to do. Its been a while since I worked with user controls, but container controls should support a Controls (or Children) collection which will return the controls that exist in it. You will then need to iterate over them, cast them to the types you care about and work with them. For example:

Foreach(Control target in panel.Controls) {
   if (target.GetType() == typeof(RadioButton) {
      ((RadioButton)target).Checked = true;
      //etc...
   }
}
GrayWizardx