3

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).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
lemunk
  • 2,616
  • 12
  • 57
  • 87
  • I don't really get what your problem is... – Stormenet Mar 23 '12 at 12:44
  • the problem is when the checkbox is set to true for branch nothing happens. but if i do it for buyer it toggles perfectly. when the two panels on my form are NOT on top of each other this code works correctly. When there on top, branch doesnt work yet buyer does – lemunk Mar 23 '12 at 12:49
  • Please don't prefix your titles with "C#" and such. That's what tags are for. – John Saunders Mar 23 '12 at 12:50
  • strange I've been contacted before due to my titles not containing the prefix. so which is it? – lemunk Mar 23 '12 at 12:53
  • The designer can't tell if you intended to overlap the panels or put one inside the other. It guesses at the latter. Not the end of the annoyance, you still have trouble reaching the panels without flipping their Z-order. A better mousetrap is here: http://stackoverflow.com/questions/2798215/hide-tabcontrol-buttons-to-manage-stacked-panel-controls/2798241#2798241 – Hans Passant Mar 23 '12 at 13:18

2 Answers2

13

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.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 2
    he is changing CheckBoxBUYER not BRANCH Checked property – Renatas M. Mar 23 '12 at 12:47
  • i am only setting the panel to false when the checkbox is set to false – lemunk Mar 23 '12 at 12:47
  • yes but as ive said, when the panels are not On top of each other on the form, this code works 100%. when the panels are on top only buyer ever works – lemunk Mar 23 '12 at 12:50
  • @StevenSmith How are they on top of each other? Through the designer? If so, you have to be careful that one panel isn't actually inside the other. – LarsTech Mar 23 '12 at 12:54
  • yup exactly, in the designer, you got a tip so they dont become linked? how does one put them exactly on top of each other through designer? or will me filling out the properties manually, rectify this? – lemunk Mar 23 '12 at 12:57
  • 3
    The tip regarding the Document Outline was invaluable to me once I started running into the inadvertent nesting of panels using the designer. – cori Dec 10 '12 at 19:40
0

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

ionden
  • 12,536
  • 1
  • 45
  • 37