在Visual Basic中创建删除按钮

时间:2015-06-22 21:13:53

标签: vb.net

我在Visual Basic中编程。我有一个表单,我已经创建并使用Microsoft Access的数据源。我创建了一个删除按钮,因此用户可以编辑信息。我为Delete按钮编写了一些代码,它删除了很好的信息。我有一条消息,询问“你确定要这么做吗?”但无论我是单击是还是否,它都会删除它。我能否就需要改变的方向找到方向,以便它能正常运行?希望这是有道理的。这是我到目前为止所拥有的......

Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDelete.Click
    '== Enforce Referential Integrity
    If OrdersDataGridView.CurrentRow Is Nothing Then  '== There were no matching orders
        If DialogResult.Yes = MessageBox.Show("You really want to delete this customer?", _
        "Confirm Delete", MessageBoxButtons.YesNo) Then CustomersBindingSource.RemoveCurrent()
    Else
        If DialogResult.Yes = MessageBox.Show( _
            "There are orders referring to this customer." _
            & vbCrLf & "You must dispose of them somehow before you can delete this customer." _
            & vbCrLf & "Do you wish to delete every matching order now?", _
            "Enforce Referential Integrity", MessageBoxButtons.YesNo) Then
            Do Until OrdersDataGridView.CurrentRow Is Nothing
                OrdersBindingSource.RemoveCurrent()
            Loop
            CustomersBindingSource.RemoveCurrent()
        End If
    End If
End Sub

谢谢!

1 个答案:

答案 0 :(得分:0)

相反,尝试将用户点击的结果和If语句中的布尔测试分开,看看是否有效。像这样:

If OrdersDataGridView.CurrentRow Is Nothing Then
    Dim result As Integer = MessageBox.Show("You really want to delete this customer?", _
    "Confirm Delete", MessageBoxButtons.YesNo)

    If result = DialogResult.No Then
        Exit Sub
    ElseIf result = DialogResult.Yes Then
        CustomersBindingSource.RemoveCurrent()
    Else     ' unnecessary, but I like to include it
      'do nothing
    End If

Else
    Dim result As Integer = MessageBox.Show( _
       "There are orders referring to this customer." _
       & vbCrLf & "You must dispose of them somehow before you can delete this customer." _
       & vbCrLf & "Do you wish to delete every matching order now?", _
       "Enforce Referential Integrity", MessageBoxButtons.YesNo)

    If result = DialogResult.No Then
       Exit Sub
    ElseIf result = DialogResult.Yes Then
       Do Until OrdersDataGridView.CurrentRow Is Nothing
           OrdersBindingSource.RemoveCurrent()
       Loop
       CustomersBindingSource.RemoveCurrent()
    Else     ' unnecessary, but I like to include it
      ' do nothing
    End If
End If