I am making a windows forms app in C# (Visual Studio 2015) and lots of forms are supposed to be similar or the same when it comes to the controls they have. Instead of making all of those similar forms separately (because I would end up with a lot of forms, which would ultimately slow down the app), I made one base form and inherited others from it. However, I still end up with a lot of forms, they are just inherited ones (with less code I guess).
I know about user control, but I don't think that is what i want. That makes the job easier, in a sense that instead of making all these forms from the bottom, I can just add this and it's going to be okay. The problem is I still have to add the user control to something - to a form? I'd like to be able to do all of this inside one class.
EXAMPLE: Here is an example of what I want to achieve, maybe it will clear things up: Lets say I want to make an interactive map of Europe - I want people to be able to click on a country and get information about it (in form of a new window - a winform). Now every country has the exact same looking window (form) - they all have a textbox, two buttons, a picturebox, and a label, the only thing that's different is the information inside those controls. Now instead of making 50 window forms that look exactly the same (but have different information), is there a more practical way to do this? I know that I could make one UserControl and add it to forms, but I still have to make all of those 50 forms and add the UserControl. I'm afraid so many forms in a simple app would slow it down, and it doesn't seem like an efficient way to do this. I thought of making a base form, that has all of these things and then with a class allow editing the information - when people click on a certain button (country), the base form is called but with the information from another class that is connected to that particular button, e.g. if a person clicks on Italy, they will get the base form but it will have information about Italy (the textbox will have italian history, there's pictures of Italy...) but if a person click on Germany, they will also get the base form but this time it has information about Germany.
Why do you need a new form class for each country? The idea of a class is that it holds common behaviours. A form is a class. just instantiate an instance of the class with the data that it needs to display. Create a data class to encapsulate the data that the form shows (like map image, description, history etc) and pass that class to the form before showing it. eg:
public class Country
{
public string Name {get;set;}
public string Description {get;set;}
public string MapUrl {get;set;}
public string History {get;set;}
//any other properties you need to set. Get these from a database or text file if you want
}
in the form add a method like the following:
public void SetCountry(Country country)
{
//assumung you have controls on your form for displaying the data:
Text = country.Name;
txtDescription.Text = country.Description;
txtHistory.Text = country.History;
//etc.
}