Click here to Skip to main content
15,895,779 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I created a method with a switch case in it.
C#
public void FormStateSwitch(FormState formState)
{
    var defaulSize = new Size(278, 132);
    var backDefLoc = new Point(10, 77);
    switch (formState)
    {
        case FormState.Template: //Template
            optionsPanel.Visible = false;
            isbnPanel.Visible = false;
            titlePanel.Visible = false;
            templatePanel.Visible = true;
            Size = defaulSize;
            break;
        case FormState.Options: //Options
            titlePanel.Controls.Remove(_back);
            isbnPanel.Controls.Remove(_back);
            optionsPanel.Controls.Add(_back);
            isbnPanel.Visible = false;
            titlePanel.Visible = false;
            templatePanel.Visible = false;
            optionsPanel.Visible = true;
            Size = defaulSize;
            _back.Location = backDefLoc;
            _previousState = FormState.Template;
            break;
        case FormState.ISBN: //ISBN
            optionsPanel.Controls.Remove(_back);
            titlePanel.Controls.Remove(_back);
            isbnPanel.Controls.Add(_back);
            optionsPanel.Visible = false;
            titlePanel.Visible = false;
            templatePanel.Visible = false;
            isbnPanel.Visible = true;
            Size = defaulSize;
            _back.Location = backDefLoc;
            _previousState = FormState.Options;
            break;
        case FormState.Title: //Title
            isbnPanel.Controls.Remove(_back);
            optionsPanel.Controls.Remove(_back);
            titlePanel.Controls.Add(_back);
            optionsPanel.Visible = false;
            isbnPanel.Visible = false;
            templatePanel.Visible = false;
            titlePanel.Visible = true;
            _back.Location = new Point(10, 137);
            Size = new Size(278, 195);
            _previousState = FormState.Options;
            break;
    }
}


The method works fine when called within the form it is contained in, but when I call it from an outside class it has no effect. The answer is probably simple and I am overlooking something so I would appreciate the help. Even when I try to edit the form without using this method and I am working in another class it also doesn't have an effect.
I would also appreciate an explanation as to why so that I am learning something instead of just being told something.
Posted
Updated 5-May-11 17:48pm
v2
Comments
Hemant__Sharma 5-May-11 0:30am    
Did you get any exception when debugging or your code came succefully to the end of this function?
Can you add code
1- where you are invoking this function inside the same form
2- where you are invoking this function from outside class
the code should include any event name like button click etc in which you are invoking this function.

Thanks
Hemant
Cardon Fry 5-May-11 0:47am    
No exceptions were made in it ran through the code completely, the form just never updated. I will edit this post with direct examples when i get back home.

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.
C#
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:
            // Do your work here
            break;
        case FormState.ISBN:
            // Do your work here
            break;
        case FormState.Title:
            // Do your work here
            break;
        default:
            // Do your work here
            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
 
Share this answer
 
Comments
Cardon Fry 5-May-11 3:16am    
Thanks for the tips, I implemented them and it works nicely, except I still hit the same problem.
When I the method for when my Back button is pressed the Form updates properly and it is in the same class file as the FormStateSwitch method.

private void Back_Click(object sender, EventArgs e)
{
FormStateSwitch(_previousState);
}

This simply takes the form back one step in the process.

But when I try to use FormStateSwitch in another class file it doesn't update properly but no exceptions are thrown.

private void GoToOptionsButton_Click(object sender, System.EventArgs e)
{
var mainForm = new MainForm();
mainForm.FormStateSwitch(MainForm.FormState.Options);
}
CodeHawkz 5-May-11 4:22am    
I don't know whether you skipped some code, but I can't see you showing the form. I think I found the glitch in your code.

You need to decide something :) Are you going to set the mode of the form when the form is shown? or is it that the form is always visible and a button in another form is clicked and it changes?

I did a very small example of what you need to do. Download it and see, I think the link expires in a day or something. So hurry.
http://www.filefactory.com/file/cb192b8/n/Cardon.FormStates.Test.zip

Hope this helps you :) Regards
Hemant__Sharma 5-May-11 9:37am    
Exactly.. my 5
CodeHawkz 5-May-11 11:34am    
Thank you :)
Cardon Fry 5-May-11 23:56pm    
Thank you so very much you have been quite the help, and have explained everything quite well. vote 5.
- if the outside class is running some other thread than the form thrad then it will raise cross thread operation exception.
 
Share this answer
 
Bad switch case! Never do such things!

At least, create some enumeration and use it in case and elsewhere:

enum MyCase { Template, Options, Isbn, Title, }


In general, avoid all those immediate constants: 0, 1, "", "my string", etc. Always use explicit constant declarations, data files and/or resources.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900