Adding a default custom control every time any other custom control is closed in the panel

I am trying to load the default custom control every time any custom control in the same panel is closed by the user. I have a panel named MainContainer , and when the main form loads, I call the following method to load this default custom control named welcome .

public void AddUserControlWelcome()
{
    MainContainer.Controls.Clear();
    welcome.Dock = DockStyle.Fill;
    MainContainer.Controls.Add(welcome);
}

      

I have a menustrip button that calls the following method,

private void sellItemsToolStripMenuItem_Click(object sender, EventArgs e)
{
    AddUserControlSellManager();
}

      

And it is defined as

public void AddUserControlSellManager()
{
    MainContainer.Controls.Clear();
    sellManager.Dock = DockStyle.Fill;
    MainContainer.Controls.Add(sellManager);
}

      

So, there is a button on the sellManager user control that actually closes sellManager. And after that I call AddUserControlWelcome () again from MainContainer_ControlRemoved (object sender, ControlEventArgs e) , but the application crashes and I don't know why.

+3


source to share


1 answer


I think it is clear why you have this problem. MainContainer_ControlRemoved

is called not only when you delete "sell", but "welcome" too. Thus, I believe the culprit is that you are adding control over an event like MainContainer_ControlRemoved

that which you shouldn't be doing. As good as .Net is, sometimes you have to avoid using certain events for certain purposes, or you run into problems.

Try something like this. Considering that your surface can only contain one control at a time



class SurfaceManager
{
    private Control _defaultCtrl;
    private bool _currentDefault;
    private Control _surface;

    void SurfaceManager(Control _surface, Control defaultCtrl) 
    {
        _surface = surface;
        _defaultCtrl = defaultCtrl;
        _surface = surface.Controls.Add(_defaultCtrl);
        _currentDefault = true;
    }

    public Control Add(Control ctrl)
    {
        Control c = null; // Returning removed control so you can do something else with it
        if (_surface.Controls.Count > 0)
        {
            if (!_currentDefault)
                c = _surface.Controls[0];
            _surface.Controls.Clear();
        }           
        _surface = surface.Controls.Add(ctrl);
        _currentDefault = false;
        Return c;
    }

    public Control Remove()
    {
        if (_currentDefault) Return // Current is default - do nothing

        Control c = null; // Returning removed control so you can do something else with it
        if (_surface.Controls.Count > 0)
        {
            c = _surface.Controls[0];
            _surface.Controls.Clear();
        }           
        _surface = surface.Controls.Add(_defaultCtrl);
        _currentDefault = true;
        Return c;

    }

}

      

Now, in your class, instantiate this manager and use Add

or Remove

. Uninstall will automatically lead to the Welcome screen

0


source







All Articles