Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got a splitcontainer panel(panel 1 contains 7 buttons, panel2 from split container contains panels 3-9)

When button 1 is clicked it will show panel 3, button 2 when clicked will show panel 4.... but every time I skip a button it will not show the panel of the clicked button. ex. I clicked button 1 it'll show panel 3 but when I skip button 2 and click button 3 it 3 it wont display panel 5... every time I skip buttons it wont show the designated panels... #WindowsAppForm C#

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace kennyrogers
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel2.Visible = true;
            panel3.Visible = true;
            panel4.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel4.Visible = true;
            panel5.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            panel5.Visible = true;
            panel6.Visible = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            panel6.Visible = true;
            panel7.Visible = false;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel7.Visible = true;
            panel8.Visible = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            panel8.Visible = true;
            panel9.Visible = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            panel9.Visible = true;
            panel6.Visible = false;
        }
    }
}
Posted
Updated 23-Jul-17 6:37am
v2

You have answered your own question. If you look at the code, not all panel visibility states are managed for each button. eg: button6 code does not change the panel1 visibility state, and so on...

You can either use the State Design Pattern[^] or a shared method and switch statement. But for something simple like above, you could do the following:

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SetState(States.first);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SetState(States.first);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        SetState(States.second);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        SetState(States.third);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        SetState(States.forth);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        SetState(States.fifth);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        SetState(States.sixth);
    }

    private void button7_Click(object sender, EventArgs e)
    {
        SetState(States.seventh);
    }

    private void button8_Click(object sender, EventArgs e)
    {
        SetState(States.eighth);
    }

    private void button9_Click(object sender, EventArgs e)
    {
        SetState(States.nineth);
    }

    private void SetState(States state)
    {
        panel1.Visible = state == States.first;
        panel2.Visible = state == States.second;
        panel3.Visible = state == States.third;
        panel4.Visible = state == States.forth;
        panel5.Visible = state == States.fifth;
        panel6.Visible = state == States.sixth;
        panel7.Visible = state == States.seventh;
        panel8.Visible = state == States.eighth;
        panel9.Visible = state == States.nineth;
    }
}

enum States
{
    first,
    second,
    third,
    forth,
    fifth,
    sixth,
    seventh,
    eighth,
    nineth
}

** UPDATE: If you need to have more than one panel shown on a button click, you could use enum bitwise operation with the Flags attribute. Eg:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SetState(States.None);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SetState(States.first | States.third);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        SetState(States.second);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        SetState(States.third | States.second);
    }

    private void SetState(States state)
    {
        panel1.Visible = (state & States.first) != 0;
        panel2.Visible = (state & States.second) != 0;
        panel3.Visible = (state & States.third) != 0;
        // and so on...
    }
}

[Flags]
internal enum States
{
    None = 0,
    first = 1,
    second = 2,
    third = 4,
    forth = 8,
    fifth = 16,
    sixth = 32,
    seventh = 64,
    eighth = 128,
    nineth = 256
}
 
Share this answer
 
v3
Comments
BillWoodruff 24-Jul-17 21:38pm    
I'm a registered owner of the excellent DoFactory tools; your code has nothing in common with the "State Design Pattern" ... it's an example of brute-force coding; sometimes there's little choice but to things that way ... but, in this case there is a choice.
Graeme_Grant 24-Jul-17 22:44pm    
I never said that it was, only mentioned the pattern. I use patterns regularly in my own code. But as the OP was a beginner, I stated that the "following code" was an alternative (basic solution) - "But for something simple like above...". :)

The update in the solution shows the OP how to toggle multiple panels... It has "Separation of Concerns", "SOLID" principle, but yes, skipped the "State Design" pattern as it would be overkill... :)
Note 0. Note that the Panel Control in WinForms is not a full-featured Window with a handle: it does not receive Key events, it will not become Focused as other Controls will. The code in the second example shows one way to get around this by setting Focus to some other Control inside the Panel which is a "full-windowed Control."
The simplest way to do this is to, at design-time, select all your Buttons that will do something to the Panels, open the Properties explorer, select Events, and double-click on the 'Click event: now VS writes the Click EventHandler for you, and all you have to do is write a Switch statement:
private void button1_Click_1(object sender, EventArgs e)
{
    switch ((sender as Button).Name)
    {
        case "buttonl":
            splitContainer1.Panel2.Visible = true;
            panel3.Visible = true;
            panel4.Visible = false;
            break;
        case "button2":
            // do whatecver
            break;
        
        // and so on ...

        default:
            break;
    }
}
Note: 1. A slightly more complex technique: map the Buttons in the left SplitterPanel to the Panels in the right SplitterPanel using a Dictionary where the Key is a Button, and the Value is an executable function that takes a Panel as a parameter: Dictionary<Button, Action>
public Dictionary<Button, Action> ButtonToAction;

private void Form1_Load(object sender, EventArgs e)
{ 
    ButtonToAction = new Dictionary<Button, Action>
    {
        { button1, () => PanelSelect(panel3)},
        { button2, () => PanelSelect(panel4)},
        { button3, () => PanelSelect(panel5)},
        { button4, () => PanelSelect(panel6)}
    };
}

private void PanelSelect(Panel panel)
{
    // see Note #0 above
    ActiveControl = panel.Controls[0];
    panel.Controls[0].Focus();

    switch (panel.Name)
    {
        case "panel3":
            splitContainer1.Panel2.Visible = true;
            panel3.Visible = true;
            panel4.Visible = false;
            break;
        case "panel4":
            break;
        case "panel5":
            break;
        case "panel6":
            break; 
        default:
            break;         
    }
}
 
Share this answer
 
v4
Comments
Graeme_Grant 23-Jul-17 21:00pm    
Your solution suffers from the same problem that the OP has ... some panels are not being hidden when others are shown. That is what solution 1 solves.
BillWoodruff 24-Jul-17 21:32pm    
This is just a sketch to show the OP how; of course it's incomplete.
Graeme_Grant 24-Jul-17 22:46pm    
As the OP is a beginner, he needs clear guidance, however, you repeat the same error that he made in a different way.

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