Question X button

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
I am trying to close my application and I have coded on the form closing, form closed events to call an exit form which asks the user for confirmation to exit or not. All works fine. When I press the X button on the winform things act different. On the main form where I press the X btn I store some user ID data that I use throughout the app. If I press cancel on the exit form that data is lost from the main form and cannot be used afterwards. So if the user pressed by mistake the X btn and then presses cancel on the exit form the application loses some stored data and many functions stop working. Any clues? Thanks for the replies
 
If all you doing is asking whether it is ok to close the app, I would use a messageBox set with Ok and Cancel buttons, then you control what happens based on the button selected.
VB.NET:
Expand Collapse Copy
If MessageBox.Show("Is it Ok to close the app?", "Save settings!", MessageBoxButtons.OkCancel) = DialogResult.Ok Then
   'do something
Else ...
End If
 
Thanks for the replies. In the code of the form I call the 'exit1' form to logout and do some operations on the db. this is the code in the exit1 form :

VB.NET:
Expand Collapse Copy
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.accdb;Persist Security Info=False;")
        conn.Open()

        Dim sql1 As String = " update Users set Active = '0' where UserID = " & Me.TextBox1.Text & " "
        Dim cmd1 As OleDbCommand = New OleDbCommand(sql1, conn)
        cmd1.ExecuteNonQuery()
        cmd1.Dispose()
        conn.Close()
        conn.Dispose()

        End
        Application.Exit()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Calendar1.useridTextBox.Text = Me.TextBox1.Text

        Calendar1.Show()

        Me.Close()

    End Sub
End Class

And this is how I call it :

VB.NET:
Expand Collapse Copy
  Exit1.TextBox1.Text = Me.useridTextBox.Text
        Exit1.Show()
        Exit1.Button1.Focus()
 
So why not use something like this in your closing event, simpler, yes?
VB.NET:
Expand Collapse Copy
If MessageBox.Show("Is it Ok to close the app?", "Save settings!", MessageBoxButtons.OkCancel) = DialogResult.Ok Then
   Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.accdb;Persist Security Info=False;")
   conn.Open()
   Dim sql1 As String = " update Users set Active = '0' where UserID = " & Me.useridTextBox.Text & " "
   Dim cmd1 As OleDbCommand = New OleDbCommand(sql1, conn)
   cmd1.ExecuteNonQuery()
   cmd1.Dispose()
   conn.Close()
   conn.Dispose()
   End [COLOR="Red"]<< don't ever do this again[/COLOR]
Else
   e.Cancel = True
End If
If you need a more options you can use YesNoCancel buttons. :cool:
 
Did you put the code in the form_closing event like I said? This will cancel the form closing if set to true.
 
I would also advise calling your connection code from a sub procedure to make your code more readable, and to also be able to use it again.
 
It is an incomplete syntax structure.
VB.NET:
Expand Collapse Copy
End Sub
End If
End Class
...
As it suggests, it is for ending a defined code block.
 
End Statement help article explains why you should not use it.
 
Back
Top