pauloh
pauloh

Reputation: 1002

Creating new Instance of the window by string Type

Camarades,

I have a WindowForm application and that contains multiple forms, each with a specific name. Well, I wanted to develop a class that manages the creation of these windows, where, through the parameter type of screen (her name), the system create one for me...

I'm thinking in the property "AcessibleName" in the MenuItem, put the name of the class that I want. Then to click on each item, the system performs the following verification

private void mnMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        try
        {
            if (!String.IsNullOrEmpty(((MenuStrip)sender).AccessibleName))
            {
                string[] _Parametros = ((MenuStrip)sender).AccessibleName.Split(uConstantes.CtSeparadorMenu);
                uMenu.CreateWindow(((MenuStrip)sender).AccessibleName, _Parametros);
            }
        }
        catch (uException __Excp)
        {
            throw __Excp;
        }
    }

uMenu.CreateWindow and within the class, I would receive the parameters and instantiate a new object, and display it. Does anyone have any idea? Reflection solves this problem? (Unfortunately I do not know much about reflection)

Thanks

Upvotes: 0

Views: 115

Answers (2)

pauloh
pauloh

Reputation: 1002

Actually, I was doing something wrong. I need to put the name of the Window, including its full path (namespace). Then, the command will work, and the type will be identified.

Thank you all.

Upvotes: 0

Jacob G
Jacob G

Reputation: 3665

I would look at the Activator.CreateInstance method to do specifically what you're asking for.

However, I'm inclined to ask if it's even necessary (based on the information you've provided.) Let's say that you have a menu that contains 3 items. When the user clicks on MenuItem1, they're presented with Form1. Likewise, MenuItem2->Form2 and MenuItem3->Form3. Why wouldn't you just launch the forms directly? What benefit is this providing you?

Upvotes: 2

Related Questions