I am working on windows form application in c#.
Firstly i created a table layout on run time and then inserted picture boxes after reading from database. Now i want to write sql query on every tablepanel cell/picturebox click.
Firstly I tried this by inserting picture box by drag and drop and i wrote query in picturebox_onclick function. But now i am creating picture box dynamically on run time.Now how i can handle onclick function of picture boxes that i created on run time.
private void GenerateTable(int columnCount, int rowCount)
{
//Clear out the existing controls, we are generating a new table layout
tableLayoutPanel1.Controls.Clear();
//Clear out the existing row and column styles
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
//Now we will generate the table, setting up the row and column counts first
tableLayoutPanel1.ColumnCount = columnCount;
tableLayoutPanel1.RowCount = rowCount;
for (int x = 0; x < columnCount; x++)
{
//First add a column
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
for (int y = 0; y < rowCount; y++)
{
//Next, add a row. Only do this when once, when creating the first column
if (x == 0)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
//Create the control, in this case we will add a button
picturebox cmd = new picturebox();
cmd.image = new bitmap("c:\\images\\abc.png");
tableLayoutPanel1.Controls.Add(cmd, x, y);
}
}
}
Please suggest me simple way as i am new in development. Thanks in advance.
This should do it for you.
PictureBox cmd = new PictureBox();
cmd.Click += cmd_Click;
Then have a new function to handle the click.
void cmd_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
To perform unique actions depending on what PictureBox was clicked you may want to give the PictureBox a name you can relate to such as a row number or unique ID of the row.
PictureBox cmd = new PictureBox();
cmd.Click += cmd_Click;
cmd.Name = y.ToString();
void cmd_Click(object sender, EventArgs e)
{
string RowID = ((PictureBox)sender).Name;
}