Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
On Methods Question
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01414827
Message ID:
01414991
Vues:
40
Isn't the more usual format:
public virtual void OnTimerEvent( EventArgs e)
{
   if (this.TimerEvent != null)
      this.TimerEvent(this, e);
}
>>In .Net there are methods that start with 'On', such as OnPaint, OnResize.
>>
>>I know I can call thes methods, but I know very little about them. What are they, when do you use them, and what's the correct way to invoike them - meaning how do you create a propert event args class for the method you're calling?
>>
>>My form automatically calls OnPaint at certain times, and anytime I call Invalidate(), but if I wanted to call it on my one (or would i?), how do you get the correct arguments?
>
>Usually methods prefixed with "On" are the code that actually fires an event is placed. It gives the developer a hook point to insert additional operations before or after an event call (or populate event arguments) from a subclass. It is usually the place where the code determines if anyone is hooked up to an event, and if so, it actually fires the event to any subscribers.
>
>For example, if you created a timer class, every so often you want to fire a timer event to notify anyone that has subscribed to this event that the event has occurred. You might create a OnTimerEvent method that looks something like this:
>
>
>public virtual void OnTimerEvent(object sender, EventArgs e)
>{
>   if (this.TimerEvent != null)
>      this.TimerEvent(sender, e);
>
>}
>
>
>This is basically the method that fires the event. As far as creating the event arguments you may or may not be able to do that, depending on the event and the type of arguments that are supposed to be passed as part of the event. In the simplest case, you can just create an instance of whatever EventArgs class is supposed to be included. Sender is usually a reference to the instance of the class that holds the OnTimerEvent method. Something like this may or may not be valid:
>
>
>MyTimerClass timer = new MyTimerClass();
>timer.OnTimerEvent(timer, new EventArgs());
>
>
>Some event arguments include other properties that are supposed to be filled with info. you may not have access to. For example, if the event is normally fired in response to a Windows event (via a windows message), the event arguments might hold a handle to the Window that sent the message, and the message itself.
>
>So what I'm basically saying is, it depends. In some cases it may be valid and OK to call On???. In other cases, it may not be. In your example, they provide the Invalidate() method which sends/queues the correct Windows message that ends up firing OnPaint.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform