Reputation: 4633
I have been working on a quick WinForms program. I found this SO question about getting a ListBox to refresh and I thought Brad Bruce's answer was interesting, though I know binding is the correct way of solving the problem. So I went to try his solution of inheriting ListBox
and declared a similar class(names were modified to be read easier):
public class AustinListBox : ListBox
{
public void AustinRefreshItems()
{
RefreshItems();
}
}
Note, my code compiled and worked perfectly before I typed this code. After declaring this and without referencing this new class, I went to run the program... and BAM I was given a run time error saying that 2 toolStripButtons (which are placeholders by the way, I have changed nothing about them and they are not used anywhere) could not load their image. And yet the default images are located in Form1.resx just like they were before. It also would not let me view the design of Form1.
What does an unused class declaration have to do with two default toolStripbutton's images at run time? If I comment out the 7 lines of AustinListBox
code, everything magically works again.
Upvotes: 0
Views: 108
Reputation: 16747
It also would not let me view the design of Form1.
The form designer demands that the form being designed is the first class defined in the file. If you put a class definition at the top of a code file that contains a form class, the designer won't work.
Why the runtime error? The compiler generates a resource file containing the images for your StatusStrip buttons. It seems that placing a class before the form class prevented those resources from being linked into the assembly, so the resource loader throws an exception when it looks for those image resources at runtime.
Just put the class definition below the form definition, inside the form definition (if appropriate), or in a separate file. The latter is the generally accepted best practice for public classes.
In this case, since your class subclasses a Component, you should place it in a separate file. When you do, it will have a different icon in the Solution Explorer, denoting it as a Component. It will also get a .resx file of its own and you can use the designer to add other Components to it.
Upvotes: 2