Here I’ve tried to demonstrate how to create controls at runtime in C#.Net. To create controls at runtime we have to create object of each control and add them to form.
TextBox txt = new TextBox();//creating object of TextBox. txt.Location = new System.Drawing.Point(100, 30);//setting location txt.Width = 200;//setting width this.Controls.Add(txt);//adding text box to form
Label lbl = new Label();//creating label object lbl.Text = "ID";//setting text lbl.Location = new Point(20, 35);//setting location this.Controls.Add(lbl);//adding label to form. TextBox txt1 = new TextBox(); txt1.Location = new System.Drawing.Point(100, txt.Location.Y+txt.Height+10); txt1.Width = txt.Width; this.Controls.Add(txt1); Label lbl1 = new Label(); lbl1.Text = "Name"; lbl1.Location = new Point(20, txt1.Location.Y); this.Controls.Add(lbl1); Button btn = new Button(); btn.Location = new System.Drawing.Point(txt1.Location.X+txt1.Width-btn.Width, txt1.Location.Y+txt1.Size.Height+5); btn.Text = "Click"; this.Controls.Add(btn);
Thanks for sharing informative post.
Enter your email address here always to be updated. We promise not to spam!
© Copyright © 2010 - 2018 MindStick Software Pvt. Ltd. All Rights Reserved