How do I access the application Form Name?

Please excuse this question.  I'm currently learning C# "on my own".  My application is an MDI with several child forms and I want to enable/disable the buttons on the active form by calling a method that will accomplish this for each form that calls it. 

I created a test app and thought I had the solution; however, I am at a stopping point and can not figure out how to solve this.  Can someone please show me what I am not doing  or doing wrong  

I've included the code from the Class and from one of the calling methods below (the code is C# 2005):

Code in ClassMain.cs:

    public void ResetButtons( Form myForm, bool Open, bool convert, bool close)
    {
        myForm.buttonOpenFile.Enabled = open;
        myForm.buttonConvert.Enabled = convert;
        myForm.buttonClose.Enabled = close;
    }

Code in calling method:

    private void buttonConvert_Click( object sender, EventArgs e )
    {
        ClassMain clas = new ClassMain();

        this.labelCurrentStatus.Text = "Processing . . .";

        clas.ResetButtons ( this, false, false, false );
    }

When I start the app, I receive these three errors but no matter what I've tried, I have not been able to solve the problem.  How can I get the Class method to retrieve the Form that is calling it so I can access the controls on it

Error 2 'System.Windows.Forms.Form' does not contain a definition for 'buttonOpenFile' C:\Projects\VS_2005\ucpFileConversion\ucpFileConversion\ClassMain.cs 21 17 ucpFileConversion

Error 3 'System.Windows.Forms.Form' does not contain a definition for 'buttonConvert' C:\Projects\VS_2005\ucpFileConversion\ucpFileConversion\ClassMain.cs 22 17 ucpFileConversion

Error 4 'System.Windows.Forms.Form' does not contain a definition for 'buttonClose' C:\Projects\VS_2005\ucpFileConversion\ucpFileConversion\ClassMain.cs 23 17 ucpFileConversion




Answer this question

How do I access the application Form Name?

  • Digitalmind

    whoa. . . back up. . .

    make a form called  ConversionFormBase

    put your three buttonOpenFile, buttonConvert, buttonClose, and labelCurrentStatus on it, change their modifiers to protected.

    add this method:


        protected virtual void ResetButtons(bool Open, bool convert, bool close)
        {
            this.buttonOpenFile.Enabled = open;
            this.buttonConvert.Enabled = convert;
            this.buttonClose.Enabled = close;
        }

    add this method:


        protected virtual void DoProcess()
        {
            throw new NotImplementedException("YOU MUST OVERRIDE THIS IN AN INHERITED FORM!!!");
        }

    double click buttonConvert and add this method:


        private void buttonConvert_Click(object sender, EventArgs e)
        {
            this.labelCurrentStatus.Text = "Processing . . .";
            this.ResetButtons (false, false, false );
            this.DoProcess();
            this.ResetButtons(true, true, true);
        }

    build your project. . . now for each of your particular conversion forms, Add an 'Inherited form', select ConversionFormBase and in the code, override DoProcess to perform the particular conversion process

    Done!

     



  • Room222

    Google: "Visual Inheritance" +.Net < click that

    I have MacDonald's User Interfaces in C# and Sells' Windows Forms Programming, but they only have about 4 or 5 pages each. But that kind of makes sense. . . its not so much about forms as it is about OOP.

    In that regard I would direct you towards the big 3 stalwarts - Grady Booch , The Gang of Four and the Fowler Refactoring Bible. No programmer should be without these on their bedside table.

    cheers!



  • cds_ks

    Forgive me, but what you sent seems to me to operate on one form.  Which to me means that if I have multiple forms (as in an MDI process), then I'll duplicate the code on each form   I thought that I would create a Class that would have the method that would do that and each child form would call the method when required, thus not duplicating code unnecessarily.  Am I overdoing it   Should I just put the same code on each form and let it go with that

     

    This was sent before I read the previous message.



  • Adam Calderon

    Rhubarb wrote:
    This was sent before I read the previous message.

    So, I assume you see the approach

    you have a set of common functionality and entry points that are shared across various forms.

    Define the common functionality and calls to the entry points in the Base.

    Mark the entry points in base as "virtual".

    Inherit from the Base and override the entry points for the Child form class to implement the particular behavior for that type of Child.

    That way the particular forms behave in a specific manner.

    The example code I linked is very simple.

    In a real world example, you might put a OpenFileDialog component on the base form - modifier set to protected.

    In the base you might attach code like this:

  • Inna73863

    Sorry, I had a Dr. appointment to go to.

    Yes, it does and I am probably going to throw all of the books out that I've read!

    Am I reading the wrong books Where can I learn how to code like this From what you showed me here and in another thread, it looks like advanced techniques to me.



  • Koiti

    Just bringing my delphi experience along for the ride.

    Always keep in mind that Forms are just classes. . .completely inheritable and overridable (Unlike that dog VB SUX. . . erm. . . I mean 6)



  • TheAgent

    Well, thank you for your help. I really appreciate it alot.

    Greg



  • Teleo

    absolutely not. . . look at my example link

  • Mike Anderson

    check this out for an example. . . .

    build the project before opening any of the forms

    http://www.obj-tec.com/MSDNForums/InheritedForms.zip



  • How do I access the application Form Name?