0

Here i am trying to create object of WinForm which name is in database table and i get form name by var getChildForm = mnuService.GetChildNodeForm(menuName); service. Since there are other form i have to access as well, so i want to make it generic, but there is an error on line getChildForm usrCrtFrm = new getChildForm(); saying 'getChildForm' is a variable but is used like a type.

Below is my code

var getChildForm = mnuService.GetChildNodeForm(menuName);               
getChildForm usrCrtFrm = new getChildForm();
usrCrtFrm.Show();



Below is my service

    public string GetChildNodeForm(string menuTitle)
    {
        var getMenuId = uow.Repository<MainMenu>().FindBy(x => x.MenuTitle == menuTitle).FirstOrDefault().MenuId;
        return uow.Repository<MainMenu>().FindBy(x => x.MenuId == getMenuId).FirstOrDefault().Url;
    }



Any help will be appreciated .Thank you

sudip
  • 85
  • 2
  • 10

3 Answers3

1

If you have the form's Type, it's pretty easy to do what you ask:

var type = typeof(MyForm);
var form = Activator.CreateInstance(type);

But what if you don't have the type? Well, you cannot directly instantiate a form from its title, its "name" (whatever that means), its menu item, or its URL. You will need some mechanism to map one of these back to the form's type.

It's possible some sort of mapping is available; for example, if the have the URL of a page, you might be able to map it to a handler using the ASP.NET framework. But there is no such mechanism that maps titles or menus to WinForm types.

If your database returns the type name you may be able to find the type like this:

public Form InstantiateFormFromTypeName(string typeName)
{
    var type = System.Reflection.Assembly.GetExecutingAssembly()
       .GetTypes()
       .Where
       (
           t => t.FullName == typeName 
             && typeof(Form).IsAssignableFrom(t)
       )
       .Single();
    return Activator.CreateInstance(type) as Form;
}

...but you will need to be careful about scope and name. For example, there may be several types with the same name but in different namespaces or different assemblies.

If no automatic mapping is available, you can create your own, if you know the names of the forms ahead of time. If you store the mappings in a dictionary, for example, you can use it to create instances. Example:

//Define a dictionary where the key is the form name and the value
//is a function that will return a new instance of it.
var formList = new Dictionary<string, Func<Form>>
{
    { "Signon",    () => new LoginForm()    },
    { "Edit User", () => new EditUserForm() },
    { "Help",      () => new FaqForm()      }
};

//Use the dictionary to create a form
public Form InstantiateFormBasedOnName(string name)
{
    return formList[name]();  //Execute the function stored at index "name" in the dictionary
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
0
getChildForm usrCrtFrm = new getChildForm();

In the above line of code, you are trying to create object of getChildForm which is a var type and you are trying to use a local variable as a type. I found this How to use Local variable as a type? Compiler said “it is a variable but is used like a type” . Hope it would be useful.

SᴇM
  • 7,024
  • 3
  • 24
  • 41
-1

The first statement will return a string which looks like a path (url) of your form.

var getChildForm = mnuService.GetChildNodeForm(menuName);

So this statement return type would be a string which I guess is a form url. It may be something like this.

var childFormUrl = mnuService.GetChildNodeForm(menuName);

After this you should create a class(for example FormCreatorClass) that has a function (for example GetForm, which takes url as parameter)which returns a form object. Now you can create instance of this class and call the function of the class through the variable of the class instance

var formCreatorInstance = new FormCreatorClass();
var form = fromCreatorInstance.GetForm(childFormUrl);
form.Show();
  • Your answer is 'write some code that will do it' (this magic `FormCreatorClass`) which is much too broad to be helpful advice. – John Wu Jan 17 '18 at 05:10