CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2001
    Posts
    155

    new creates 2....

    I have the following code in a command button:


    private Sub Command1_Click()
    Dim n as Form1
    set n = new Form1
    n.show
    End sub



    But when i click on the button it creates 2 new forms the first time.
    how can i solve this?

    --Ant
    --------------------------------------------------
    check out my newest freeware
    E-mail me at: [email protected]
    for the address

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: new creates 2....

    Hmm, I got this problem myself, and it was because somewhere I reffered to the form directly, not to the instance of the form. This makes that another instance of the form was created.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    May 2001
    Posts
    155

    Re: new creates 2....

    How am i doing that?

    --Ant
    --------------------------------------------------
    check out my newest freeware
    E-mail me at: [email protected]
    for the address

  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: new creates 2....

    Your sample creates only one additional form on my computer. Running your sample should result in having two forms afterwards, The original and the newly created copy.

    John G

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: new creates 2....

    Or you have the commandbutton in form1, and so you have two as 1 is the first where command clicked is and second is the new one, or you called click twice
    (maybe once with clicking, and a second via code setting command1= true)
    try adding a secod form, and modifying your click code this way:

    Dim n as Form2
    set n = new Form2
    n.Show




    how many Form2 do you have now?
    Of course, using New keyword you will have a new form each time. If you want only one no matter how many time you click on command button, do the following:

    Dim n as Form1
    set n = Form1
    n.Show




    You have to provide a way to set n to nothing. Here may be a way to do it:

    option Explicit
    'General declaration
    Dim n as Form2

    private Sub Command1_Click()
    'only if it is not already set. However, it will not cause an error
    'if not tested
    If n is nothing then
    set n = Form2
    End If

    n.Show

    End Sub

    private Sub Form_Unload(Cancel as Integer)
    Dim f as Form
    for Each f In Forms
    If f.Name <> me.Name then
    Unload f
    set f = nothing
    End If
    next
    If Not n is nothing then
    'set the n form_variable to nothing
    'to free memory
    set n = nothing
    End If
    End Sub





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: I will become a Joker here...

    Sorry. Did not see all answers...

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured