0
votes

I'm new to programming and I need help with something really basic..

I have a form with a datagridview and an "insert" button that opens a popup window (new form). This popup contains labels and textboxes, and 2 buttons (insert & cancel). How do you add event handlers to the buttons inside the popup? For example, if the user clicks "Cancel", the popup should close.

Here's the code I have so far:

Public Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click

    Dim insertPopup As New Form
    insertPopup.Size = New System.Drawing.Size(300, 400)
    insertPopup.StartPosition = FormStartPosition.CenterScreen
    insertPopup.Show()

    Dim acceptButton As New Button
    Dim cancelButton As New Button

    acceptButton.Location = New System.Drawing.Point(100, 325)
    acceptButton.Text = "Insert"
    acceptButton.Size = New System.Drawing.Size(85, 24)
    acceptButton.TabIndex = 1

    cancelButton.Location = New System.Drawing.Point(190, 325)
    cancelButton.Text = "Cancel"
    cancelButton.Size = New System.Drawing.Size(85, 24)
    cancelButton.TabIndex = 2

    insertPopup.Controls.Add(acceptButton)
    insertPopup.Controls.Add(cancelButton)

End Sub

Thanks for your help.

1

1 Answers

0
votes

You use the AddHandler Method to add the proper EventHandler. You will need to be sure to create your handler with the correct signature.

AddHandler acceptButton.Click, AddressOf acceptButton_Click
AddHandler cancelButton.Click, AddressOf cancelButton_Click

Private Sub acceptButton_Click(sender As System.Object, e As System.EventArgs) 
End Sub

Private Sub cancelButton_Click(sender As System.Object, e As System.EventArgs) 
    CType(CType(sender, Button).Parent, Form).Close()
End Sub