Hello!! Well, You could look at userControls, do one where you put everything that the controls have in common, then inherit it to two seperate controls, where you put all the specifics. Hi. Hello!! I would have put th common code in one form and create two new forms that inherits the common form. Well, From what I can understand you display the form at all times, it's just the contents that's suppose to differ. If that is the case:How do I use one form showing two different look
I'm using C#
Assume I have an application containing a form named form.cs.
When you create the look for the form you use the form designer
to construct the look that you want.
All changes that you do in form designer are put into method InitializeComponent.
Assume the form have many more controls than these three controls mentioned just below.
The controls that are interesting for my question are:
Two buttons called put_in_box and get_from_box.
One label showing the correct time.
One datagrid.
The property for these controls is located in the InitializeComponent as I mentioned earlier.
Assume another customer requirement on the form is quite similar to form form.cs.
The only difference is that some controls should not be displayed and some other controls be added instead.
The followings controls should not be displyed for the new customer.
The two buttons named put_in_box and get_from_box.
A label showing current time.
A datagrid.
The new customer want to have a combobox in the same location as the datagrid.
We can assume that if you compare the appearance of the form between the two customer much is the same.
Now to my problem.
1. I want to have only one form in this case form.cs. So no duplication of code.
2. In an easy way being able to have two appearance(look) of the same form which is named form.cs.
3. Being able to change the appearance(look) for any of these two customer in an easy way..
4.The form designor is always looking for InitializeComponent when you want to change the appearance(the look) for the form.
5. If I had used MFC I would have used the resource files which store the look for a form in a named resource file. One customers look had been stored in one resource file and the other customer look would have been stored in another resource file but the code would only be in one place so no duplicate code
I can't find any good solution to my problem.
- One solution which is bad is to have two forms having one form for each customer which also duplicate the code. For example having form.cs and form_xyz.cs
- Another solution which is very bad is to have two InitializeComponent
InitializeComponent_abc and InitializeComponent_xyz. One for each customer.
When you want to edit the appearance(the look) for a customer xyz you have to rename InitializeComponent_xyz to InitializeComponent because the form designed is always looking for InitializeComponent. As a summary a lot of renaming here and there.
I wonder if you could help me with a good solution if there exist one.
Many thnaks!
//TonySv: How do I use one form showing two different look
Then you just have to choose which userControl to display in your form. And when to load/unload the other.
/EmmaSv: How do I use one form showing two different look
Could this be done by adding a method to your form called setApperance(..)? The method setApperance(..) will set the controls you don't want to show as invisible and vice verse with the controls you want to show. It's not the fanciest solution though =).
good luck anyway!Sv:How do I use one form showing two different look
Thanks for you answer.
The most normal is to add the userControl to the form manually.
Because you mentioned about load and unload is it possible to load the userControl dynamicall which mean the program load some specific userControl based on some value from the database.
Do you know which class and method takes care of this loading and unloading
//TonySv: How do I use one form showing two different look
Adding the diffrent code to this two new forms.
This could be more useful for an larger mor complelex for where only litele of the form differ.
Dont know how this tip works for you. That is upp to you.Sv: How do I use one form showing two different look
- How do you get your information from the database? By an event of some sort?
- In the method where you get the database-information put in a select case/if-else or how ever you like to check the value, and choose to load the usercontrol.
- The easiest way is to load them both at startup and just alter their visability depending on the database status.
But you really ought to have three userControls as I mentioned in my first post, then you won't have the problem of having the same code in two places. Put all that's common in one, inherit it to the two usercontrols that you wish to display and build them.
Hope that's helpfull.
/Emma