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 Buttons { public partial class Form1 : Form { const int ROWS = 5; const int COLS = 10; public Form1() { InitializeComponent(); this.Load += new System.EventHandler(this.Form1_Load); } public void Form1_Load(object sender, EventArgs e) { new MyButton(ROWS, COLS, this); } } public class MyButton : Button { const int WIDTH = 50; const int HEIGHT = 50; const int SPACE = 5; const int BORDER = 20; public static List<List<MyButton>> buttons { get; set; } public static List<MyButton> buttonList { get; set; } public Form1 form1; public int row { get; set; } public int col { get; set; } public MyButton() { } public MyButton(int rows, int cols, Form1 form1) { buttons = new List<List<MyButton>>(); buttonList = new List<MyButton>(); this.form1 = form1; for(int row = 0; row < rows; row++) { List<MyButton> newRow = new List<MyButton>(); buttons.Add(newRow); for (int col = 0; col < cols; col++) { MyButton newButton = new MyButton(); newButton.Height = HEIGHT; newButton.Width = WIDTH; newButton.Top = row * (HEIGHT + SPACE) + BORDER; newButton.Left = col * (WIDTH + SPACE) + BORDER; newButton.row = row; newButton.col = col; newRow.Add(newButton); buttonList.Add(newButton); newButton.Click += new System.EventHandler(Button_Click); form1.Controls.Add(newButton); } } } public void Button_Click(object sender, EventArgs e) { MyButton button = sender as MyButton; MessageBox.Show(string.Format("Pressed Button Row {0} Column {1}", button.row, button.col)); } } }
Make dynamic
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 WindowsFormsApplication72 { public partial class Form1 : Form { const int ROWS = 5; const int COLS = 10; public Form1() { InitializeComponent(); new PictureText(10, this); } } public class PictureText : Panel { const int WIDTH = 50; const int HEIGHT = 50; const int SPACE = 5; const int BORDER = 20; public TextBox textbox { get; set; } public PictureBox picturebox { get; set; } public int row = 0; public static List<PictureText> pictureText = null; public PictureText() { } public PictureText(int rows, Form1 form1) { this.Left = BORDER; this.Top = BORDER ; this.Width = 2 * (Width + BORDER); this.Height = rows * (HEIGHT + BORDER); this.BackColor = Color.Blue; form1.Controls.Add(this); pictureText = new List<PictureText>(); for(int row = 0; row < rows; row++) { PictureText newRow = new PictureText(); this.Controls.Add(newRow); pictureText.Add(newRow); newRow.textbox = new TextBox(); newRow.textbox.Text = "Hello"; newRow.picturebox = new PictureBox(); newRow.picturebox.BackColor = Color.Green; newRow.picturebox.Text = "Goodbye"; this.Controls.Add(newRow.textbox); this.Controls.Add(newRow.picturebox); newRow.textbox.BringToFront(); newRow.textbox.Height = HEIGHT; newRow.textbox.Width = WIDTH; newRow.textbox.Top = row * (HEIGHT + SPACE) + BORDER; newRow.textbox.Left = BORDER; newRow.picturebox.BringToFront(); newRow.picturebox.Height = HEIGHT; newRow.picturebox.Width = WIDTH; newRow.picturebox.Top = row * (HEIGHT + SPACE) + BORDER; newRow.picturebox.Left = 2 * (BORDER + Width); } } } }