Introductionhow to add buttons at run time by clicking on a button in .net. Implementationusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace addCompRunTime
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int Y = 13;
private int X = 13;
private Button b;
private int i = 0;
private void button1_Click(object sender, EventArgs e)
{
b = new Button();
b.Text = "btn" + i.ToString();
b.Name = b.Text;
b.Width = 70;
b.Height = 33;
b.Top = Y;
b.Left = X;
Y += 47;
if (b.Top + 46 > this.Height)
{
X += 104;
Y = 13;
}
else
{
this.Controls.Add(b);
b.Show();
}
i++;
//if(i%2==0)
b.Click += new System.EventHandler(this.b_Click);
Thread.Sleep(164);
}
private void b_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
MessageBox.Show(btn.Text /* btn.Top.ToString() */ );
}
}
}
Sample Project SourceDownload source files -33 kb |