2013-03-16 112 views
0

以下是我的MsgBox代码。该按钮正常工作,但是当我点击没有取消它仍然会删除数据......Msgbox是否&取消按钮在VB.NET 2010中工作

 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() 

我怎么把它取消删除操作点击没有时取消

回答

5

使用基本的If声明。 MsgBox不会取消任何内容。

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

您也可以使用MsgBoxStyle.YesNo来获取仅Yes和No按钮。

+0

否其仍然删除 – CrashOverride 2013-03-16 18:25:36

+0

尝试移动'If'内的所有其他代码? – Pietu1998 2013-03-16 18:33:34

0
 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 
2

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 
+0

这也可以工作 – CrashOverride 2013-03-17 16:28:21

+0

顺便说一句,你应该使用.NET等效的System.Windows.Forms.MessageBox.Show()而不是MsgBox。尽管它们都指向.NET版本,但我们已转向.NET,并且没有理由坚持VB6语法。 http://stackoverflow.com/questions/8799976/is-there-any-difference-between-msgbox-and-messagebox-show – djv 2013-03-18 13:55:02

0

还有一个MsgBox不会在ASP.NET生产环境或质量保证,而不是开发环境中工作。