Frage

I have a parent form, called form A. This creates child form, lets say called form B.

For A subscribes to form B's FormClosing event and makes changes based on this action.

What I want to know is, how do I detect which control in Form B caused the FormClosing event to trigger. I want the code to behave one way if the "X" or "Cancel" buttons are pressed and another if the OK button is pressed.

The code looks like this:

Private Sub btnAddSections_Click(sender As Object, e As System.EventArgs) Handles btnAddSections.Click
    Cursor = Cursors.WaitCursor
    Dim frm As New dlgAddPartsMartixSection(m_intMatrixId)
    AddHandler frm.FormClosing, AddressOf btnAddSectionsRefresh
    frm.Show()
    Cursor = Cursors.Default
End Sub 

 Private Sub btnAddSectionsRefresh(sender As Object, e As FormClosingEventArgs)
    dgvParts.DataSource = Nothing
    btnAddParts.Enabled = False
    dgvParts.Refresh()
    Me.LoadPartMatrices()
    dgvMatrices.Rows(m_intMatrixLocationId).Selected = True
    Me.LoadPartSections(m_intMatrixId)
End Sub

Any ideas/Suggestions?

War es hilfreich?

Lösung

Usually the arguments to the method tell you everything. Investigate on

Private Sub btnAddSectionsRefresh(sender As Object, e As FormClosingEventArgs)

and you should be able to determine who caused this by checking out the sender parameter.

Andere Tipps

You can use the DialogResult property to determine how form B was closed.

Try this code

     Private Sub btnAddSectionsRefresh(ByVal sender As Object, ByVal e As FormClosingEventArgs)
           if  Me.ActiveControl.Text = "X" then
.....
......
elseif Me.ActiveControl.Text="Cancel"
.....
.....
elseif Me.ActiveControl.Text ="Ok"
.....
...
        End Sub
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top