0

我是编程新手,我需要一些非常基本的帮助..

我有一个带有 datagridview 和一个“插入”按钮的表单,可以打开一个弹出窗口(新表单)。此弹出窗口包含标签和文本框,以及 2 个按钮(插入和取消)。如何将事件处理程序添加到弹出窗口内的按钮?例如,如果用户单击“取消”,弹出窗口应该关闭。

这是我到目前为止的代码:

Public Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 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

谢谢你的帮助。

4

1 回答 1

0

您使用AddHandler方法来添加正确的 EventHandler。您需要确保使用正确的签名创建您的处理程序。

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
于 2012-10-09T02:55:14.633 回答