Results 1 to 15 of 15

Thread: [RESOLVED] Close / Hide / Dispose A Form Issue

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Resolved [RESOLVED] Close / Hide / Dispose A Form Issue

    Hello all

    I have a little issue.

    I have two forms in my app (Form1 & Form2)
    when the button to show form2 is clicked and form2 is loaded an operation is performed.
    if form2 is closed/hide and then reopend I want the "form_load" function to execute but this does not seem to be the case.

    I tried this

    (from form2)

    Form1.show()
    me.dispose

    but this throws and exception on the windows PC.

    All I want to do is have form2 refreshed/reloaded anytime it is called?

    Help?

  2. #2
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Close / Hide / Dispose A Form Issue

    Try Me.Hide() instead of dispose

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Close / Hide / Dispose A Form Issue

    It still won't trigger the Load event.

    You will only get the load event when the form loads for the first time. Your example has me a bit baffled, since I don't understand the order. If you had Form1 show Form2, and when you were done with Form2 you closed it, then created a new instance of Form2 and displayed it, you'd get the Load event to run. However, you would also be showing a totally new instance, which may not be what you want if you want the form to reappear the way you left it. If you aren't willing to create a new instance of Form2 each time you want to show it, then you'll want to move the activity out of Form Load and into something else. The VisibleChanged event seems like the only one that works well for this, though Activated may be what you want in some situations.
    My usual boring signature: Nothing

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Close / Hide / Dispose A Form Issue

    I'm willing to bet my right eye he is using the default instance.

    Quote Originally Posted by gmack View Post
    All I want to do is have form2 refreshed/reloaded anytime it is called?
    Then create a new instance of the Form every time you want it as Shaggy suggested.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: Close / Hide / Dispose A Form Issue

    Ok that makes sense... Thanks for the input.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Ok I thought I had solved this but NOPE.

    I still have the issue with closing/opening a form.

    I have a windows form (lets call it Form2) that i open from form1.

    Form2 has a DGV and does some caluculations and such from the DGV rows etc.

    when i open form2 from form1, for the first time, form2 works fine.

    when i close form2 (me.close or me.hide) then from form1 reopen form2 i receive errors from my DGV.
    It seems that somehow It is not reinitializing (if thats the right term) the form2.

    I have tried this to open form2

    dim myform2 as new form2
    myform2.open()





    HELP?

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Whats the error ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    The error is an "index out of range" error happening when I add columns to the DGV and change the values in the cells.

    it seems as if form2 has allready loaded and its trying to compound my DGV calc & adds, even though I disposed of it.
    If i just hide it, then if the user changes values in form1 when form2 is re opened , it does not recalculate.

    argh....

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    So, I gave UP.

    instead I added a "REFRESH" button the form2 to refresh the DGV. if a user changes data in form1 then a flag is set & the refresh button(on form2) is enabled. now i only Hide form2 when user closes....

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Is this Form opened using ShowDialog by any chance ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Quote Originally Posted by Niya View Post
    Is this Form opened using ShowDialog by any chance ?
    no, I have never used that? what does it do?

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Well never mind. Closing a form opened by ShowDialog doesn't dispose of it. That's why I asked. Come to think of it, if you did what I told you to do, which is to create a new instance every time, you should have a problem with Form's old state clashing with the your attempts to initialize it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Quote Originally Posted by Niya View Post
    Well never mind. Closing a form opened by ShowDialog doesn't dispose of it. That's why I asked. Come to think of it, if you did what I told you to do, which is to create a new instance every time, you should have a problem with Form's old state clashing with the your attempts to initialize it.
    Well I sure tried that

    DIM myForm as New Form2()
    Myform.show

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Ok....are you doing that every time you open the Form ? Make sure now. Go and check your code and make sure there isn;t any place where you're just calling Myform.Show without setting Myform to a new instance.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] Close / Hide / Dispose A Form Issue

    Ultimately, it comes down to this:

    When you create a new instance of the form, you are creating a new object. It won't be retaining any values from any other instance unless you made them Shared (in which case, all the instances share a single copy of that item). You certainly didn't make the DGV shared, and it doesn't seem likely that you made anything else shared without some good reason. Therefore, if, when you call that MyForm.Show, you should be going through the load event, and everything should be in its initial state because MyForm is a new object. You can see what is happening by putting a breakpoint on MyForm.Show and stepping forwards to see what happens. If the DGV is not in its initial state, then you have managed to entangle them in some way that you haven't told us about. Of course, you probably didn't tell us because you didn't realize you had done it, but that is what you are looking for.
    My usual boring signature: Nothing

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