Cannot access a disposed object?

Viewed 75647

I have a countdown Timer form - on the first form the user will enter the countdown time - warning times, end message, etc. There are also two Radio buttons (Max/Min) and depending on which is selected they will open a new Max or Min form where the time will actually start to countdown. It is working fine and counting down as I expect. However, if I exit the Max or Min form and try to run again with new times I get the error. The code is below - note the comment out ShowDialog(this); was something I tried - it let me close and open the new forms ok but it did not actually start the countdown. UpdateLabels is the function that does the updating of Labels.

                bool Max = rbMax.Checked;
                if (Max == true)
                {
                    //_Max.ShowDialog(this);
                    _Max.Show();

                }
                else
                    //_Min.ShowDialog(this);
                    _Min.Show();

                UpdateLabels();
            }

I also tried the following which I read online as a possible solution but it also did not work...

    private void Max_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.Hide();
        this.Parent = null;
    }

Can anyone help me out - I can post the UpdateLabels function if needed. I am pretty new to UI C# development so any help would be great. Thanks.

6 Answers

create new instatnce if object is not available

if(frmRGB==nullptr || frmRGB.IsDisposed==true ) { frmRGB= new Form(); }

Create Object inside button click event like this

private void btn_supplier_order_Click(object sender, EventArgs e)
        {
            form_supplier_order so = new form_supplier_order();
            so.Show();
        }
Related