Reputation: 540
I am using following code to generate controls dynamically inside a FlowLayoutPanel (win forms - c#). I want to add line break after completion of inner foreach. var fullText = textBox1.Text;
List<string> listPoints = fullText.Split('#').ToList();
foreach (var listPoint in listPoints)
{
if (listPoint.Contains('^'))
{
var listTextboxes = listPoint.Split('^');
int textBoxCount = listTextboxes.Count();
int index = 1;
foreach (var listTextbox in listTextboxes)
{
CheckBox ck = new CheckBox();
ck.Text = listTextbox;
ck.AutoSize = true;
ck.CheckStateChanged += new EventHandler(this.CheckBox_CheckedChanged);
flowLayoutPanel1.Controls.Add(ck);
if (index < textBoxCount)
{
TextBox tb = new TextBox();
tb.AutoSize = true;
tb.TextChanged += new EventHandler(this.TextBox_TextChanged);
flowLayoutPanel1.Controls.Add(tb);
}
index++;
}
// code for New line break
}
else
{
CheckBox ck = new CheckBox();
ck.Text = listPoint;
ck.AutoSize = true;
ck.CheckStateChanged += new EventHandler(this.CheckBox_CheckedChanged);
flowLayoutPanel1.Controls.Add(ck);
flowLayoutPanel1.
}
Upvotes: 3
Views: 2901
Reputation: 81610
Use the SetFlowBreak method:
Control lastControl = null;
if (listPoint.Contains('^')) {
var listTextboxes = listPoint.Split('^');
int textBoxCount = listTextboxes.Count();
int index = 1;
foreach (var listTextbox in listTextboxes) {
CheckBox ck = new CheckBox();
ck.Text = listTextbox;
ck.AutoSize = true;
ck.CheckStateChanged += new EventHandler(this.CheckBox_CheckedChanged);
flowLayoutPanel1.Controls.Add(ck);
if (index < textBoxCount) {
TextBox tb = new TextBox();
tb.AutoSize = true;
tb.TextChanged += new EventHandler(this.TextBox_TextChanged);
flowLayoutPanel1.Controls.Add(tb);
lastControl = tb;
} else {
lastControl = ck;
}
index++;
}
if (lastControl != null) {
flowLayoutPanel1.SetFlowBreak(lastControl, true);
}
Upvotes: 7