private void btnRun_Click(object sender, EventArgs e)
    {

      double dConvTol=0.003;
      int iRepeatCount=2;
      double dMaxShift = 0;

      double dVal=0;
      if(Double.TryParse(this.txtMainDistResReport.Text, out dVal))
      {
      //write to registry
        Utilities FabUTILS= new Utilities();
        string sVersion = FabUTILS.GetDesktopVersionFromRegistry();
        FabUTILS.WriteToRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSADistanceToleranceReport", this.txtMainDistResReport.Text, true);
        FabUTILS=null;
      }

      bool bAdjResult=true;
      string sSummary="";

      #region Setup Adjustment & Verify that there's enough information
      
       ICadastralPacketManager pCadastralPacketManager = null;
       ICadastralAdjustment pCadAdj = null;
       ICadastralAdjustment3 pCadAdjEx = null;
       ICadastralMapEdit pCadMapEdit =null;

        LoadValuesFromRegistry();

        UID pUID = new UIDClass();
        pUID.Value = "{114D685F-99B7-4B63-B09F-6D1A41A4DDC1}";
        ICadastralEditor pCadEd = (ICadastralEditor)ArcMap.Application.FindExtensionByCLSID(pUID);
        ICadastralFabric pCadFabric = pCadEd.CadastralFabric;

        pCadastralPacketManager = (ICadastralPacketManager)pCadEd;
        bool open = pCadastralPacketManager.PacketOpen;

        ISelectionSet pBeforeSS = null;
        IFeatureSelection pParcelSelection = null;

        if (!open)
        {
          ICadastralSelection pSelection = (ICadastralSelection)pCadEd;
          IEnumGSParcels pParcels = pSelection.SelectedParcels;
          IEnumCEParcels pCEParcels = (IEnumCEParcels)pParcels;
          if (pCEParcels != null)
          {
            long count = pCEParcels.Count;
            if (count == 0)
            {
              MessageBox.Show("There are no parcels selected to adjust." + Environment.NewLine
              + "Please select parcels and try again.", "Fabric Adjustment");
              return;
            }
          }

          ICadastralFabricLayer pCFLayer = pCadEd.CadastralFabricLayer;
          if (pCFLayer != null)
          {
            IFeatureLayer pParcelLayer = pCFLayer.get_CadastralSubLayer(esriCadastralFabricRenderer.esriCFRParcels);
            pParcelSelection = (IFeatureSelection)pParcelLayer;
            if (pParcelSelection != null)
              pBeforeSS = pParcelSelection.SelectionSet;
          }
        }

        pCadMapEdit = (ICadastralMapEdit)pCadEd;
        pCadMapEdit.StartMapEdit(esriMapEditType.esriMEParcelSelection, "Fabric Adjustment", false);

        ICadastralPacket pCadastralPacket = pCadastralPacketManager.JobPacket;
        int cpCount = 0;

        ICadastralControlPoints pCPts = (ICadastralControlPoints)pCadastralPacket;
        if (pCPts != null)
        {
          IGeometry pGeom = null;
          IEnumGSControlPoints pEnumCPs = pCPts.GetControlPoints(pGeom);
          pEnumCPs.Reset();
          IGSControlPoint pCP;
          if (pEnumCPs != null)
          {
            pCP = pEnumCPs.Next();
            while ((pCP != null) && (cpCount < 2))
            {
              if (pCP.Active)
                cpCount++;
            }
          }
        }

        if (cpCount < 2)
        {
          MessageBox.Show("Please make sure that at least 2 control points are" +
          Environment.NewLine + "attached to the selected parcels.", "Fabric Adjustment");
          return;
        }
        pCadAdj = (ICadastralAdjustment)pCadastralPacket;
        pCadAdjEx = (ICadastralAdjustment3)pCadastralPacket;
        ApplyAdjustmentSettings(pCadAdj, pCadAdjEx);
      
      #endregion

        double dHighestMaxShift = 0;

      //// Change display text depending on count
      //string itemText = count > 1 ? "items" : "item";

      for (int i =1; i<=iRepeatCount; i++)
      {
        if(!RunAdjustment(pCadastralPacketManager,pCadAdj,pCadAdjEx, i, out dMaxShift, out sSummary))
        {
          bAdjResult=false;
          break;
        }
        if(dHighestMaxShift > dMaxShift)
          dHighestMaxShift=dMaxShift;

        pCadAdj.AcceptAdjustment();
        if(dMaxShift<dConvTol)
          break;
      }

      if(bAdjResult)
        lblAdjResult.Text="Adjustment Complete";
      else
        lblAdjResult.Text="Adjustment Failed";

      lblAdjResult.Visible=true;

      dlgAdjustmentResults AdjResults = new dlgAdjustmentResults();
      AdjResults.txtReport.Text = sSummary;

      //Display the dialog
      DialogResult pDialogResult = AdjResults.ShowDialog();
      if (pDialogResult != DialogResult.OK)
      {
        AdjResults = null;
        pCadMapEdit.StopMapEdit(false);
      }
      else
      {
        pCadMapEdit.StopMapEdit(true);
      }

      pParcelSelection.SelectionSet = pBeforeSS;

      Utilities FabUTILS2= new Utilities();
      FabUTILS2.RefreshFabricLayers(ArcMap.Document.FocusMap,pCadFabric);
      FabUTILS2=null;

    }
        private void btnRun_Click(object sender, EventArgs e)
        {
            double dConvTol     = 0.003;
            int    iRepeatCount = 2;
            double dMaxShift    = 0;

            double dVal = 0;

            if (Double.TryParse(this.txtMainDistResReport.Text, out dVal))
            {
                //write to registry
                Utilities FabUTILS = new Utilities();
                string    sVersion = FabUTILS.GetDesktopVersionFromRegistry();
                FabUTILS.WriteToRegistry(RegistryHive.CurrentUser,
                                         "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
                                         "LSADistanceToleranceReport", this.txtMainDistResReport.Text, true);
                FabUTILS = null;
            }

            bool   bAdjResult = true;
            string sSummary   = "";

            #region Setup Adjustment & Verify that there's enough information

            ICadastralPacketManager pCadastralPacketManager = null;
            ICadastralAdjustment    pCadAdj     = null;
            ICadastralAdjustment3   pCadAdjEx   = null;
            ICadastralMapEdit       pCadMapEdit = null;

            LoadValuesFromRegistry();

            UID pUID = new UIDClass();
            pUID.Value = "{114D685F-99B7-4B63-B09F-6D1A41A4DDC1}";
            ICadastralEditor pCadEd     = (ICadastralEditor)ArcMap.Application.FindExtensionByCLSID(pUID);
            ICadastralFabric pCadFabric = pCadEd.CadastralFabric;

            pCadastralPacketManager = (ICadastralPacketManager)pCadEd;
            bool open = pCadastralPacketManager.PacketOpen;

            ISelectionSet     pBeforeSS        = null;
            IFeatureSelection pParcelSelection = null;

            if (!open)
            {
                ICadastralSelection pSelection = (ICadastralSelection)pCadEd;
                IEnumGSParcels      pParcels   = pSelection.SelectedParcels;
                IEnumCEParcels      pCEParcels = (IEnumCEParcels)pParcels;
                if (pCEParcels != null)
                {
                    long count = pCEParcels.Count;
                    if (count == 0)
                    {
                        MessageBox.Show("There are no parcels selected to adjust." + Environment.NewLine
                                        + "Please select parcels and try again.", "Fabric Adjustment");
                        return;
                    }
                }

                ICadastralFabricLayer pCFLayer = pCadEd.CadastralFabricLayer;
                if (pCFLayer != null)
                {
                    IFeatureLayer pParcelLayer = pCFLayer.get_CadastralSubLayer(esriCadastralFabricRenderer.esriCFRParcels);
                    pParcelSelection = (IFeatureSelection)pParcelLayer;
                    if (pParcelSelection != null)
                    {
                        pBeforeSS = pParcelSelection.SelectionSet;
                    }
                }
            }

            pCadMapEdit = (ICadastralMapEdit)pCadEd;
            pCadMapEdit.StartMapEdit(esriMapEditType.esriMEParcelSelection, "Fabric Adjustment", false);

            ICadastralPacket pCadastralPacket = pCadastralPacketManager.JobPacket;
            int cpCount = 0;

            ICadastralControlPoints pCPts = (ICadastralControlPoints)pCadastralPacket;
            if (pCPts != null)
            {
                IGeometry            pGeom    = null;
                IEnumGSControlPoints pEnumCPs = pCPts.GetControlPoints(pGeom);
                pEnumCPs.Reset();
                IGSControlPoint pCP;
                if (pEnumCPs != null)
                {
                    pCP = pEnumCPs.Next();
                    while ((pCP != null) && (cpCount < 2))
                    {
                        if (pCP.Active)
                        {
                            cpCount++;
                        }
                    }
                }
            }

            if (cpCount < 2)
            {
                MessageBox.Show("Please make sure that at least 2 control points are" +
                                Environment.NewLine + "attached to the selected parcels.", "Fabric Adjustment");
                return;
            }
            pCadAdj   = (ICadastralAdjustment)pCadastralPacket;
            pCadAdjEx = (ICadastralAdjustment3)pCadastralPacket;
            ApplyAdjustmentSettings(pCadAdj, pCadAdjEx);

            #endregion

            double dHighestMaxShift = 0;

            //// Change display text depending on count
            //string itemText = count > 1 ? "items" : "item";

            for (int i = 1; i <= iRepeatCount; i++)
            {
                if (!RunAdjustment(pCadastralPacketManager, pCadAdj, pCadAdjEx, i, out dMaxShift, out sSummary))
                {
                    bAdjResult = false;
                    break;
                }
                if (dHighestMaxShift > dMaxShift)
                {
                    dHighestMaxShift = dMaxShift;
                }

                pCadAdj.AcceptAdjustment();
                if (dMaxShift < dConvTol)
                {
                    break;
                }
            }

            if (bAdjResult)
            {
                lblAdjResult.Text = "Adjustment Complete";
            }
            else
            {
                lblAdjResult.Text = "Adjustment Failed";
            }

            lblAdjResult.Visible = true;

            dlgAdjustmentResults AdjResults = new dlgAdjustmentResults();
            AdjResults.txtReport.Text = sSummary;

            //Display the dialog
            DialogResult pDialogResult = AdjResults.ShowDialog();
            if (pDialogResult != DialogResult.OK)
            {
                AdjResults = null;
                pCadMapEdit.StopMapEdit(false);
            }
            else
            {
                pCadMapEdit.StopMapEdit(true);
            }

            pParcelSelection.SelectionSet = pBeforeSS;

            Utilities FabUTILS2 = new Utilities();
            FabUTILS2.RefreshFabricLayers(ArcMap.Document.FocusMap, pCadFabric);
            FabUTILS2 = null;
        }