Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Im working on this software for my personal use, and im currently using panels.

And I wrote this code which hides and shows the panels because im using buttons to switch between windows in the software.
I know how it works but im not understanding it fully, not the code itself, just how its functional, how its functioning together. So here is the code

C#
private void sideDashboard_Click(object sender, EventArgs e)
{
    activePan.Visible = false;
    gainpan.Visible = false;
    losPan.Visible = false;
}

private void mostActive_Click(object sender, EventArgs e)
{
    activePan.Visible = true;
    gainpan.Visible = false;

}

private void mostGainers_Click(object sender, EventArgs e)
{
    activePan.Visible = true;
    gainpan.Visible = true;
    losPan.Visible = false;
}

private void mostLosers_Click(object sender, EventArgs e)
{
    activePan.Visible = true;
    gainpan.Visible = true;
    losPan.Visible = true;
}


Let me explain..

So I have 3 windows named (Pan is short for Panel)

activePan
gainpan
losPan


And I have 4 Buttons
sideDashboard
mostActive
mostGainers
mostLosers

As you can see they are connected to eachother but what im not fully understanding is how its doing outside the code.

Like I know that I start on the sideDashboard which is the main window that you see and when ever that is being pressed it clears out all the windows it hides them so you cant see them but they are still there.

Then when you press mostActive

it shows the activePan but hides the one above? (gainPan) Why?
I tried removing that code but that made my software really buggy when pressing the buttons

C#
activePan.Visible = true;
gainpan.Visible = false;



Then we have the next button which is (mostGainers)
Which reads the code

C#
activePan.Visible = true;
gainpan.Visible = true;
losPan.Visible = false;


And there again it hides Panel above it which is
C#
losPan.Visible = false;


But Why is it hiding the one above but still showing the 2 behind it?

and then the last Panel which is

C#
private void mostLosers_Click(object sender, EventArgs e)


It just shows everything, I tried hiding the 2 below it which would be changing the code from


C#
private void mostLosers_Click(object sender, EventArgs e)
        {
            activePan.Visible = true;
            gainpan.Visible = true;
            losPan.Visible = true;
        }



To

private void mostLosers_Click(object sender, EventArgs e)
{
activePan.Visible = false;
gainpan.Visible = false;
losPan.Visible = true;
}

That didnt work it made my code very buggy!

Keep in mind that my code is fully functional at this moment, but im not sure how I solved it, Well I know how I did it I just dont understand what I did

What I have tried:

I've tried changing things from true to false to see what the outcome is but it made the code buggy and non functional, didnt make me understand how its functioning
Posted
Updated 13-Apr-16 19:54pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Apr-16 1:37am    
Debugger!

To me, it's a mystery: how it's possible to say "I wrote this code" and "i'm not understanding it fully" at the same time.
Anyway, the fragment of code you show does next to nothing; I cannot imagine what could be discussed.

—SA
BladeLogan 14-Apr-16 2:05am    
I would show you the software and the functionalities and my thoughts on how I built it if i could but unfortunatly I dont know a way that I could do that.

1 solution

OK - you have three panels which presumably occupy the same screen "real estate".
So you can only display one of them at a time.
So when you want to show a panel, you set it's Visible property to true - but that's not enough to make sure it can be seen, because one of the other two could be in the same place, and could cover it - so you have to ensure that the "competition" is hidden by setting their Visible property to false:
Which means that your three buttons need to look like this:
C#
private void mostActive_Click(object sender, EventArgs e)
{
    activePan.Visible = true;
    gainpan.Visible = false;
    losPan.Visible = false;
}
 
private void mostGainers_Click(object sender, EventArgs e)
{
    activePan.Visible = false;
    gainpan.Visible = true;
    losPan.Visible = false;
}
 
private void mostLosers_Click(object sender, EventArgs e)
{
    activePan.Visible = false;
    gainpan.Visible = false;
    losPan.Visible = true;
}

If you don't explicitly hide what you don't want to see, it will likely stay there and cover what you do want to see!
Does that make sense?

"What is a good "second option" to the "z-order" what would I want to use to display other panels?"

Don't worry about z-order for the moment - you probably don't need to play with it at the moment.
Instead, write a method which turns all your panels off, and displays one of them:

C#
private Panel ShowOnly(Panel showThis)
    {
    activePan.Visible = false;
    gainpan.Visible = false;
    losPan.Visible = false;
    if (showThis != null)
        {
        showThis.Visible = true;
        }
    return showThis;
    }


You then call it each time, passing the name of the Panel you want visible and it does the rest.
C#
private void mostActive_Click(object sender, EventArgs e)
    {
    ShowOnly(activePan);
    }
 
private void mostGainers_Click(object sender, EventArgs e)
    {
    ShowOnly(gainpan);
    }
 
private void mostLosers_Click(object sender, EventArgs e)
    {
    ShowOnly(losPan);
    }

And your "Hide them all" feature is equally simple:
C#
private void sideDashboard_Click(object sender, EventArgs e)
    {
    ShowOnly(null);
    }

That way, adding a new Panel just means adding the Visible=false line to ShowOnly, and calling it with the new panel when you want it visible.
 
Share this answer
 
v2
Comments
BladeLogan 14-Apr-16 2:04am    
Ooh! yeah that makes sense! But my code is working as it is now and its the one showed under where it say "So here is the code"

I got to say, your version looked alot more "Clean" How come mine is still working though?

On my mostActive_Click I only have

activePan.Visible = true;
gainpan.Visible = false;

and it still shows the Panel correctly
OriginalGriff 14-Apr-16 2:46am    
Because it depends on something called the "z-order" which is basically a number saying "this control is on top of those controls" so higher z-order number controls hide lower ones if they overlap.
It's not a good idea to rely on the z-order if you want to display a specific panel, but to explicitly hide the ones you don't want!

"Also, I set all my panels to visable = false in the properties in VS"
Which means that on startup none of them will be visible unless you explicitly override that in your constructor, or the Load event, or the Shown event.
That's pretty normal - you might want to save the last state when your app exits and restore it when it re-starts for example.
BladeLogan 14-Apr-16 2:48am    
What is a good "second option" to the "z-order" what would I want to use to display other panels?
OriginalGriff 14-Apr-16 3:08am    
Answer updated
BladeLogan 14-Apr-16 2:08am    
Also, I set all my panels to visable = false in the properties in VS

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900