duplicate Form

pekt2s

Member
Joined
Apr 26, 2007
Messages
15
Programming Experience
Beginner
Hi. i have a problem on form showing. This is my problem. i made a program that if i click a button form1 will show but if i click it again it, form1 will show again. i want to know how to trapped if the form1 is still active form1 will not show again. i am currently using VB.Net 2003. can you pls help me on this matter. Thanks. I'll wait for your answer.

pekt2s!!!
 
Keep a class level reference to the form you created, in button click you can check if this is Nothing or if it's disposed, which requires you to to create a new instance, otherwise you activate the current.
VB.NET:
Private f As Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If f Is Nothing Then
        f = New Form
        f.Show()
    ElseIf f.IsDisposed Then
        f = New Form
        f.Show()
    Else
        f.Activate()
    End If
End Sub
 
duplicate Form on VB.Net 2003

Hello,

Use the code below to achieve the desired:

Dim frm As New Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If frm Is Nothing Then
frm = New Form2
End If
frm.Show()

End Sub

Regards,
Dave

Dave Traister
Software Engineer
ComponentOne LLC
 
That won't work, DaveT. If that form is closed it is disposed, but you still hold the reference, so frm is not Nothing and you can't call Show on a disposed form, you get ObjectDisposedException. You have to check IsDisposed as the code I posted. Also, if the form is still open calling Show will not activate it, you have to call Activate method to do that.
 
Back
Top