none
Detect user control removal RRS feed

  • Question

  • I am new to user controls in vb.net.  I have a form with a splitter container with a panel on the left and panel on the right.  On the left hand side, I have a treeview control that acts as a menu.  when the user clicks an item on the treeview control, I load the appropriate user control on the right hand panel.  I have all of that working as I want but I have a small problem.  A couple of the user controls are used to update data in a database.  The user might change data, but forget to save the data.  If they click a new menu item on the left, I use a controls.clear and then load the new user control they selected.  If they forgot to save, the changes are lost.  What is the best way to trap in the user control that it is about to be cleared and prompt the user to save or abandon their changes?  I have tried all of the events of the user control that seemed to make sense, but none seem to capture the fact that the control is getting removed and it is removed and the new control is loaded.

     

    The only method that I have been able to come up with so far is to keep track of the control loaded in a form variable and when a change occurs, call a custom OnClose method of that form that checks for changes before processing the controls.clear of the panel.  This seems like a real pain because I would have to reference each form.

     

    Any help would be much appreciated.  I hope this is something easy I have overlooked.

    Thursday, May 31, 2007 2:09 PM

Answers

  • Hows about:

     

    Code Snippet

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.Controls.Remove(TextBox1)

    End Sub

    Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)

    MsgBox("Hey " & e.Control.Name & " is going bye bye")

    MyBase.OnControlRemoved(e)

    End Sub

     

    Thursday, May 31, 2007 7:41 PM