Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 16-03-2010 5:59pm
    #1
    Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭


    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:
    [INDENT][INDENT]panel = new btSettings();
    this.Controls.Add(panel);
    panel.Location = new Point(15, 49);[/INDENT][/INDENT]
    

    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.


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    You could expose the controls, or just the properties you want to set as properties of the User Control I guess. But I think the Observer Pattern might be a better solution.

    <edit>
    Just read that through properly - the first will probably suit you better for design-time access.


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    I understand what you are saying but the user control is only instantiated at runtime i.e. user clicks a category in the settings screen, then an object of that control is created. I can't reger to that object during design time as it has not yet been created.


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    techguy wrote: »
    I understand what you are saying but the user control is only instantiated at runtime i.e. user clicks a category in the settings screen, then an object of that control is created. I can't reger to that object during design time as it has not yet been created.

    Well you can never reference an object at design time as objects only get created at run time.

    If you can do the code in your first post you can get at properties on the contol (assuming btSettings is the control your on about).

    Does your usercontrol have say an ok button? If it does you could always just fire an event and pass a reference to the usercontrol back to any event handlers which you can hook up when you create the control at run time.


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    When you do Controls.Add you are adding controls to the Control.ControlCollection class. This class implements IEnumerable so maybe you could use a foreach to iterate over the controls in the container?

    I don't bother creating controls dynamically, I just use progressive disclosure to show/hide controls at runtime. It's much easier.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Isn't foreach recursive? Seems a bit overkill to me, but I'm generally against foreach to loop through controls anyway.

    Techguy, could you post the code for the btSettings usercontrol and a tell us a bit about it? It will give us a much better idea of what you're trying to do.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Why would foreach be recursive? Not that I agree with a foreach through controls or anything that's dynamically created just because that's how it should appear when using the program.

    The overhead of creating all user controls, and show/hiding the one you want is nothing compared to the implementation and debugging nightmare of plonking controls at runtime and trying to identify what's in them by enumerating their child controls.

    I didn't really mention that earlier because well, I don't know the full picture here.


Advertisement