开发者

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 clas开发者_开发百科s 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


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜