Link to home
Start Free TrialLog in
Avatar of David Svedarsky
David SvedarskyFlag for United States of America

asked on

How to create a message box to cancel record delete

This is databound:

I am looking for sample code to create a message box that will cancel a BindingNavigatorDeleteItem record delete.

This does not work:
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
      If MessageBox.Show("Are you sure?", "Delete Row", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
      End If

Everything I have tried will not cancel the event.
Avatar of vbturbo
vbturbo
Flag of Denmark image

Do you mean

MessageBox.Show("text","caption", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

Or some kind of rollback

vbturbo
Avatar of David Svedarsky

ASKER

vbturbo,
Yes to your question.
Im not quite sure if i got the grasp of your question

maybee this is what you mean

       Dim msg As String = "The data will be permanently lost.?"
        If MessageBox.Show(msg, "Deleting Data File", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
 else

Exit sub

        End If
I see ,,, sorry for my late binding of concepts -:)

Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
    If (Me.Validate() And Not (CustomersBindingSource Is Nothing)) Then
         If (MsgBox("Delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
             CustomersBindingSource.RemoveCurrent()
             CustomersBindingSource.EndEdit()

            'Me.TableAdapter.Update(Me.DataSet.Name)
         End If
    End If
End Sub

this should do it

vbturbo
vbturbo,
This is what I mean except on this code and all I have tried - if you click "No" - the record is deleted.
(Clicking "No" does not cancel the delete)
Set the DeleteButton member on the BindingNavigator to Nothing and then hook the Delete Button Click yourself.
Try this

Set the DeleteButton member on the BindingNavigator to Nothing and then hook the Delete Button Click yourself

Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
    If (Me.Validate() And Not (CustomersBindingSource Is Nothing)) Then
         If (MsgBox("Delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
             CustomersBindingSource.RemoveCurrent()
             CustomersBindingSource.EndEdit()

            'Me.TableAdapter.Update(Me.DataSet.Name)
         End If
    End If
End Sub
vbturbo,

I tried your last comment - still doesn't work - deletes the record when clicking "No" or "Cancel".
ASKER CERTIFIED SOLUTION
Avatar of vbturbo
vbturbo
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you talking about a a ToolStripItem that represents the Delete button for the BindingSource. ?

http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingnavigator.deleteitem.aspx
vbturbo,

Sorry, I missed the propery setting.
Works great!

Thanks,

Dave
just glad you got it Dave
good stuff guys!  One quick question.  by removing the deleteitem property and setting it to None, will this ensure that the removecurrent and endedit methods will truly delete the record from the database without any further coding?  Because before it was removing the record by itself but not truly deleting the record. yes, it removes it from the dataset, but if you restart your application the record was coming back.