Question

                Form myForm = clsUIMethods.CreateFormInstance(iObjectAppId, sObjName, sFormCaption, sKey, toolCategory, sAccessibleDefaultActionDescription);




    public Form CreateFormInstance(int objectAppID, string formName, string formCaption, string formTag, string accessibleName, string accessibleDefaultActionDescription)
    {
        try
        {

            Assembly myAssembly = null;

            VersionInfo clsAssemblyInfo = GetAssemblyInfo(objectAppID);
            if (clsAssemblyInfo == null)
            {
                throw new Exception("Cannot open " + formCaption + ".\nPlease Check Your Application Menu Rights.", new Exception("Could not Load Assembly for : " + formName + "."));
            }

            formName = clsAssemblyInfo.NameSpace + "." + formName;

            try
            {
                if (objectAppID == 0)
                    myAssembly = Assembly.GetEntryAssembly();
                else if (objectAppID > 5000) //AppID above 5000 are for supporting projects 
                    myAssembly = Assembly.LoadFile(string.Format("{0}\\{1}.dll", Application.StartupPath, clsAssemblyInfo.AssemblyName));
                else
                    myAssembly = Assembly.Load(clsAssemblyInfo.AssemblyName);
            }
            catch
            {
                throw new Exception("Application file not Found.\nPlease Contact " + AppInstance.Name + " Co-ordinator.", new Exception("Could not Load Assembly for : " + clsAssemblyInfo.ApplicationName + "."));
            }

            //if (!clsAssemblyInfo.CheckVersionAtLogin)
            //{
            //    if (GetAssemblyVersion(myAssembly) != clsAssemblyInfo.Version)
            //    {
            //        throw new Exception("Object cannot be opened.\nYou are Running an Application of Different Version.", new Exception("Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + "."));
            //    }
            //}

            int iOverrideVersionControl = 0;
            try
            {
                if (ConfigurationManager.AppSettings["OverrideVersionControl"] != null)
                    iOverrideVersionControl = ConfigurationManager.AppSettings["OverrideVersionControl"].ToInt32();
            }
            catch { }

            if (iOverrideVersionControl != 1)
            {
                if (clsAssemblyInfo.CurrentAssemblyVersion != clsAssemblyInfo.Version)
                {
                    if (!clsAssemblyInfo.AllowOldVersion)
                    {
                        throw new Exception("Screen cannot be opened.\nYou are Running an Application of Different Version.", new Exception("Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + "."));
                    }
                    else
                    {
                        if (DisplayMessages("The application version of the screen being opened is different than the release version.\nDo you wish to continue ?", MessageStyle.YesNo, "Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + ".") == MessageResult.No)
                            throw new Exception("OLDVERSION");
                    }
                }
            }

            //if (objectAppID == 0)
            //    myAssembly = Assembly.GetEntryAssembly();
            //else
            //{
            //    try
            //    {
            //        myAssembly = Assembly.Load(clsAssemblyInfo.AssemblyName);
            //    }
            //    catch
            //    {
            //        throw new Exception("File not Found.\nPlease Contact " + AppInstance.Name + " Co-ordinator.", new Exception("Could not Load Assembly for : " + clsAssemblyInfo.ApplicationName + "."));
            //    }

            //    if (!clsAssemblyInfo.CheckVersionAtLogin)
            //    {
            //        if (GetAssemblyVersion(myAssembly) != clsAssemblyInfo.Version)
            //        {
            //            throw new Exception("Object cannot be opened.\nYou are Running an Application of Different Version.", new Exception("Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + "."));
            //        }
            //    }

            //    //string str = myAssembly.ImageRuntimeVersion; 
            //    //FileInfo fileInfo = new FileInfo(Directory.GetCurrentDirectory() + @"\" + assemblyName);
            //    //if (fileInfo.Exists)
            //    //myAssembly = Assembly.LoadFile(fileInfo.FullName);
            //    //else
            //    //    throw new Exception("File Not Found.\nPlease Contact " + AppInstance.Name + " Co-ordinator.", new Exception("Could not Load Assembly : " + assemblyName + "."));
            //}

            //myAssembly.GetName().CultureInfo = glMod.GetCultureInfo();

            Form myForm = myAssembly.CreateInstance(formName, true) as Form;
            if (myForm != null)
            {
                MainForm.Instance.AskBeforeClosingForm = true;
                if (formCaption != string.Empty)
                    myForm.Text = formCaption;
                myForm.Tag = formTag;
                myForm.AccessibleName = accessibleName;
                myForm.AccessibleDefaultActionDescription = accessibleDefaultActionDescription;
                myForm.KeyPreview = true;
                return myForm;
            }
            else
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Please tell me how to catch exception which happens on a button click event on the form which is created using the method CreateFormInstance() .

My button click code is :

  private void button1_Click(object sender, EventArgs e)
        {
            throw new NullReferenceException("nullref test muh sa");
        }

I want the exception in the parent form which created this form.

Was it helpful?

Solution

You can add an event handler to 'Application.ThreadException' this will allow you to run code and causes your application to not exit whenever a uncaught Exception is thrown on your UI(main) thread. Alternitivly you can also handle 'AppDomain.CurrentDomain.UnhandledException' this will allow you to run code whenever an exception is thrown on any thread in you application but doesn't prevent your application from exiting

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top