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 {
int ctextbox;
int cbutton;
int btntop = 1;
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
try {
ctextbox = int.Parse(textBox1.Text);
cbutton = int.Parse(textBox2.Text);
if (textBox1.Text == "" && textBox2.Text == "") {
MessageBox.Show("Please enter your value first");
} else {
for (int i = 0; i <= int.Parse((textBox1.Text)) - 1; i = i + 1) {
TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Text = "TextBox" + (i + 1);
txt.Top = btntop * 25;
txt.Left = 20;
txt.BackColor = Color.Red;
btntop = btntop + 1;
}
for (int i = 0; i <= int.Parse((textBox2.Text)) - 1; i = i + 1) {
Button btn = new Button();
this.Controls.Add(btn);
btn.Text = "Button" + (i + 1);
btn.Top = btntop * 25;
btn.Left = 20;
btn.BackColor = Color.Purple;
btntop = btntop + 1;
}
}
} catch {
MessageBox.Show("Please enter valid number");
}
}
}
}In this part we will learn how to add multiple button and textbox at a time in window form application. I used 2 textbox to receive the number of require textbox and button controls from the user.
