user69668
user69668

Reputation: 3

How to completely destroy a windows form while leaving it.?

I am pretty much new to visual basic and I have this thing bragging me. I have two forms in my windows application on selecting some properties in first form and them clicking on proceed then second form loads. I have written some code to execute while second form is loading according to the type of radio button checked on first form.

Below is the code for Load event on second form.

         `If Selectdisplay.rdbtnmps.Checked = True Then
        LoadFromExcelForRDIToolStripMenuItem.Visible = False
        LoadFromExcelForTIToolStripMenuItem.Visible = False
        Label1.Text = "Prediction of MPS in SINTER PLANT 4"
        Chart1.Series(2).Enabled = False
        Chart1.Series(3).Enabled = False
        Chart1.Series(4).Enabled = False
        Chart1.Series(5).Enabled = False
    ElseIf Selectdisplay.rdbtnti.Checked = True Then
        LoadFromExcelToolStripMenuItem1.Visible = False
        LoadFromExcelForRDIToolStripMenuItem.Visible = False
        Label1.Text = "Prediction of TI in SINTER PLANT 4"
        Chart1.Series(0).Enabled = False
        Chart1.Series(1).Enabled = False
        Chart1.Series(4).Enabled = False
        Chart1.Series(5).Enabled = False` 

"select display" being the first form. so now when I exit the second form and return to the first form the properties I set above when the second form is loading for first time are being retained and I want them to reset in another words I want to destroy the second form completely when I exit it so that I can load it freshly by checking different radio buttons in first form.

Thank you for reading Thanks in advance.

Upvotes: 0

Views: 2116

Answers (3)

inquisitive
inquisitive

Reputation: 3639

you said in the comments that the second form shows like a modal dialog. i would prefer in form1:

Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click
    Using frm2 = new Form2()
        frm2.ShowDialog()
    End Using
End Sub

also, in form2, i would refrain from If Selectdisplay.rdbtnti.Checked = True Then type calls. dont reference a form by its static name. rather use actual references. So in form2, i would have:

Public SelectdisplayForm as Form

modifying the Button1_Click code:

Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click
    Using frm2 = new Form2()

        ' --- NOTE THIS LINE ---
        frm2.SelectdisplayForm = me

        frm2.ShowDialog()
    End Using
End Sub

and then in form2, whenever you want to reference anything from form1, use the local field (variable) not the form class name.

                 |--|
 If SelectdisplayForm.rdbtnmps.Checked = True Then
                 |--|
     do something..
                     |--|
 ElseIf SelectdisplayForm.rdbtnti.Checked = True Then
                     |--|

     do other thing..

 End if   

Upvotes: 2

Archlight
Archlight

Reputation: 2079

The simple way is just to call form.Close() before accessing the document.

Selectdisplay.Close()
If Selectdisplay.rdbtnmps.Checked = True Then
....
ElseIf Selectdisplay.rdbtnti.Checked = True Then
....
end if

Upvotes: 0

Sean Airey
Sean Airey

Reputation: 6372

You could try hooking in to the FormClosing event (documentation here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx).

So in your second form add a handler like this:

Protected Sub SecondForm_FormClosing(sender As Object, e As EventArgs) Handles Me.FormClosing

    'add code to change selected radio buttons etc.

End Sub

This event handler will be called as the form is closing. You could also use the FormClosed event in much the same way (documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx).

Upvotes: 0

Related Questions