Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Control Value before another Control value in C#

I have a "FlowLayoutPanel" and want to add series of "UserControl" to it:

mainPanel.Controls.Add(fx);

Every new usercontrol added after old one, I want to add new usercontrol before the previous usercontrol that was added how could I do this? I didn't find any functionality like mainPanel.Controls.AddAt(...) or mainPanel.Controls.Add(index i, Control c) or mainPanel.Controls.sort(...) or ... .

like image 931
Am1rr3zA Avatar asked May 09 '12 13:05

Am1rr3zA


2 Answers

You can use the SetChildIndex method. Something like (maybe you need to fiddle with the indecies):

var prevIndex = mainPanel.Controls.IndexOf(previouslyAdded)
mainPanel.Controls.Add(fx);
mainPanel.Controls.SetChildIndex(fx, prevIndex); 
like image 114
nemesv Avatar answered Nov 12 '22 21:11

nemesv


by the sounds of it you want to change the flowdirection attribute so that newest controls added are added to the top

flowLayoutPanel1.FlowDirection = FlowDirection.BottomUp;

or you could

 Label label1 = new Label();
 flowLayoutPanel1.Controls.Add(label1);
 label1.BringToFront();
like image 4
General Grey Avatar answered Nov 12 '22 23:11

General Grey