私有子BtnUpdate_Click(发送者为System.Object,e为System.EventArgs)处理BtnUpdate.Click

时间:2018-12-26 21:15:51

标签: sql vb.net

我能够更新我的数据,但是当我单击“否”或取消时,我仍然收到“ DATA UPDATED”消息框。我应该对代码进行哪些更改以停止更改而不得到更新的消息框?

Private Sub BtnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles BtnUpdate.Click
    Try
        da1.Update(ds1, "Table")

        MessageBox.Show("Do you want to update?", "Table", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

        If (DialogResult.Yes) Then
            MessageBox.Show("Data Updated...!")
            End

        ElseIf (DialogResult.No) Then
            End

        ElseIf DialogResult.Cancel Then
            End

        End If

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

2 个答案:

答案 0 :(得分:2)

您必须获取MessageBox.Show的结果,因此必须定义一个变量来存储结果。

尝试一下:

Dim yourResult As DialogResult = MessageBox.Show("Do you want to update?", "Table", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

If (yourResult = DialogResult.Yes) Then
        MessageBox.Show("Data Updated...!")

    ElseIf (yourResult = DialogResult.No) Then Console.WriteLine("Press No")

    ElseIf (yourResult = DialogResult.Cancel) Then Console.WriteLine("Press Cancel")

End If

答案 1 :(得分:1)

您应该像这样重新构建代码:

Try
    Dim result As DialogResult = MessageBox.Show("Do you want to update?", "Table", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

    If result = DialogResult.Yes Then
        da1.Update(ds1, "Table")
         MessageBox.Show("Data Updated...!")
    ElseIf result = DialogResult.No Then

    ElseIf result = DialogResult.Cancel Then

    End If
Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try

您将da1.Update(ds1, "Table")放在If ... Else语句块之前,以便在检查条件之前首先执行该语句。