Dynamic Children
While creating dynamic children this logic may hepls in better way. You can make use of the following code to generate Textbox and Labels dynamically.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
GetEditors(Convert.ToInt16(this.textBox1.Text == string.Empty ? "0" : this.textBox1.Text));
}
void GetEditors(int count)
{
this.panel1.Controls.Clear();
var left = 0;
var top = 0;
for (int i = 0; i < count; i++)
{
MyEditor editor = new MyEditor();
editor.label.Text = "Label" + (i + 1).ToString();
editor.textBox.Text = editor.label.Text + "'s Text";
editor.Left = left;
editor.Top = top;
if (editor.Left + editor.Width > (this.panel1.Width - editor.Width))
{
top = top + editor.Height + 5;
left = 0;
}
else
{
left = left + editor.Width + 5;
}
this.panel1.Controls.Add(editor);
}
}
}
}In the demo sample as per dynamic textbox to its relation matrix, Using the Same logic in Editor by using the following code.
public MyEditor(int EditorCount)
{
InitializeComponent();
for (int i = 0; i < EditorCount; i++)
{
TextBox text = new TextBox();
text.Width = 100;
text.Height = 30;
text.Left = this.label.Left +( 105 * (i + 1));
this.Controls.Add(text);
}
}For MyEditor I am using UserControl concept by modifiying the Access modifier of the child controls.
Regards,
Selva Ganapathy K
