I created some buttons in Code like this.
if (infoLoader.categoriesLoaded)
{
sideBarButtons = new Button[infoLoader.categoriesLength];
for (int i = 0; i < sideBarButtons.Length; i++)
{
sideBarButtons[i] = new Button();
sideBarButtons[i].Content = infoLoader.categories[i].name;
Canvas.SetLeft(sideBarButtons[i], 30);
Canvas.SetTop(sideBarButtons[i], 110 + (40 * i));
sideBarButtons[i].Click += new EventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideBarButtons[i]);
}
}
With the Button Event handling function like this:
private void SideButton_Click(object sender, EventArgs e)
{
// Uhh
Console.WriteLine("The Button has been clicked.");
mainText.Text = infoLoader.games[0].id.ToString() + ", " + infoLoader.games[0].name + ": " + infoLoader.games[0].description;
MainLaunchBtn.Content = "Launch " + infoLoader.games[0].name;
}
And it’s giving me the error:
Error 1 No overload for ‘SideButton_Click’ matches delegate ‘System.EventHandler’
I’m kind of confused as to what I’m missing here, and any help would be appreciated. Thanks,
Andy
The
Button.Clickevent in WPF (inherited fromButtonBase) isn’t of typeEventHandler– it’sRoutedEventHandler. I’m somewhat surprised about the exact error message you’re getting, but you should be able to change your code to:or more simply:
Delegate variance will allow your existing method signature to be converted into a
RoutedEventHandlereven though the second parameter is of typeEventArgsinstead ofRoutedEventArgs.You should check that you’ve got the right
usingdirectives though – make sure you’re genuinely creating WPF buttons instead of WinForms buttons.