Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Hook into Form Events from Listview
Message
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01084641
Message ID:
01084648
Views:
10
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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform