473,396 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how do i create a generic method for opening forms?

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?
Nov 17 '05 #1
2 1465
Amil wrote:
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
Nov 17 '05 #2
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.

"Jianwei Sun" wrote:
Amil wrote:
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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
3
by: rGenius | last post by:
hi all, im planning to create a lookup usercontrol where i need to specify a custom property (a form) to be loaded up once a button is pressed. usercontrol contains: textbox button now...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.