how do i create a generic method for opening forms?

G

Guest

example:
// form classes:
// ItemEditor
// CategoryEditor

private void OpenForm(string formClassName)
{
// I want to be able to instantiate a form based on the parameter.
if (formClassName == "ItemEditor" )
ItemEditor editor = new ItemEditor();
else if (formClassName == "CategoryEditor")
CategoryEditor editor = new CategoryEditor();

// (formClassName) editor = new (formClassName); // something like this.
editor.Show();
}

I think this may work, but this has hard-coding issues. Is there a way to
evaluate the formClassName without having to use multiple IF conditions?
 
J

Jianwei Sun

Amil said:
example:
// form classes:
// ItemEditor
// CategoryEditor

private void OpenForm(string formClassName)
{
// I want to be able to instantiate a form based on the parameter.
if (formClassName == "ItemEditor" )
ItemEditor editor = new ItemEditor();
else if (formClassName == "CategoryEditor")
CategoryEditor editor = new CategoryEditor();

// (formClassName) editor = new (formClassName); // something like this.
editor.Show();
}

I think this may work, but this has hard-coding issues. Is there a way to
evaluate the formClassName without having to use multiple IF conditions?

First of all, I don't think it's a hardcoding, you have to depend on
some configuration to create proper forms, right?

Second, This is a typical factor pattern, you may want to create a
EditorFactory class, and based on configuration, create a proper editor.

Inheirt your ItemEditor, CategoryEditor from Editor class.

Editor poperEditor = EditorFactory.Create(string formClassName)

HTH
 
G

Guest

I'm already inheriting forms from baseforms. The suggestion you made is very
useful and I will implement it that way. I have the same situation for the
editor toolbar buttons as well. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top