Results 1 to 12 of 12

Thread: [RESOLVED] [2008] Problem closing/opening forms

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Norwich, UK.
    Posts
    304

    Resolved [RESOLVED] [2008] Problem closing/opening forms

    Hi,

    I have Form1 which contains a button which opens Form2. Form2 contains a datagrid. I have coded it, so that when an item is double-clicked, it opens the database record which is shown on Form1.

    (Form1 is the database showing names, addresses etc, Form2 is a datagrid that shows everything in the database, allowing one item to be selected and load the details into Form1 - hope this is making sense).

    Anyway, what happens is this...

    1) Either when double-clicking my grid item, the whole application closes down (because I want to close Form2 after the item is selected)

    OR

    2) It loads the details into Form1, but opens a new instance of Form1 - thus leaving me with the original Form1 which is blank (which I want to close without closing the whole app), a new populated Form1 (which is correct) and grid in Form2 (which I wanted to close down).

    Can anyone help with this please?

    The code I am using at the moment is as per item (2) above:

    Code:
    Dim Form1 As New Form1
            MainForm.GetRecordFromGrid(intAccId)
            MainForm.ShowDialog()
            Me.close
    I've tried all sorts of variations form1.close, form1.hide and in different orders - always getting different results but never what I actually want.

    Thanks.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Norwich, UK.
    Posts
    304

    Re: [2008] Problem closing/opening forms

    Sorry, code typo above... should actually read

    vb Code:
    1. Dim Form1 as New Form1
    2. Form1.GetRecordFromGrid(intAccId)
    3. Form1.ShowDialog()
    4. Me.close

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Problem closing/opening forms

    you don't need to open a new form1. use the form1 you already have open
    remove this line:

    vb Code:
    1. Dim Form1 as New Form1

    + this line:

    vb Code:
    1. Form1.ShowDialog

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Norwich, UK.
    Posts
    304

    Re: [2008] Problem closing/opening forms

    Thanks but it doesn't work. I get this error:

    Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.
    I also need to run a procedure contained in Form1, passing the variable from Form2.

    If I open a new form, it executes the variable and populates the form's text boxes. If I go to the existing Form1 it passes the variable (confirmed by using msgbox() to see if it's there) but doesn't populate the text boxes.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Problem closing/opening forms

    if it works with a new form, go with that.
    just close the first instance of form1 when you open form2, then use a new instance of form1 to display your records

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Norwich, UK.
    Posts
    304

    Re: [2008] Problem closing/opening forms

    I can't do that, because closing the first instance of Form1 closes the entire application... this is the whole problem.


  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Problem closing/opening forms

    Quote Originally Posted by ShiftySid
    Thanks but it doesn't work. I get this error:



    I also need to run a procedure contained in Form1, passing the variable from Form2.

    If I open a new form, it executes the variable and populates the form's text boxes. If I go to the existing Form1 it passes the variable (confirmed by using msgbox() to see if it's there) but doesn't populate the text boxes.
    ok. don't use
    vb.net Code:
    1. form1.showdialog

    as form1 is already open. try it that way then let us know what the problem is with the procedure that doesn't run

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Norwich, UK.
    Posts
    304

    Re: [2008] Problem closing/opening forms

    I've resolved it.

    Code extract for button that calls Form2 is

    Code:
    Me.Hide()
    Dim Form2 As New Form2
    Form2.ShowDialog()
    Code in Form2 (once item in datagrid is double clicked) closes form2 and calls a new Form1 as follows, then executes the procedure.

    Code:
    Me.Hide()
    Dim Form1 As New Form1
    Form1.GetRecordFromGrid(shrAccId)
    Form1.ShowDialog()
    This seems to work fine. Not sure about the implications of using HIDE however. Presumably, if the app isn't closed down after a certain period of time, the user will end up with several hidden Form1's, hogging system resources?

    Anyway, does what I need it to do for now - so that's one for another day.

    Cheers all.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] [2008] Problem closing/opening forms

    you can change your projects settings so the application exits when the last form closes, instead of when the startup form closes. so you could close all those hidden forms.

    the setting is in Project-->Properties - application tab

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Norwich, UK.
    Posts
    304

    Re: [RESOLVED] [2008] Problem closing/opening forms

    That was something I tried earlier, but it still closes down the whole application - unless the Project | Properties options don't take effect in debug mode and only work when compiled?

    There's only one user of the app, and that's my other half so I shall leave it as it is! )

  11. #11
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: [RESOLVED] [2008] Problem closing/opening forms

    Have you also tried out My.Forms.Form1.Show etc?

    Usually that way gets rid of the problems you seem to have.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Norwich, UK.
    Posts
    304

    Re: [RESOLVED] [2008] Problem closing/opening forms

    Bingo! It does solve the problem - thank you.

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