Results 1 to 4 of 4

Thread: Raise event in user control

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Raise event in user control

    I have a windows user control "MyControl" which contains a method "MyMethod".

    I add the control dynamically to my form which is easy enough but how does my parent form then call the method ?

    TIA

    Parksie

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,430

    Re: Raise event in user control

    The question you've asked doesn't have anything to do with raising events. Have you mis-stated the question?

    When you say dynamically I assume that you mean at run time. Just like any object, you must have a reference to this UserControl to access its members. You can assign it to a class-level variable when you create it and access it through that. This variable could be of the same type as the UserControl, or it could be an array or a collection and you add the control as an element or item. Alternatively you can access the control by iterating over the Controls collection of the form until you find a control that matches whatever criteria you have, like a specific Name, Tag or just the type.

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Raise event in user control

    Sorry if it sounds strange but to be honest I dont know what the heck Im doing.

    Basically when the user clicks on a button on my form I want my user control to be loaded
    Code:
    "ctlAddProject controlAddProject;Controls.Add(controlAddProject);"
    which is easy enough.
    Now there is a public method called doBind(string fName) on the control which I want to call from the parent form passing the appropriate string.
    If the control was added at design time I could do something like :
    Code:
    controlAddProject.doBind("StringHere");
    How do I do this now the control is added at run time ?
    Last edited by venerable bede; Apr 27th, 2006 at 05:02 AM.

    Parksie

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,430

    Re: Raise event in user control

    Like I said, you need to keep a reference to this object when you create it. How many of these objects will you have? Will it be a set number or will it vary? If there is just one then you can use a single variable. If it's a fixed-number greater than one the use an array. If it's not a fixed number then a collection would be the way to go.

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