kiran kumar

kiran kumar

  • NA
  • 1k
  • 270.6k

help me(dynamically adding controls)

Feb 2 2010 9:08 AM
 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 dynamicctrls
{
public partial class Form1 : Form
{

private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.White;
this.ForeColor = Color.Black;
this.Size = new System.Drawing.Size(900,900);
this.Text = "Run-time Controls";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Add";
this.btnAdd.Location = new System.Drawing.Point(90, 25);
this.btnAdd.Size = new System.Drawing.Size(50, 25);
this.txtBox.Text = "Text";
this.txtBox.Location = new System.Drawing.Point(10, 25);
this.txtBox.Size = new System.Drawing.Size(70, 20);

this.lstBox.Items.Add("One");
this.lstBox.Items.Add("Two");
this.lstBox.Items.Add("Three");
this.lstBox.Items.Add("Four");
this.lstBox.Sorted = true;
this.lstBox.Location = new System.Drawing.Point(10, 55);
this.lstBox.Size = new System.Drawing.Size(130, 95);
this.chkBox.Text = "Disable";
this.chkBox.Location = new System.Drawing.Point(15, 190);
this.chkBox.Size = new System.Drawing.Size(110, 30);
this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
this.lblCount.Location = new System.Drawing.Point(55, 160);
this.lblCount.Size = new System.Drawing.Size(65, 15);
this.Controls.Add(btnAdd);
this.Controls.Add(txtBox);
this.Controls.Add(lstBox);
this.Controls.Add(chkBox);
this.Controls.Add(lblCount);
btnAdd.Click += new EventHandler(OnClickMeClicked);
}

public void OnClickMeClicked(object sender, EventArgs ea)
{
for (int i = 0; i < 2; i++)
{
TextBox chkBox = new TextBox();
this.chkBox.Text = "Disable";
this.chkBox.Location = new System.Drawing.Point(152+i+10, 192+i+20);
this.chkBox.Size = new System.Drawing.Size(110, 30);
this.Controls.Add(chkBox);
//MessageBox.Show("hello kiran how are you");
}

}
}
}

when i click the button only one textbox gets added...help me 

Answers (3)