Results 1 to 8 of 8

Thread: Dynamic User Control [RESOVLED ]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Resolved Dynamic User Control [RESOVLED ]

    I have created a user control that has a public function in it called
    Getstatus()

    I am loading 3 of these user controls dynamically upon starting my application. How do i access each controls GetStatus function individually?
    Last edited by Halisco; May 11th, 2008 at 09:28 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Dynamic User Control

    vb Code:
    1. dim uc1 as new usercontrol
    2. me.controls.add(uc1)
    3.  
    4. uc1.getstatus

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dynamic User Control

    Like any object, to get access to your user control's you need a variable that references them. When you add a control to your form at design time the IDE generates a member variable for you and it assigns that control to that variable when the form is initialised. If you're adding the controls yourself in code then this doesn't happen, so it's up to you to declare your own variable.

    The code that .paul. provided is only so much use because it only allows you to access the control within the method it was created. If you want to access the control outside that method then you need a variable that exists outside that method, i.e. a member variable. If you know exactly how many controls there will be then you could declare individual variables for each of them, or you might choose to have a single array. If you don't know how many controls there will be then you'd be best to use a collection, to which you can add as many or as few instances as you like.

    That said, if you know for a fact that there will be three instances of this control then I'd question why you're adding them in code anyway. There may be a legitimate reason but I'd be interested to hear it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Re: Dynamic User Control

    Quote Originally Posted by jmcilhinney
    Like any object, to get access to your user control's you need a variable that references them. When you add a control to your form at design time the IDE ......
    Your correct i do not knowhow many there will be added at once the number may be different each time.i have tried this. but there is one problem with it. It works with all objects that are part of the >NET library but it wont allow me to use the GetStatus function in my usercontrol. Here is my code..

    Code:
            
            Dim ControlShowing As Control
            Dim idx As Integer
            idx = 0
            For Each ControlShowing In Me.Controls
                idx = idx + 1
                If TypeOf ControlShowing Is MyUserControl Then
                    If ControlShowing.Name = "Exx" & idx Then
                        ControlShowing.Getstatus()
                    End If
                End If
            Next ControlShowing
    The problem is when i call this:

    ControlShowing.Getstatus()

    It says is not a member of System.Windows.Forms.Controls

    Any other input?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dynamic User Control

    Exactly as the error message says. GetStatus is a member of YOUR class, not of the Control class. If you want to access members of YOUR class then you have to use a reference of YOUR type, not Control. In the case of the code you're using there that means casting the Control reference as your type:
    vb.net Code:
    1. DirectCast(ControlShowing, MyUserControl).Getstatus()
    That said, I really recommend not looping needlessly through every control on the form and casting when you can do what I suggested earlier and just added each instance you create to a List that is already typed to your class.
    vb.net Code:
    1. Private myUserControls As New List(Of MyUserControl)
    You can then just loop through that List and you know that you'll only be referencing the controls of your type and there will be no need to cast.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Re: Dynamic User Control

    Quote Originally Posted by jmcilhinney
    Exactly as the error message says. GetStatus is a member of YOUR class, not of the Control class. ....
    Wow Thank you very much!! I am new to .NET and so much for me to learn. Where did you learn what you know?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dynamic User Control

    Quote Originally Posted by Halisco
    Where did you learn what you know?
    I do have a Computer Science degree, so all the fundamentals were there to begin with. that said, I did teach myself .NET programming from scratch using the MSDN Library as my primary resource.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    31

    Re: Dynamic User Control

    Quote Originally Posted by jmcilhinney
    I do have a Computer Science degree, so all the fundamentals were there to begin with. that said, I did teach myself .NET programming from scratch using the MSDN Library as my primary resource.

    Wish i had that knowledge. You must have a prosperous and wealthy career.

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width