How to catch the unsaved data before closing?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to make sure that the user cannot close (using "X") the form before
saving the data on it. Which form event should I write the control code in?

Thank you,

-Delen
 
Look at the forms Closing event.

Define a form level var that you can set to tru if your forms data is dirty
and trigger of that in the event...


Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As
System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing

If (isDirty = True) Then
e.Cancel = True

End If

End Sub
 
Durson,

There are in my opinion only two kind of options which do prevent you that a
user blames you that your program has a bug when he pushes the X and
nothing happens.

That is catching it in the closing event and than ask with a messagebox to
close or not and than set the e.cancel.

Or this nice solution from Mike, because then there is no X what in my
opinion is much nicer.

http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=248554&Page=1#248801

I hope this helps,

Cor
 
Dursun said:
I need to make sure that the user cannot close (using "X") the form before
saving the data on it. Which form event should I write the control code
in?

\\\
Imports System.ComponentModel
..
..
..
Private Sub Form1_Closing( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles MyBase.Closing
If _
MsgBox( _
"Really close?", _
MsgBoxStyle.YesNo Or MsgBoxStyle.Question _
) = MsgBoxResult.No _
Then
e.Cancel = True
End If
End Sub
///
 
I also like the no "X" option. However, I have a problem with forms that I
set the ControlBox to False so it doesn't show. I also set the StartPosition
to Manual then set the form location in code depending on the situation. The
form has the bottom 1/4 chopped off and doesn't show. If I change the
ControlBox to True, the form shows fine in the proper location to which I set
it. Any idea what causes this?
 
Dennis,

With this code while the first is false or true the form comes forever on
the same place.

\\\
Me.ControlBox = False
Me.StartPosition = FormStartPosition.Manual
Me.Height = 400
Me.Width = 400
Me.Left = 400
Me.Top = 400
////
So can you explain something more?
Cor
 
Cor Ligthert said:
There are in my opinion only two kind of options which do prevent you that
a user blames you that your program has a bug when he pushes the X and
nothing happens.

That is catching it in the closing event and than ask with a messagebox
to close or not and than set the e.cancel.

Or this nice solution from Mike, because then there is no X what in my
opinion is much nicer.

Mhm... From a usability standpoint that's a bad choice because the user
should have a way to exit the application. I'd choose the way chosen by
many other applications, that is showing a messagebox that asks the user to
save the data and dispose the data if the user replies with "No". If there
is no data to save no messagebox needs to be shown and the application can
terminate.
 
Herfried,
Mhm... From a usability standpoint that's a bad choice because the user
should have a way to exit the application. I'd choose the way chosen by
many other applications, that is showing a messagebox that asks the user
to save the data and dispose the data if the user replies with "No". If
there is no data to save no messagebox needs to be shown and the
application can terminate.
What do I see wrong?

The code you showed had not any comment from me, because that is what I
advice.

:-)

Cor
 
You are correct...this works. However, delete the line Me.ControlBox = False
and go the the IDE and set the Controlbox to False in the property box. Then
run it. You will find that the bottom 1/4 of the form does not show up.
Bottom line is if you set the controlbox to false in code, it works ok but if
you set it in the IDE property list at design time, it doesn't.
 
Dennis,

I did as you said.
Then I added a statusbar
The form shows up donw in my screan (as it should with a part hidden).
When I than drag it up, I see a full form that I can by the statusbar handle
(right on screen not the windowshandle) make smaller and larger.

Where don't we understand each other?

Cor
 
I tried creating a new project and can't seem to reproduce the error in the
new simple project. However, if I set the Startposition to Manual in code in
my large project, all seems to work well. I guess I'll just go with that.
Thanks Cor for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top