Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am learning VB and I tried to create a form from a second form in 2 ways. The first, by a sub that is called and checks to see if the form is already there, creates one if it isn't, but doesn't make a new one if there is one there already. This works.

In the second, I am trying to do the same, but this time, passing the name of the form to be opened. If I declare the name of the form (frm3 as New Form2), the sub works but opens many instances. If I don't declare it, I get an error that I should have declared it.

What am I missing that the sub without passing the form works correctly (creates only a single form on btn_click unless the form doesn't exist) while the other creates multiple forms on the btn_click event?
Thanks for being patient!

VB
Dim frm2 As Form2
    Dim frmName As Form2
Private Sub InitForm()
        'rem if form not created already, then show it;   If already there, do nothing
        If frm2 Is Nothing OrElse frm2.IsDisposed = True Then
            frm2 = New Form2
        End If
        'rem if invisible, show the NEW frm2 form
        If frm2.Visible = False Then
            frm2.Show()
        End If
    End Sub

    Private Sub btnShowForm2_Click(sender As Object, e As EventArgs) Handles btnShowForm2.Click
'rem use InitForm so initialization of form2 code not repeated;  shows the form
        InitForm() 
    End Sub


'rem Wanted to make same Sub as InitForm, but with a form object passed:

    Private Sub InitFormPassObj(frmName) rem  As Form2)
        'rem is form not created already, then show it;   If already there, do nothing
        If frmName Is Nothing OrElse frmName.IsDisposed = True Then
            frmName = New Form2
        End If
        'rem if invisible, show the NEW frm2 form
        If frmName.Visible = False Then
            frmName.Show()
        End If
    End Sub

    Private Sub btnShowFrm2ObjectPass_Click(sender As Object, e As EventArgs) Handles btnShowFrm2ObjectPass.Click
        Dim frm2 As New Form2
        InitFormPassObj(frm2)
        frm2.SayHello(txtName.Text)
    End Sub
    
    Private Sub btnSayHello_Click(sender As Object, e As EventArgs) Handles btnSayHello.Click
        'rem Say Hello is via Public Property in Form2 called Sub SayHello, which accepts a string
        InitForm()
        'rem SayHello is PUBLIC on Frm2
        frm2.SayHello(txtName.Text)
    End Sub
End Class
Posted
Updated 27-Aug-15 9:02am
v4
Comments
Sergey Alexandrovich Kryukov 27-Aug-15 15:04pm    
I edited your post, for proper formatting. Please click "Improve question" and see how it should be done; pay attention for "pre" element and apostrophe for VB.NET comment.
—SA

There is no such concept as "open a form" in programming; you can only close a form :-)
You don't really pass the form name, which would not make any sense. Your frmName is a reference to an instance of Form2 class (never ever use names like that; use refactoring engine to change each and every auto-generated names and avoid giving bad names yourself).

What you do makes little sense in principle. At least both your frmName and InitForm should be static (shared). Without it, you would have separate frmName for each instance of Form2. Apparently, you have little understanding of types, instances and instance members (vs static). You need to learn it.

It's possible that you are trying to combine factory and singleton patterns, or use just one of them. You are doing it wrong. Indeed, you can have a factory method like yours, but it should either be a method of some "factory" class or a static method of the form class, which you could use instead of public/internal constructor (and then you should better make all constructors private). If you need a singleton feature, read this: https://en.wikipedia.org/wiki/Singleton_pattern[^].

I saw many bad singleton implementations. This is a right one: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

See also: https://en.wikipedia.org/wiki/Factory_%28object-oriented_programming%29#VB.NET[^].

Now, for a side advice: I would recommend you to hold on UI development, as well as other as advanced or more advanced topics. First, get confident with classes, instances and OOP, using perhaps some simple console-only applications.

—SA
 
Share this answer
 
v2
Thanks, Sergey. I was just trying to do an exercise to learn how to call one form from another, varying the way I did it. This is only a hobby so all you programmers are safe- at least from me!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900