I made a class witch building some cards(Panel), and I want to add “Click” event while they are generated. I use this code below:
private void CoverBuilder(Panel mainPanel, Grid grid, int x, int y, int gameId, string
imageAddress, string exeAddress, string title)
{
card = new GameCard(grid.GameCardWidth, grid.GameCardHeight, grid.GameCardCoverWidth, grid.GameCardCoverHeight, Image.FromFile(imageAddress), title, gameId);
Panel gameCard = card.Make();
gameCard.Location = new Point(x, y);
gameCard.Cursor = Cursors.Hand;
gameCard.Click += GameCard_Click;
mainPanel.Controls.Add(gameCard);
}
private void GameCard_Click(object sender, EventArgs e)
{
Panel gameCard = (Panel)sender;
StartGame(int.Parse(gameCard.Name));
}
private void StartGame(int gameId)
{
DataTable table = gameDB.Find(gameId);
string exeFileAddress = table.Rows[0]["ExeAddress"].ToString();
// start exe process
var psi = new ProcessStartInfo(exeFileAddress)
{
UseShellExecute = true
};
Process.Start(psi);
}
But when I use them in a form and click on them, they do not work.