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.
When user clicks any button then another object is added:
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);
}
}
}