ElPicklers Assosciate
Card Game
public void AssignCardsToButtons()
{
for (int i = 2; i < Math.Min(deck.Count + 2, 9); i++)
{
// Assuming the CardX methods create dynamic buttons
GetType().GetMethod($"Card{i}").Invoke(this, null);
int cardIndex = i - 2;
if (cardIndex >= 0 && cardIndex < deck.Count)
{
string newdir = @"(where the cards are stored)";
newdir = Path.Combine(newdir, $"{deck[cardIndex].Value.ToLower()}of{deck[cardIndex].Suit.ToLower()}.png");
var dynamicButton = Controls.OfType<Button>().Last(); // Assuming the dynamic button is the last added button
dynamicButton.Click += (sender, e) => MessageBox.Show($"Clicked: {deck[cardIndex]}");
dynamicButton.Image?.Dispose();
dynamicButton.Image = Image.FromStream(new MemoryStream(File.ReadAllBytes(newdir)), true, false);
}
else
{
MessageBox.Show($"Not enough cards in the deck for button {i}");
}
}
}
7 replies