Hi there,
If I am not mistaken, what you are trying to achieve is to have multiple modes in the form like Insert, Edit, Update. There is an easier way of achieving this.
What you need to do is, drag and drop a Panel object on to your form, dock it in the form, design one state/mode. Repeat this process, for each and every state/mode you want. Then instead of, adding/removing your controls, you can show/hide them.
enum FormState {
Template,
Options,
ISBN,
Title
}
public void FormStateSwitch(FormState formState) {
switch (formState)
{
case FormState.Template:
templatePanel.Visible = true;
optionsPanel.Visible = false;
isbnPanel.Visible = false;
titlePanel.Visible = false;
break;
case FormState.Options:
break;
case FormState.ISBN:
break;
case FormState.Title:
break;
default:
break;
}
}
On a final note, please do not use int to depict your states, use an Enum instead, like I have used. There are many reasons why you should, I am going to mention only a few.
1. If you use numbers, you have to manually keep track what each mode. I.e. 1 = Template, etc. But if you use Enums, you see directly what "formState" is without remembering it. So you can use it in multiple locations without accidentally coding for a wrong state.
2. It is very easy to add/remove states. As soon as you remove a state, everywhere you've used that particular state will be highlighted as an error. As soon as you change the name of a state to something different, it will be updated all over your solution (if you are using a IDE like VS). If you add a state, all you have to do is go and add another case to your switch.
3. You can assign values to each item in an enum. For instance,
enum FormState {
Template = 1,
Options = 10,
ISBN = 15,
Title = 20
}
These are just example values but you can directly convert your enum to integer and use it in a calculation. The best part is, everything is strongly typed. You update your enum, everywhere else it would be updated.
Hope this helps :) Regards