Lucas
Lucas

Reputation: 3715

Remove the windows form controls on exit

I'm adding the form controls on loading the form manually:

Me.FieldI = New TextBox()
Me.FieldI.Location = New System.Drawing.Point(50, 10)
Me.FieldI.Name = "FieldI"
Me.FieldI.Size = New System.Drawing.Size(40, 20)
Me.FieldI.TabIndex = 5
Me.Conversion.Controls.Add(Me.FieldI)
[..]

When I close the form window and reopen it, the control is still there (with the old .Text content , because its an textbox in this case).

I would like to remove the controls that have been created while form loading on the form close event, to prevent doubling the elements on my form.

How can I achieve this?

edit

Form closing code looks following (just showing up the main form back):

Private Sub Form1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
    Main.Show()
End Sub

Upvotes: 0

Views: 283

Answers (1)

Jack Pettinger
Jack Pettinger

Reputation: 2755

The problem here is that the form is not being disposed, so when you open it again the controls are still there from the last time it was opened.

Try the following:

Using frm = New subForm()
  frm.ShowDialog()
End Using

The variable frm will be disposed after the using.

Also...

You can also provide feedback from a dialog, to check whether the form was successful or not. For example:

Dim frm As New subForm()
If frm.ShowDialog = DialogResult.OK Then
  'YAY!
Else
  'Something failed
End If

Upvotes: 1

Related Questions