I am trying to make a Generic FindControl method and I get the following error:
Cannot convert type 'System.Windows.Forms.Control' to 'T'
Code:
public T Control<T>(String id)
{
foreach (Control ctrl in MainForm.Controls.Find(id, true))
{
return (T)ctrl; // Form Controls have unique names, so no more iterations needed
}
throw new Exception("Control not found!");
}
try this
public T Control<T>(String id) where T : Control
{
foreach (Control ctrl in MainForm.Controls.Find(id, true))
{
return (T)ctrl; // Form Controls have unique names, so no more iterations needed
}
throw new Exception("Control not found!");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With