0
   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; 
     } 
    } 

請告訴我如何捕捉異常恰好在按鈕單擊事件,其使用方法CreateFormInstance()創建的窗體 上。捕獲異常是由Assembly.CreateInstance()方法創建的反思

我按一下按鈕的代碼是

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

我想在其中創建這種形式的父窗體例外。

+1

那麼,問題的很大一部分可能是'catch(Exception ex){throw ex; }'......但是:現在究竟發生了什麼,以及這與你期望發生的事情相比如何?注意:如果你不想讓按鈕點擊來爆炸應用程序,那麼你的*按鈕點擊*應該捕獲異常。 –

+0

可能你必須在子窗體中實現某種通知。一種方法是在發生異常時觸發子表單中的事件。然後,父表單可以在創建實例後訂閱它。 –

回答

0

您可以添加一個事件處理程序「Application.ThreadException」這將允許您運行的代碼,並導致您的應用程序只要未捕獲的異常是在你的UI(主)線程拋出不退出。另外,你也可以處理'AppDomain.CurrentDomain.UnhandledException'這將允許你運行代碼,當你的應用程序中的任何線程拋出異常,但不會阻止你的應用程序退出

相關問題