lemunk
lemunk

Reputation: 2636

winforms panels overlapping bugged

Oh wow is this one annoying me. Using c#, winforms, visual studio 2010 ulti.

Here's my code:

private void CheckBoxBranch_CheckedChanged(object sender, EventArgs e)
{   

    if (CheckBoxBranch.Checked == true)
    {
        panelBranch.Visible = true;
        //panelBranch.Parent = null;
        //PanelBuyer.Parent = null;
        //panelBranch.SendToBack();
        //PanelBuyer.SendToBack();
        PanelBuyer.Visible = false;
        CheckBoxBuyer.Checked = false;
        this.Refresh();
    }
    if (CheckBoxBranch.Checked == false)
    {
        panelBranch.Visible = false;
        this.Refresh();
    }

}

private void CheckBoxBuyer_CheckedChanged(object sender, EventArgs e)
{
    if (CheckBoxBuyer.Checked == true)
    {
        panelBranch.Visible = false;
        PanelBuyer.Visible = true;
        CheckBoxBranch.Checked = false;
        this.Refresh();
    }
    if (CheckBoxBuyer.Checked == false)
    {
        PanelBuyer.Visible = false;
        this.Refresh();
    }
}

now this code works fine, no problems, as long as the panels are not on top of each other. When this happens panel-Buyer works fine, Panel-Branch never shows at all.

Now I think this has something to do with the branch panel becoming a child of the buyer panel thus when my logic runs for buyer it applies itself to branch too.

I could transform there positions each time like a rotation but that's a lot of extra code.

Is there a way to solved this headache nice short sweet and simple one liner? (I wish).

Upvotes: 3

Views: 4791

Answers (2)

ionden
ionden

Reputation: 12786

I guess the problem is because one panel sits on top of the other becoming a child of the other panel that's why you can't set it visible to true. To check that you can right click on your panel and if you have the option to select the other panel then it's clear that the panel has the other panel as his parent

Upvotes: 0

LarsTech
LarsTech

Reputation: 81675

Make sure one panel is not inside the other panel.

Easy way to avoid it is by setting the location property manually in the designer. Do not use the mouse to drag and drop the controls in place.

You can also use the View - Other Windows - Document Outline window to make sure the panels are not inside each other.

Upvotes: 13

Related Questions