views:

38

answers:

2

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

+1  A: 

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?

Jacob G
Camarade,This rule will not apply to everyone, and according to my rules of programming, usually put specific names and tend to vary in the cases. Answered your question?I will try to implement this solution.Thanks
Ph.E
@Ph.E: I guess my question was more along the lines of, is it always the case that clicking on MenuItem1 will need to display Form1? If so, why not just say `new Form1()` in the click handler for MenuItem1?
Jacob G
@Jacob G: Why is there a number of settings, which will be passed in the form of array. If I use the OnClick event will be very polluting the code, leaving it very segmented.
Ph.E
@Jacob G: Camarades, I have difficulty getting the type from the string value (variable). Do you have any idea? === GetType() did not work.
Ph.E
@Ph.E: Activator.CreateInstance has a number of overloads. One of them takes 2 strings. One string represents the assembly name and the other represents the class name. See here: http://msdn.microsoft.com/en-us/library/d133hta4.aspx
Jacob G
@Jacob G: Yes, Camarade! But, that there is the problem. The Type of him and his assembly are always identified as a string, not as a formName. Just need to solve the identification
Ph.E
A: 

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.

Ph.E