Multiple classes to a single declaration in form

Posted on 2006-05-03
Last Modified: 2010-04-16
Hello experts:
I'm doing an applicaton that has a main form, configuration form, configuration class and two (for lack of better terms) main classes. The main form starts. Before it is displayed, the configuration form displays as a dialog form. It is the interface to the configuration class. When the configuration form is terminated with 'OK', the main form displays. Based on the configuration information, the main form must have a reference to either main1 or main2 class. The main form will reference either of these two classes as main(not referring to main method() ). How do I do this?
Question by:freename
    1 Comment
    LVL 4

    Accepted Solution

    OK, the main idea here is
    - Create MainForm first, but user cannot interact with it.
    - Create ConfigDialogForm later with button OK.
    - When user click button OK on ConfigDialogForm, we will return a string with value "MainAClass" or "MainBClass" to MainForm object.
    ----------The question is how we can return the string to MainForm object?
    ----------That is we will pass the MainForm object into Constructor of ConfigFormDialog (see how to do below)
    - After that, make a condition statement to decide which class, MainAClass or MainBClass should be instantiated.
    ----------We have the same object is MainClass, but how can we have 2 different ways of functioning. We use polymophism in .NET.
    ----------Create Abstract class named MainClass, and ... see how below.
    So, you have 6 class.
    - class MainForm
    - class ConfigDialogForm
    - class ConfigClass
    - class MainClass (abstract class)
    - class MainAClass
    - class MainBClass.
    If so, let move on.
    //MainForm Constructor
    public class MainForm
          public string strChooseWhichMainClass;
          public MainClass a;
          public MainForm()

                  //Make a call to ConfigDialogForm right down here.
                  ConfigDialogForm confForm = new ConfigDialogForm(this); //We need a reference to MainForm
                  confForm.Visisble = true; //It means you also open ConfigDialogForm

                  //You may want to disable all function of MainForm here.
                  //If users click Cancel on ConfigDialogForm, it means they cannot use the application.

            public void btnProcessMainFormFunction_Click(object sender, System.EventArgs e)
                  if(strChooseWhichMainClass != "") //To make sure user click button OK now, if not, nothing will happen when click this button.
                             a = new MainAClass();
                             a = new MainBClass();

    So, in the class ConfigDialogForm, you will create object ConfigClass which has some functions to Process request from user.
    public class ConfigDialogForm
           public MainForm MForm1;

           //user defined constructor
           public ConfigDialogForm(MainForm form1)
                   //After this line of code, you now have an object of MainForm in ConfigDialogForm,
                    MForm1 = form1;

           public void btnOK_Click(object sender, System.EventArgs e)
                   //After this line of code, variable named strChooseWhichMainClass will have result as "MainAClass" or MainBClass
                   MForm1.strChooseWhichMainClass = RequestWhichMainClass(<params>);


           public string RequestWhichMainClass(<params>)
                 ConfigClass confgObj = new ConfigClass();
                 return confgObj.ChooseWhichMainClass(<params>);

    public abstract MainClass

    public class MainAClass : MainClass
           //Overloading function1,2,3 above in MainAClass way.

    public class MainBClass : MainClass
           //Overloading function1,2,3 above in MainBClass way.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now