Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am converting a main frame application to c# that has 133 screens(forms) and each form has a text box that the user will enter the next form that they want to go to.
How can I do this with out creating 133 case statements.
Posted
Comments
ZurdoDev 16-Apr-14 9:28am    
I suppose there may be many ways but we likely need more info.
Emre Ataseven 16-Apr-14 11:08am    
Are forms same type? Do they have unique id or name?

This is my article on how to eliminate those long switch statements: Dynamic Method Dispatcher, No more long switch statements!

In your case, it could be even simpler: you could have a dictionary keyed by something, with the value representing the form reference (window reference, page reference, page URL, whatever). If this is not clear, I would need to know more detail (what is entered).

However, you should better remove this apparently bad design with entering something in a text box and create, say, humane menu. And don't create a separate handler of all menu items, use one dispatching a click on a particular item to a corresponding form. You can, for a simple implementation, store form references in item's Tag.

For your information, since the times when mainframe computers dominated, Inquisition has virtually ran out of business; sinned people are no longer sentenced for entering mystical codes in text boxes, they are still allowed to use menus, toolbars and other similar tools of humanity.

[EDIT]

Please see also my comment to Solution 2. You can wrap the call to Form.Show in the code adding a form instance to the dictionary. First, this code checks up if the reference is already in a dictionary (using Dictionary.HasValue), and, if not, adding it. The calculation of the key can be implemented in the form class, say, your base form class.

—SA
 
Share this answer
 
v6
Comments
CHill60 16-Apr-14 13:26pm    
+5. I've used this method despatcher for something similar. Recommended.
Sergey Alexandrovich Kryukov 16-Apr-14 13:58pm    
Thank you.
—SA
Emre Ataseven 16-Apr-14 14:11pm    
The link you gave seems wrong addressed :)
Sergey Alexandrovich Kryukov 16-Apr-14 14:21pm    
Thank you very much; I'll fix it. fixed.
—SA
I'm not sure I understand your problem clearly. But I made a try with my understandings.


C#
public partial class FormHome : Form
{

    public FormHome()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //textBox1.Text is Form's name
        var frm = FormManager.CreateForm(textBox1.Text);
        if (frm != null)
        {
            frm.Show();
            frm.BringToFront();
        }
    }
}



C#
static class FormManager
{
    private static readonly Hashtable HFormTypeMap = new Hashtable();
    private static readonly Hashtable HFormConcreteMap = new Hashtable();

    static FormManager()
    {
        Initialize();
    }

    private static void Initialize()
    {
        var asm = Assembly.GetExecutingAssembly();

        var allTypes = asm.GetTypes();
        foreach (var type in allTypes)
        {
            if (type.BaseType == typeof(Form))
            {
                HFormTypeMap[type.Name] = type;
                HFormConcreteMap[type] = null;
            }
        }
    }

    //Creates 1 instance for each form type and hold it in hashtable
    public static Form CreateForm(string formName)
    {
        var type = (Type)HFormTypeMap[formName];
        if (type == null)
            return null;

        var value = HFormConcreteMap[type];
        if (value != null && value is Form && !((Form)value).IsDisposed)
            return (Form)value;

        var form = (Form)Activator.CreateInstance(type);
        HFormConcreteMap[type] = form;
        return form;
    }
}
 
Share this answer
 
There's lots of ways to do this, but here's what I'd do.

But first, why would you want to replicate some ancient user interface in a modern GUI??

Anyway, custom attributes. Create a new attribute with a single string 'name' (a custom attribute is a class the derives from Attribute and provides metadata about things in .NET) and apply it to each form class.

Then create a discovery mechanism which iterates through all the forms in all the assemblies in your application and creates a dictionary of the name from the attribute to the type of form.

Then you can create a simple method that will instantiate the form based on its name. Its extensible and no switch statements are required.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Apr-14 11:35am    
Basically, I agree, but searching through the assemblies means using reflection, which would give you all form types (or any types in general), but not instances of the forms. It is actually simpler than that: you need to wrap all creations (or, better, events of showing a window) in the code adding a form instance (reference) to the dictionary. I actually meant that in my answer, please see (in a next step, I add the [EDIT] to mention this detail). Voted 4.
—SA
Rob Philpott 16-Apr-14 11:41am    
Here we go again.

Not ALL forms, just the ones with appropriate custom attributes. And you absolutely want the type not an instance, or you are going to have to create 133 of them all in advance.

Please stop adding these useless comments and stick to your own solutions.
Sergey Alexandrovich Kryukov 16-Apr-14 11:55am    
The answer will be the same, but not all forms. What's the difference? How about having two instances of the same form type? Such things happen...
—SA

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