Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Hook into Form Events from Listview
Message
De
09/01/2006 07:45:55
 
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01084641
Message ID:
01084648
Vues:
9
Pete,

The following code is one method you can use for hooking into the load and closing events of a form. As you can see I subclass the ListView control and override the OnParentChanged event.

It assumes the parent is the form by referencing the TopLevelControl value (not the Parent), if this is not the case then you will need to modify your code to suite. It also assumes that the parent form doesn't change, again if this is the case then you will need to handle this.

This is a quick example so feel free to make this a little more robust.
public class MyListView : System.Windows.Forms.ListView
{
   private Form _parentForm = null;

   public MyListView()
   {
   }

   protected override void OnParentChanged(EventArgs e)
   {
      base.OnParentChanged(e);
			
      if (this._parentForm != null)
         return;

      if (this.DesignMode == false)
      {
         if (this.TopLevelControl != null && this.TopLevelControl is Form)
         {
            this._parentForm = (Form)this.TopLevelControl;

            this._parentForm.Closing += new CancelEventHandler(MyListView_Closing);
            this._parentForm.Load += new EventHandler(MyListView_Load);
         }
         else
         {
            MessageBox.Show("Cannot hookup form events to a non-form type");
         }
      }
   }

   private void MyListView_Closing(object sender, CancelEventArgs e)
   {
      MessageBox.Show("The parent form is closing!");

      if (this._parentForm != null)
      {
         this._parentForm.Closing -= new CancelEventHandler(MyListView_Closing);
         this._parentForm.Load -= new EventHandler(MyListView_Load);
      }
   }

   private void MyListView_Load(object sender, EventArgs e)
   {
      MessageBox.Show("The parent form is loading!");
   }
}
Regards
Neil
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform