none
Reusing an instance of a form class. RRS feed

  • Question

  • Hello,
    I created an X class with inherit of the form class.
    When starting the application I define a variable: public X1 as new X.
    I show X1 and then I close it.
    When I try to reuse X1, the system tells me that it can't be used because X1 was Disposed.

    There is a way to reuse an instance of a closed form.

    Thanks.
    Friday, October 11, 2019 1:08 PM

Answers

  • It depends on how you coded things. For instance this example works with zero issue (and it's stock form).

    Public Class Form1
        Public childForm As New Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            childForm.ShowDialog()
        End Sub
    End Class

    While if we did this a runtime exception is thrown.

    Public Class Form1
        Public childForm As New Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            childForm.ShowDialog()
            childForm.Dispose()
        End Sub
    End Class

    While this first checks to see if the form has been disposed and if so recreate the form.

    Public Class Form1
        Public childForm As New Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If childForm.IsDisposed Then
                childForm = New Form2()
            End If
            childForm.ShowDialog()
            childForm.Dispose()
        End Sub
    End Class
    Keep in mind the last two code samples are not real life but do show how an exception can be thrown and how to assert for a disposed form or object and handle this case.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by _Genko_ Monday, October 14, 2019 8:28 AM
    Friday, October 11, 2019 2:01 PM
    Moderator

All replies

  • Hi

    You might use .Hide instead of .Close, or, here is some code that does what I think you are asking for.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		Dim x As New x
    		x.Show()
    	End Sub
    End Class
    Class x
    	Inherits Form
    	Sub New()
    		With Me
    			.Text = "New Form"
    			.Name = "x"
    		End With
    	End Sub
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Controls.Add(New Label With {.Name = "Label1", .Text = "New Label", .Location = New Point(20, 20)})
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, October 11, 2019 1:59 PM
    Friday, October 11, 2019 1:59 PM
  • It depends on how you coded things. For instance this example works with zero issue (and it's stock form).

    Public Class Form1
        Public childForm As New Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            childForm.ShowDialog()
        End Sub
    End Class

    While if we did this a runtime exception is thrown.

    Public Class Form1
        Public childForm As New Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            childForm.ShowDialog()
            childForm.Dispose()
        End Sub
    End Class

    While this first checks to see if the form has been disposed and if so recreate the form.

    Public Class Form1
        Public childForm As New Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If childForm.IsDisposed Then
                childForm = New Form2()
            End If
            childForm.ShowDialog()
            childForm.Dispose()
        End Sub
    End Class
    Keep in mind the last two code samples are not real life but do show how an exception can be thrown and how to assert for a disposed form or object and handle this case.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by _Genko_ Monday, October 14, 2019 8:28 AM
    Friday, October 11, 2019 2:01 PM
    Moderator
  • Reusing of objects is mostly expensive because of all the extra code to reset everything. 

    If you use classes you simply make a new one, with the already in the sub new of the class included code then everything is again set as new.

    In time and memory use it will mostly make no difference. 

    Be aware that the form class is a very special class in VB. There is a lot included only in this class, because of compatibility with previous versions of VB. Also it makes extensive use of the Application Framework. 


    Success
    Cor

    Friday, October 11, 2019 4:25 PM
  • I'm not sure if this is the same in forms, but it is the confusing part in WPF.

    dim x as new Window
    x.ShowDialog()
    x = nothing

    In Windows forms the correct way is as follows.

    Dim childForm As New Form2
    Try
        childForm.ShowDialog()
    Finally
        childForm.Dispose()
    End Try

    Or

    Dim childForm As New Form2
    Try
        childForm.ShowDialog()
    Finally
        childForm = Nothing
    End Try
    There are some differences under the covers but for a simple form either or of the above are fine.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 11, 2019 5:41 PM
    Moderator
  • Thanks to all for the help.

    Many thanks.

    Monday, October 14, 2019 8:30 AM