简体   繁体   English

Msgbox Yes No & 在 VB.NET 2010 中工作的取消按钮

[英]Msgbox Yes No & cancel button working in VB.NET 2010

Below is my code for a MsgBox.下面是我的 MsgBox 代码。 The Yes button is working, but when I click No or Cancel it still deletes the data... 是”按钮正在工作,但是当我单击“否”或“取消”时,它仍会删除数据...

        strup = "DELETE FROM student WHERE urno =" & CInt(txtUrn.Text) & ";"
        Dim command As New OleDb.OleDbCommand(strup, con)
        MsgBox("Do you want to delete record(s)", MsgBoxStyle.YesNoCancel, "Confirm Delete")
        command.ExecuteNonQuery()
        con.Close()

How do I have it cancel the delete operation when clicking No or Cancel ?单击NoCancel时如何取消删除操作?

Use the basic If statement to check the return value of MsgBox .使用基本的If语句检查MsgBox的返回值。 The MsgBox doesn't cancel anything by itself. MsgBox本身不会取消任何内容。

If MsgBox("Prompt", MsgBoxStyle.YesNoCancel, "Title") = MsgBoxResult.Yes Then
    ' execute command
End If

You could also use MsgBoxStyle.YesNo to get only the Yes and No buttons.您还可以使用MsgBoxStyle.YesNo来仅获取 Yes 和 No 按钮。

Similar to If...Then , but I think this is cleaner类似于If...Then ,但我认为这更干净

Select Case MsgBox("Are you sure ?", MsgBoxStyle.YesNo, "Delete")
    Case MsgBoxResult.Yes
        ' Do something if yes
    Case MsgBoxResult.No
        ' Do something if no
End Select
        If MsgBox("Are you sure ?", MsgBoxStyle.YesNo, "Delete") = MsgBoxResult.Yes Then
            strup = "DELETE FROM student WHERE urno =" & CInt(txtUrn.Text) & ";"
            Dim command As New OleDb.OleDbCommand(strup, con)
            'MsgBox("Do you want to delete record(s)", MsgBoxStyle.YesNoCancel, "Confirm Delete")
            command.ExecuteNonQuery()
            con.Close()
            txtUrn.Text = ""
            txt10Per.Text = ""
            txt12Per.Text = ""
            txtCAdd.Text = ""
            txtEid.Text = ""
            txtFname.Text = ""
            txtGPer.Text = ""
            txtMno.Text = ""
            txtName.Text = ""
            txtPAdd.Text = ""
            cmb10YofPass.Text = ""
            cmb12YofPass.Text = ""
            cmbDate.Text = ""
            cmbGender.Text = ""
            cmbMonth.Text = ""
            cmbNameofGCourse.Text = ""
            cmbYear.Text = ""
            ComboBox1.Text = ""
            TextBox1.Text = ""
            MsgBox("Record Deleted Successfully")
        ElseIf MsgBoxResult.No Then

        End If

MsgBox 仍然不能在 ASP.NET 生产环境或 QA 而不是开发环境中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM