How to dispose a form while restarting another form? RRS feed

  • Question

  • Hi All,

    I have two forms as Form1 and Form2. The Form1 is the startup form which has a button to show the new form (Form2). When I start the project in debugging mode and click on the button to open the Form2 it is working fine. below is my code to open the new form,

    'In Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Form2.ShowDialog() End Sub

    When, I restart the form1 with the form2 is in opened state (i.e. showing), the Form2 was not disposed. So, one of my dictionary collection returns key already exist exception, though I have checked the not contains condition for the key.

    So, how to dispose the form2 on debugging mode when Form1 is restarted?

    Please share your valuable suggestions to resolve the issue.




    Tuesday, February 6, 2018 12:00 PM

All replies

  • Arulpriya, 

    Your question is not C# but VB.

    I've asked if it will be moved using the abusive button which has not only the meaning abuse.

    However, a showdialog opens a modal form (a dialog, when it is opened you cannot use form1 until the user clicks the OK or other buttons and then it closes direct)

    Therefore the problem is not in the part of the code you show. (Do you have a threading part which can cause this?)

    Success Cor

    Tuesday, February 6, 2018 12:46 PM
  • Did you create the form as follows 

    Dim f As New Form2 ? If not you are using the default instance of the form and should use a new instance.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 6, 2018 1:30 PM
  • I don't know what you mean by restarting the form. As best as I understand what you are doing you do not need to restart the form.

    Sam Hobbs

    Wednesday, February 7, 2018 12:53 AM
  • Hi

    What do you use on Form2 to re-open Form1? A Button perhaps?Show the code for whatever it is that re-opens Form1.

    Do you have any code in Form2 FormClosed event handler? (or FormClosing)

    What is the offending 'dictionary collection returns key already exist', show the code that is relevant to the Dictionary.

    You may need to show a lot more code to get some help.

    Regards Les, Livingston, Scotland

    Wednesday, February 7, 2018 1:22 AM