Results 1 to 4 of 4

Thread: call function in parent form?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    call function in parent form?

    Hi,

    I have a class inhert ToolStripButton. it builds a new toolstrip button. the button will be added to the toolstrip which is on main form.

    I will need call function in the main form. what's the right way to do this?

    now, I pass the main form when create the class. but I guess the right way is to raise the event in main form.

    but, how about the properties in main form?

    if I raise the event, will it run the event first, then go to the next line of the class? or, just raise event, then go to the next line in the class immediately?


    thanks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: call function in parent form?

    Passing the form to the class is not all that bad. The key concept is that the class needs to communicate with the form in some fashion. There are a couple ways you could do this. One would be to have the class raise an event and have the form handle that event. This will work if the class instance was created on the form WithEvents, or was otherwise bound. Since this is a control, it WILL be created WithEvents, so that makes raising an event a very appealing option.

    As for your second question, it will run the event, then go to the next line. The question is whether it runs synchronously or asynchronously. There are a few asynchronous events around, or ones that behave that way, but you won't be seeing that unless you put in a fair amount of effort to make it happen that way. One way you can examine this is to put a breakpoint on the line where you raise the event, then press F11 (or F8, depending on your keyboard settings) to step forward and see which line is executed next.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: call function in parent form?

    Quote Originally Posted by Shaggy Hiker View Post
    Passing the form to the class is not all that bad. The key concept is that the class needs to communicate with the form in some fashion. There are a couple ways you could do this. One would be to have the class raise an event and have the form handle that event. This will work if the class instance was created on the form WithEvents, or was otherwise bound. Since this is a control, it WILL be created WithEvents, so that makes raising an event a very appealing option.
    thanks. what about properties in main form?

    say in the class, I need use a property in the main form, and it may change in the main form (because of other input). now, I pass the form to the class, then use the property directly.

    but if I pass the parent form anyway, why not just use it to call functin in parent form (instead of using raise event)?

    a little bit confused.


    thanks

    bear

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: call function in parent form?

    That's a fair point. If you need to access properties in the main form, there really isn't any way around it: You need to be able to access the properties, and therefore you need to access the main form.

    This is not a great design, because that control is now tied to that specific form. The goal in OO is to make things versatile enough that an object can be taken from one place and used somewhere else. Having said that, it should also be obvious that there are objects that can ONLY work if they are given certain information, and your inherited control may well be one of those objects. It may not make any sense for it to NOT have access to the form, in which case, just give it the reference to the form, as you are doing it. You might spend a little time thinking about whether you can get that information any other way than from the form properties, but it actually sounds like you probably can't, so I wouldn't spend too much time thinking about it.

    So, if you have the reference, should you bother with the event? Calling the method on the form is going to be faster than raising the event, though only very slightly. It will also take more effort to use the event, since you have to create an event, raise it, and have a method on the form that will handle the event. Kind of a pain, but it might be a way for you to avoid using the properties. What you could do is create a custom event that has a custom argument object. When you raise the event, you will have to create one of those argument objects and pass it when you raise the event. The main form could then stuff things into that custom argument when it handles the event, and the raiser will still have the object (you are only passing the address around, not the actual object, after all) with those new items in it. So, if you only need the properties from the form at the time you raise the event, you could use the event mechanism to get those properties. Still, that may well not be a good enough reason to just use events rather than the passing in the form. Only you can decide.

    If you decide that passing in the form is the best way, then the only reason to raise the events is if any other part of the code other than the form, might want to receive those events. That's pretty unlikely for a control, so if you pass in the form, you probably should just call the method on the form.
    My usual boring signature: Nothing

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