none
How to get the MainForm reference?

    Question

  • I'm looking for a similiar call in C# for the C++ MFC call AfxGetApp().

     

    From my mainForm I create and call a new class.  In this new class it something fails I want to be able to get a reference to the application or main form or whatever it is called in the .net environment so all my error handling is done in one place.

     

    I know I can pass in a reference to the main form, but isn't there a way to access it from one of the many "namespaces". 

     

    I assumed this would work, but it is NULL

     

    P2IDlog myDlog = (P2IDlog)TestADI.P2IDlog.ActiveForm;

     

    Where TestADI is the namespace for my application and P2IDlog is the class for my "mainform".

    The main form was created in the Main method.... Application.Run(new P2IDlog())

     

    Any help would get great.

    Chris

    Friday, April 06, 2007 4:19 PM

Answers

  • You could use Application.OpenForms[0], it has a reference to the first opened form.  You'll need a cast.  Passing "this" to the class constructor is superior of course.

    Better yet, if you want to centralize all your error handling, you should make the error handler "static" so everybody can call it without providing an object reference.
    Friday, April 06, 2007 5:32 PM

All replies

  • You could use Application.OpenForms[0], it has a reference to the first opened form.  You'll need a cast.  Passing "this" to the class constructor is superior of course.

    Better yet, if you want to centralize all your error handling, you should make the error handler "static" so everybody can call it without providing an object reference.
    Friday, April 06, 2007 5:32 PM
  • I get the error:

    'Application' does not exist in the current context

    for the line:

    Application.OpenForms[0] 

     

    In fact the intelsense doesn't even have the application object.

     

    Which of course brings me back to my original question.  How do i get a reference to the application object.  I would think it would be somehow global in scope and could be accessed from anywhere within the program.

     

    Thanks Chris

    Friday, April 06, 2007 5:56 PM
  • At the top of the source code file, add this statement:

      using System.Windows.Forms;
    Friday, April 06, 2007 6:01 PM
  • Declare the mainform as a static reference class in your Program.cs file, and then create the object in the Main() function:

    private static void Main(string[] args)
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    // next line has to be after Application.SetCompatibleTextRenderingDefault(false); 
    mf = new MainForm(); 
    Application.Run(mf);
    }
    public static MainForm mf = null;

    In your mainForm.cs file, declare a public method to access/pass into to members on the form:

    public void DisplayInTextBox(string newLine)

    public partial class MainForm : Form
    {
    ...
    DisplayInTextBox(string text)
    {
    textBox1.Text += text;
    }
    }

    Now you should be able to access objects within your mainForm in your Program.cs file:

    class otherPrgmClass
    {
    public void Display()
    {
    Program.mf.DisplayInTextBox("Put this Text in a textbox in the Mainform");
    }
    }

    That should do it.  
    Sunday, February 27, 2011 3:50 AM
  • Thank you so much.
    Thursday, September 26, 2013 10:05 PM