Hi Friend,
I am back, and here is the full code that may help you for sure.
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 Test_Dynamic_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TextBox mybox = new TextBox();
Button mybutton = new Button();
List<control> arraylist = new List<control>();
private void initlist()
{
mybox.Text = "Hello";
mybutton.Text = "Click";
mybox.Left = 10;
mybutton.Left = 10;
mybox.Top = 10;
mybutton.Top = 40;
mybutton.Click +=new EventHandler(mybutton_Click);
arraylist.Add(mybox);
arraylist.Add(mybutton);
}
private void Form1_Load(object sender, EventArgs e)
{
initlist();
foreach (Control myobj in arraylist)
{
this.Controls.Add(myobj);
}
}
private void mybutton_Click(object sender, EventArgs e)
{
mybox.Text = "I am Changed";
}
}
}
This program is working fine. It uses a list to generate the controls dynamically on the screen and sets a callback inside this list. So, whenever this callback is called it performs the task inside the function.
As per this code. It draws two controls on the screen, the first being a Textbox and the next is a button, there is a Event callback setup for button click event. This dynamically sets up a callback event and links it to the function mybutton_Click(). Thus the text inside the textbox changes :-)
Hope this code helped you.
Regards
Tushar Srivastava :-)