- This topic explains about how to dynamically create controls or dynamically add controls in C# programming.
- Using this, you can dynamically create any controls like TextBox, Button, and Panel etc.
- Here we explain, to dynamically create TextBox and Button controls.
TextBox Class – Dynamically create or dynamically add controls in C#
- It represents a control that can be used to display or edit unformatted text.
- Syntax for Search character from textbox in C#
|
1 2 3 4 5 |
// Declaration [LocalizabilityAttribute(LocalizationCategory.Text)] [ContentPropertyAttribute("Text")] public class TextBox : TextBoxBase, IAddChild { } |
Button Class – Dynamically create or dynamically add controls in C#
- Button Class represents a Windows button control, which reacts to the ButtonBase.Click event.
- Syntax for Button Class
|
1 2 3 |
// Declaration public class Button : ButtonBase { } |
Dynamically create or dynamically add controls in C# – Dynamically create TextBox
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Create an object for the textbox { TextBox txtinput = new TextBox(); //Assigning location for the textbox txtinput.Top = 10; txtinput.Left = 10; txtinput.Width = 100; txtinput.Height = 50; txtinput.TextAlign = HorizontalAlignment.Left; //Adding textbox control to the frame.. this.Controls.Add(txtinput); } } } } |
Dynamically create or dynamically add controls in C# – Dynamically create Button
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Create an object for the button { Button btnadd = new Button(); //Assigning location for the button btnadd.Top = 10; btnadd.Left = 10; btnadd.Width = 100; btnadd.Height = 50; btnadd.Text = "Click"; //Adding button control to the frame.. this.Controls.Add(btnadd); } } } } |
If you have any suggestions regarding this topic, please contact us…
Dynamically create or dynamically add controls in C#






