Code:
...
Dim frm2 As New Form2()
If frm2 Is Nothing Then
frm2 = New Form2()
frm2.Show()
Else
frm2.Show()
End If
...
chẳng trách code snippet này sẽ tạo ra nhiều windows vì frm2 là local variable --> đoạn condition frm2 Is Nothing luôn False
trường hợp bạn đưa frm2 ra thành public member : khi bạn close frm2 cũng là lúc frm2.Dispose() được invoke --> frm2 dọn sạch resource của nó rồi. Mà khi đó, frm2 Is NOT Nothing --> đoạn condition frm2 Is Nothing sẽ là False --> frm2.Show() sẽ tặng bạn một run-time error to tướng vì frm2 disposed rồi, chả còn gì mà show với hide.
nếu tôi nhớ không lầm thì class Form có property .Closed. Bạn có thể thay điều kiện frm2 Is Nothing thành frm2.Closed.
Bookmarks