closing and re-opening the same form

Grinski

Member
Joined
Apr 16, 2010
Messages
7
Programming Experience
Beginner
I have a form called frmAddStudentData , being used by students one time only when they enrol and when the user clicks save, i want to save the data, and make sure that everything is wiped clean because the next student should never see what the last student did (some of it is confidential).

I currently have a ClearAll sub which sort of works but i would think

Me.Close()
Dim studentDetails As frmAddStudentData = New frmAddStudentData()
studentDetails.ShowDialog()

would close the first form and open a fresh copy? instead it just creates a new copy. What am i doing wrong? Should i have a variable that points to the first form, and close that instead like

dim ToBeClosed = me 'not sure how to code this
Dim studentDetails As frmAddStudentData = New frmAddStudentData()
studentDetails.ShowDialog()
ToBeClosed.Close

Thanks for your help!
 
There's no need to go to any trouble clearing anything. You simply create a new form each time you want to show one and destroy it once it's closed.
VB.NET:
Using dialogue As New frmAddStudentData
    If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
        'Do whatever.
    End If
End Using
The dialogue is implicitly disposed at the End Using line. That's all you need.
 
Ok, just trying to get my head around this. I have a login form which runs in the background and if the administrator logs in as a student it
runs

Dim frmAddDetails = New frmAddStudentData()
frmAddDetails.ShowDialog()

Does the frmAddDetails save button need to send data back to the login form telling it "Windows.Forms.DialogResult.OK" or equivalent? I need to keep a copy of the student form open at all times until the quit button is clicked. So clicking the "Save" button on the student form has this code:

Dim returnValue As DialogResult
returnValue = MessageBox.Show("Are you sure you want to save?", "Save student", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

If returnValue = Windows.Forms.DialogResult.Yes Then
'TODO SAVE DATA
'TODO 'Clear Form

I'm not sure how to pass data between forms but i think it involves 'shared' variables and can research that if that's the direction i should be headed in?
 
I'm not sure I fully understand your design but that's by the by. If you're not closing the form then you have no choice but to clear it, so that's exactly what you should do.

A dialogue doesn't need to return a DialogResult to its caller unless the caller will make use of it. That said, a dialogue always returns a DialogResult anyway. If you call Close then that's equivalent to setting DialogResult to Cancel. If you want some other value then you explicitly set DialogResult to that and don't call Close.

It's not appropriate to prompt the user for confirmation on a save. Closing without saving is something that should be confirmed but when the user requests a save then you should save, assuming that the data is valid.

Passing data to a form is exactly like passing data to any object, because a form is just like any object. You pass data in by setting a property or calling a method and passing an argument. You get data out by getting a property or calling a method and getting the return value. For more info, follow the second link in my signature.
 
Back
Top