Dev Learner
Dev Learner

Reputation: 93

Flowlayout Adding control in between controls dynamically

I am making a change to an existing application. I am stuck on an issue where I need to add controls in the middle of a flow panel. I have simplified the problem below.

A label and button controls are added per object. I have 3 sets of objects I add at initial screen launch.

enter image description here

When user clicks any button then another object is added:

enter image description here

However, I need the controls to be under the button where it was clicked instead of being added at the bottom. For example, the A button is clicked then the Label D and button d should appear under the A button and moving other controls down. The event is a button but could also be other events like a dropdown but the rules/behaviour are the same. is it possible to add a control in the middle of the flow panel?

Below is a sample of the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            List<Control_Info> list = new List<Control_Info>();
            list.Add(new Control_Info { Label_Name = "Test A", Button_Name = "Desc A" });
            list.Add(new Control_Info { Label_Name = "Test B", Button_Name = "Desc B" });
            list.Add(new Control_Info { Label_Name = "Test C", Button_Name = "Desc C" });

            foreach (Control_Info ci in list)
            {
                loadControlToPanel(ci);
            }

        }

        private void loadControlToPanel(Control_Info ci)
        {
            Label label = new Label();
            label.Text = ci.Label_Name;
            label.AutoSize = false;
            flowLayoutPanel1.Controls.Add(label);

            Button button = new Button();
            button.Text = ci.Button_Name;
            button.AutoSize = false;
            button.Click += new EventHandler(buttonClickEvent);
            flowLayoutPanel1.Controls.Add(button);
        }

        private void buttonClickEvent(object sender, EventArgs e)
        {
            Control_Info ci = new Control_Info{ Label_Name = "Test D", Button_Name = "Desc D" };
            loadControlToPanel(ci);
        }
    }
}

Upvotes: 0

Views: 1032

Answers (1)

Loathing
Loathing

Reputation: 5256

Use the SetChildIndex(...); method, e.g:

    Form f5 = new Form();
    FlowLayoutPanel panel = new FlowLayoutPanel { FlowDirection = FlowDirection.TopDown, Dock = DockStyle.Fill };
    f5.Controls.Add(panel);
    Label lbA = new Label { Text = "A" };
    Button btnA = new Button { Text = "A" };
    panel.Controls.Add(lbA);
    panel.Controls.Add(btnA);
    char c = 'B';
    EventHandler action = null; 
    action = (o, e) => {
        int index = panel.Controls.IndexOf((Control) o); // index of button clicked
        Label lbNew = new Label { Text = c.ToString() };
        Button btnNew = new Button { Text = c.ToString() };
        btnNew.Click += action;
        c++;
        panel.SuspendLayout();
        panel.Controls.Add(lbNew);
        panel.Controls.Add(btnNew);
        panel.Controls.SetChildIndex(btnNew, index+1);
        panel.Controls.SetChildIndex(lbNew, index+1);   
        panel.ResumeLayout(true);
    };
    btnA.Click += action;

    Application.Run(f5);

Upvotes: 2

Related Questions