开发者

C#.NET How to access fields in user control dynamically?

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开发者_Go百科 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.


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.


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...
   }
}


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜