0

When I close Form2 it won’t open up again after.

I am using a button click to open up another form. Right now I just have:

Dim Form2 As New Form2

Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
   Form2.Show()
End Sub

Now whenever I close the form I cannot open it again.

Joundill
  • 6,828
  • 12
  • 36
  • 50
  • Do you want to open a new instance of the form, or are you expecting it to still show whatever state it had when it was closed? – David Jan 03 '23 at 16:47
  • @David The same state it was in when closed. – Bailey Nygard Jan 03 '23 at 16:51
  • @BaileyNygard You will need some way to save the state, and a method to reinstate it. Do you want the state to persist every time the program is run or just for the current run? – Andrew Morton Jan 03 '23 at 17:30
  • @AndrewMorton Just the current run – Bailey Nygard Jan 03 '23 at 17:54
  • @BaileyNygard Instead of closing Form2, could you hide it? [Hide form instead of closing when close button clicked](https://stackoverflow.com/q/2021681/1115360) is in C#, but hopefully you'll be able to figure out how to do the same in VB.NET. – Andrew Morton Jan 04 '23 at 17:47

1 Answers1

1

Like this

Private MyForm2 As Form2
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
       If MyForm2 Is Nothing OrElse MyForm2.IsDisposed Then
        MyForm2 = New Form2
    End If
    MyForm2.Show()
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33