Jump to content
  • Advertisement
Sign in to follow this  
phil05

C# die-hards, I need some help with form communication

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm pretty good with one form, but after I add a new form to the project, things get nasty. For example, in my first form (frmMain) it loads a new constructor which also opens the second form (frmInit). frmInit acts like a flash window that does some networking initialization. That's fine and dandy. After it's done, it should close itself with this.Close(); Unfortunately, it throws an error at me saying it can't do this when the object is disposed. Error: Can not access a disposed object.\r\nObject name: 'frmMain'. Here's the code in action:
        // loads initForm
        public frmMain()
        {
            InitializeComponent();
            frmInit initForm = new frmInit();
            initForm.Show();

        }
frmInit initializing itself and displays progress bar of networking progress.
        public frmInit()
        {
            InitializeComponent();
            this.Show();
            this.Update();
            StartProgressBar();
        }
At the end of the progress bar before it closes, it attempts to close the form.

this.Close();

Now, this.Close() can't close because of this constructor problem. I'm not entirely sure how forms communicate with each other in C#. It is definitely tricky. Makes Win32 API seem easy, but this is a good learning experience here. Please help if you can. Thanks, Phil

Share this post


Link to post
Share on other sites
Advertisement
My gut feeling would be because you are trying to close the form whilst it's still in it's constructor - that just feels wrong to me (trying to kill it before it's even been fully created) - but then again, I'm still playing with C#, so take that with a pinch of salt.

One question though - if it's just an automated process you want to run, can't you just add it as a class - do you really need the functionality encapsulated in a form?

Jim.

Share this post


Link to post
Share on other sites
I don't know. I'm lost after my first post. You know, I may just create this game in a console so I can practice using OO instead of the extra windows crap. Sure, I'd like to understand my question still, but all of this seems overwhelming for a big project right now for me. I know C++, Win32, etc, but this whole .NET thing needs a clear understanding of OO. It's very tricky working in complete objects.

Share this post


Link to post
Share on other sites
Part of the problem could be because you are trying to do a initForm.Show(); after the constructor has run. Also your error says that it's coming from frmMain where this line of code is.

You don't need that second Show() because the constructor is showing itself. If removing teh initForm.Show() doesn't help, please try finding exactly which line is throwing the error.

Share this post


Link to post
Share on other sites
Wow, removing Show() worked :) All bugs are gone. Thanks!

Since this thread is based on form communication, how is it done correctly? Say you want to pass information to a second form, and that form sends information back. Is this usually done through get/set properties?

Share this post


Link to post
Share on other sites
In c# Form is just an object. How do you communicate with regular class object ?
get and set should be fine solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!