Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, I made my accordion control.

code:
class Acordion : Panel
    {


        Panel panel = new Panel();
        int Y;

        public Acordion(int x, int y, int sizeX,int sizeY)
        {
            this.Location=  new Point(x, y);
            this.Size = new Size(sizeX,sizeY);

            Button expandButton = new Button();
            expandButton.Location = new Point(0,0);
            expandButton.Size = new Size(sizeX,20);
            expandButton.Click += (s, e) => { Expand(); };

            
            panel.Location = new Point(0,0);
            panel.Size = new Size(sizeX,100);

            TextBox textBox = new TextBox();
            textBox.Location = new Point(0,expandButton.Size.Height + 10);
            textBox.Size = new Size(sizeX, 10);

            textBox.BringToFront();

            this.Controls.Add(expandButton);
            this.Controls.Add(panel);
            panel.Controls.Add(textBox);

            Y = panel.Height;

        }
        public void Expand()
        {
            if (panel.Height != 0)
            {
                panel.Height = 0;
            }
            else
            {
                panel.Height = Y;
            }
        }

    }


this code works almost like I want it to behave. currently, when you click the button the panel underneath it disappears because its size is set to 0.
But the other controls underneath it do not move up.

so in this pictures all acordions are expanded.
picture link[^]

and in this one the first one is closed.
picture link[^]
how do i make it so that the second one moves up if the first one is closed ?

What I have tried:

I tried to make a methode to run through all controls in the panel and move them up. but i could not get it to work.
Posted
Updated 19-Jul-19 9:04am

To have the Controls appear from top-to-bottom ... in the order you added them to the Panel ... and have the other elements auto-size when you change their 'Height Property, or delete them, make them visible, or hide them:

1. add the newly added Controls to the host Panel's ControlCollection

2. set the newly added Controls 'Dock Property to 'Top

3. call 'BringToFront() on the newly added Controls
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class MyAccordion : UserControl
    {
        public MyAccordion()
        {
            InitializeComponent();

        }

        private void MyAccordion_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                MyAccordionElement ele = new MyAccordionElement();

                ele.SetText(i.ToString());

                this.Controls.Add(ele);
                ele.Dock = DockStyle.Top;
                ele.BringToFront();
            }
        }
    }
}
Here 'MyAccordionElement is another UserControl (code not shown).
 
Share this answer
 
Another option would be to use the FlowLayoutPanel control:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var button1 = new Button();
        var button2 = new Button();
        var button3 = new Button();
        this.flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
        this.flowLayoutPanel1.BackColor = Color.Beige;
        this.flowLayoutPanel1.Controls.Add(button1);
        this.flowLayoutPanel1.Controls.Add(button2);
        this.flowLayoutPanel1.Controls.Add(button3);
        button1.Click += ButtonCollapse;
        button2.Click += ButtonCollapse;
        button3.Click += ButtonCollapse;
    }

    private void ButtonCollapse(object sender, EventArgs e)
    {
        var btn = (Button)sender;

        if (btn.Tag == null)
        {
            btn.Height /= 2;
            btn.Tag = "collapsed";
        }
        else
        {
            btn.Height *= 2;
            btn.Tag = null;
        }
    }
}
 
Share this answer
 
Comments
BillWoodruff 20-Jul-19 4:44am    
My vote of #4: good alternative; however, adding only only Buttons is pretty weird.
No need to calculate the positions, just set the Anchor
Dock property of the controls in the panel to Top.
 
Share this answer
 
v2
Comments
BillWoodruff 20-Jul-19 4:38am    
My vote of #1: this is nonsense.
RickZeeland 20-Jul-19 5:59am    
Oops, that must have been the Dock property :)
BillWoodruff 20-Jul-19 6:05am    
Knowing you occasionally make a mistake, I feel less insecure :)

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