Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why is this happening??
Message
De
29/01/2009 19:30:22
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
29/01/2009 19:21:25
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Divers
Thread ID:
01378204
Message ID:
01378346
Vues:
14
>This looks really interesting...I'll be the first one to admit this is an area (Delegates, etc.) that I'm not very familiar with...
>
>An example would be wonderful if you have a minute to put one together...
>
>It would help me connect all of the dots.
>
>bob
>

No problem Bob. Will have it in your inbox first thing in the AM.
Tim

>
>>>OK. Can you help me see what the code would look like? And how can the control pass values to the event?
>>>
>>>Just so you know what I am trying to do here is I have a textbox/button combo, and the textbox has a tablename, and indexname properties that are passed to a search window and the user types the search string, and then I want to activate code to do the search and update the windows on the form with the new record data. The form is one class, the search functionality is in another class and the textbox/button is another class (they say you're supposed to write this stuff in multiple tiers :)
>>>
>>
>>Sure: Here is an example I use in a class which I call Tracker. When certain things happen in the class it fires events. One of those events is Tracker Activity. This is the pieces you need to have.
>>1. I have a file called TrackerActivityEventArgs and it contains the event args class which is a subclass of EventArgs. It looks like this.
>>
>>public class TrackerActivityEventArgs : EventArgs
>>{
>>     public readonly string Message;
>>     public readonly TrackerAction Action;
>>		
>>     public TrackerActivityEventArgs(string message, TrackerAction action)
>>     {
>>		this.Message = message;
>>		this.Action = action;
>>     }
>>}
>>
>>2. Then you need a delegate which can be in the same control cs file but outside the class or it can be in a seperate file. I usually create a seperate file for all the delegates. Here is the delegate.
>>
>>public delegate void MyControlButtonClickedDelegate (Ojbect sender, MyControlButtonEventArgs e);
>>
>>3. Now you need to define the event inside your control class like this. Notice the "TrackerActivityDelegate" is what I defined above.
>>
>>public event TrackerActivityDelegate TrackerActivity;
>>
>>I also add right under the event definition a method which raises the event properly and safely. It does this by first checking if there are any subscribers listening. If there are, then it raises the event and creates a new TrackerActivityEventArgs object.
>>
>>private void OnTrackerActivity(string message, TrackerAction action)
>>{
>>     TrackerActivityDelegate tad = this.TrackerActivity;
>>     if (tad != null)
>>     {
>>          // Raises the Event
>>          this.TrackerActivity(this, new TrackerActivityEventArgs(message, action));
>>     }
>>}
>>
>>Now all we have to do in the control is raise this event when we need to. In my case it looks like this. In your case, it might be in the button click that you raise an event after doing whatever you need to do first. All this call does is call that method above "OnTrackerActivity" passing it what that method needs. That method then verifies if it is safe to raise the event.
>>
>>this.OnTrackerActivity("Preparing to save to Queue", TrackerAction.Processing);
>>
>>Now in your application form you need to register for this event or subscribe to it. You can do it in the forms constructor or anywhere you want so long as it is done before you need to be listening for the event. Note the part inside the parentheses is the method you want to run when the event is raised.
>>
>>this.myControlTrackerActivity += new MyControl.TrackerActivityDelegate(myControl_TrackerActivity);
>>
>>Now you just need the event described above so that it will do what you want in the form. Notice the arguments match those of the delegate.
>>
>>public void myControl_TrackerActivity(object sender, TrackerActivityEventArgs e)
>>{
>>     // do what you want here, but caution you may need to deal with invoke.  I can give you example of that if needed also.
>>}
>>
>>
>>Now that I have put all that together in the message, it might have been easier for me to have made you a quick sample. I can still do that and include the Invoke example you need. Just let me know, I will make it more like what you are doing. This may be enough to make sense to you. Most of your code to create this is in your control side. The form only needs to subscribe to it and assign a method to run when the event occurs.
>>Tim
Timothy Bryan
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform