Results 1 to 6 of 6

Thread: events delegates help

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    events delegates help

    I know, almost, how these work. Pretty cool.

    I have used it before but for this new application, a bit confused on how to make it work.

    I have 2 forms which I will be dealing with.

    1 form is a main form the other is an "editor"

    in the "editor" I want some things disabled, if a certain condition happens, and this works fine.

    Since the "editor" is never always open (only when user clicks button, the editor.ShowDialog() is then processed), I dont know how to make this work:


    the main form has a timer. and when a certain condition evalutes to true in this timer, a process starts (whatever it maybe). I want to notify the "editor" WHEN this happens - and only when the editor form is shown to the user.

    Sure, I could just call a method from the timer to the editor form only if the editor form is not currently showing but I do not think this would be the best way - better just raising an event no?

    I hope you understand what I am saying if not, please feel free to let me know and I will explain more.

    so:

    2 forms
    1 form is editor

    if certain condition is true on the main form timer, I want an event to be raised only if the editor form is in process/opened/shown to the user.

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

    Re: events delegates help

    That's a misuse of an event. An event is supposed to be raised when something happens. Something evaluating to True is not something happening. If something was False and its value changed to True, that would be an appropriate time to raise an event, but the event should be raised when that change happens. Besides that, if you were to use an event you would raise it regardless of whether the child form is displayed or not. Only if the child form was displayed would it handle the event, but the event would be raised regardless.
    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

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: events delegates help

    Thanks for that, thanks for clearing it up

    well yes, I am trying to do this. I am trying to raise an event when something happens - that is what I want to do. But how would I go about doing this within those 2 forms?

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

    Re: events delegates help

    You've done exactly the same thing a thousand times I bet. Forms are no different to any other object. If you have a Button and you want to hanlde its Click event in a form you declare a variable and a method, then you assign the Button to the variable and add the method to the event. You do exactly the same thing now, except your variable and event are of a different type.
    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

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: events delegates help

    I understand but its hard to implement.

    This "editor" form is not always open so how do I go about putting in the delegate/initialising it on this form?

    Sorry, I can't even remember what I am doing now lol.

    Going to see where I am having problems again and get back to you more specifically.

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

    Re: events delegates help

    You write an event handler for the event in the editor form class. When the form is opened it needs a reference to the other form. If it is the main form that opens the editor form then it should pass a reference to itself as a parameter to the constructor. In the constructor or Load event handler of the editor form you attach the method to the event.
    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

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