Wednesday, May 2, 2012

How to attach components programmatically to GroupBox in C#

I have program which creates groupbox and label automatically. However I can't seem to attach label to groupbox as it doesn't recognize its name. It would be also helpful if the name could be dynamic, e.g. groupbox[i] where i is changing integer.



                    //instantiate and configure the groupbox
myarray.Add(new GroupBox().Name="test[i]");
System.Drawing.Point p3 = new System.Drawing.Point(8, 350 + alistcount * 41);
(myarray[count+ 2] as GroupBox).Location = p3;
(myarray[count+ 2] as GroupBox).Size = new System.Drawing.Size(340, 100);
(myarray[count+ 2] as GroupBox).Text = code;
tabPageLevel4.Controls.Add(myarray[count+ 2] as GroupBox);

//instantiate and configure the text box
myarray.Add(new Label());
System.Drawing.Point p2 = new System.Drawing.Point(12, 30);
(myarray[count+1] as Label).Location = p2;
(myarray[count+ 1] as Label).Size = new System.Drawing.Size(60, 30);
(myarray[alistcount + 1] as Label).Text = name;
test.Controls.Add(myarray[count+ 1] as Label);




No comments:

Post a Comment