How To Add Multiple Textbox And Button At A Time In Window Form Application

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace WindowsFormsApplication1 {  
  10.     public partial class Form1: Form {  
  11.         int ctextbox;  
  12.         int cbutton;  
  13.         int btntop = 1;  
  14.         public Form1() {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void button1_Click(object sender, EventArgs e) {  
  18.             try {  
  19.                 ctextbox = int.Parse(textBox1.Text);  
  20.                 cbutton = int.Parse(textBox2.Text);  
  21.                 if (textBox1.Text == "" && textBox2.Text == "") {  
  22.                     MessageBox.Show("Please enter your value first");  
  23.                 } else {  
  24.                     for (int i = 0; i <= int.Parse((textBox1.Text)) - 1; i = i + 1) {  
  25.                         TextBox txt = new TextBox();  
  26.                         this.Controls.Add(txt);  
  27.                         txt.Text = "TextBox" + (i + 1);  
  28.                         txt.Top = btntop * 25;  
  29.                         txt.Left = 20;  
  30.                         txt.BackColor = Color.Red;  
  31.                         btntop = btntop + 1;  
  32.                     }  
  33.                     for (int i = 0; i <= int.Parse((textBox2.Text)) - 1; i = i + 1) {  
  34.                         Button btn = new Button();  
  35.                         this.Controls.Add(btn);  
  36.                         btn.Text = "Button" + (i + 1);  
  37.                         btn.Top = btntop * 25;  
  38.                         btn.Left = 20;  
  39.                         btn.BackColor = Color.Purple;  
  40.                         btntop = btntop + 1;  
  41.                     }  
  42.                 }  
  43.             } catch {  
  44.                 MessageBox.Show("Please enter valid number");  
  45.             }  
  46.         }  
  47.     }