I have an add-in program that has several forms. When the program is done with one form, it automatically goes to the next method it needs. (Word add-in). My problem is this...When I hit cancel, I want it to stop all actions with-in my add-in. Right now, if I hit cancel, it just continues to the next method. I want to cease all actions, and undo what was done essentially. Enivronment.Exit(exitcode) doesn't work because it closes Word all together.

I want it to just spot the add-in from moving to the next method. Any suggestions?

You're going to need to post code for this I think. I don't understand enough of the problem to help from what you have posted.

Have you tried

Application.Close()

?
If you have, can you please post your code here so we can help you!

Okay this is my main class. It is an IDTExtensibility2 add-in.
So when I call SignatureSetup I open my form SigSetup.
After it is done with this method, it automatically goes to the next method. If I click cancel on the form, I want the add-in to stop, and not go to the next method. So I need to kill the add-in, somehow.

Code for Main:

public class Connect : Object, Extensibility.IDTExtensibility2, SignatureProvider 
    {

        private object applicationObject;
        private object addInInstance;
        private Image sigimgfinal;
        private string tempPath = Path.GetTempPath();
        private int count = 0;
        

public Connect()
		{
            
		}

		
		public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
		{
			applicationObject = application;
			addInInstance = addInInst;
           
		}

public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
		{
		}

public void OnAddInsUpdate(ref System.Array custom)
		{
		}

public void OnStartupComplete(ref System.Array custom)
		{
		}

public void OnBeginShutdown(ref System.Array custom)
		{
		}
		
		
        #region SignatureProvider Members

        public IPictureDisp GenerateSignatureLineImage(SignatureLineImage siglnimg, SignatureSetup sigsetup,
                                                        SignatureInfo siginfo, object XmlDsigStream)
        {

            //all my code
        }

        public void ShowSignatureSetup(object parentWindow, SignatureSetup sigsetup)
           {
            
              SigSetup sigsetup = new SigSetup();
              sigsetup.ShowDialog();
            }

        public void ShowSigningCeremony(object parentWindow, SignatureSetup sigsetup, SignatureInfo siginfo)
        {
              //all my code
        }

        public void SignXmlDsig(object queryContinue, SignatureSetup sigsetup, SignatureInfo siginfo,
                                object xmldsigStream)
        {
         
          throw new NotImplementedException();
         
        }

        public void NotifySignatureAdded(object parentWindow, SignatureSetup sigsetup, SignatureInfo siginfo)
        {
            //all my code
        }

        public void VerifyXmlDsig(object queryContinue, SignatureSetup sigsetup, SignatureInfo siginfo,
                                  object xmldsigStream, ref ContentVerificationResults contverresults,
                                  ref CertificateVerificationResults certverresults)
        {
           
            throw new NotImplementedException();
        
        }

        public void ShowSignatureDetails(object parentWindow, SignatureSetup sigsetup, SignatureInfo siginfo,
                                         object xmldsigStream, ref ContentVerificationResults contverresults,
                                         ref CertificateVerificationResults certverresults)
        {
           //my code
        }

        public object GetProviderDetail(SignatureProviderDetail sigProvDetail)
        {
            switch (sigProvDetail)
            {
                case Microsoft.Office.Core.SignatureProviderDetail.sigprovdetHashAlgorithm:
                    return this.HashAlgorithmIdentifier;

                case Microsoft.Office.Core.SignatureProviderDetail.sigprovdetUIOnly:
                    return true;

                case Microsoft.Office.Core.SignatureProviderDetail.sigprovdetUrl:
                    return this.ProviderUrl;

                default:
                    return null;
            }
        }

        public Array HashStream(object queryContinue, object stream)
        {
            throw new NotImplementedException();
            
        }

        protected static X509Certificate2 GetSigningCertificate(SignatureInfo siginfo)
        {
          //all my code
        }

       
        #endregion SignatureProvider Members
         
        #region Properties
        public virtual string HashAlgorithmName { get { return "SHA1"; } }
        public virtual string HashAlgorithmIdentifier { get { return SignedXml.XmlDsigSHA1Url; } }
        public virtual string ProviderUrl { get { return "http://www.microsoft.gov"; } }
        #endregion Properties


    }

Here is the code for the form:

public partial class SigSetupForm : Form
    {
        public string username, email, signingfor, signreason;
        
        public SigSetupForm(SignatureSetup sigsetup)
        {
            InitializeComponent();
            //stuff I initialize
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
           //Doesn't work. I need this to kill the add in so it doesn't go on to the next method.
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            //my code
        }

        private void SigSetupForm_Load(object sender, EventArgs e)
        {
            //my code

         }

        private void chkSignNames_CheckedChanged(object sender, EventArgs e)
        {
            //code
        }

Like I said, I just want to kill the add-in if I hit cancel. Even if in btnCancel I put this.Close() it will continue to the next method automatically. I don't want it to do that.

When the signature button is clicked, it goes to the signature setup method. After that one executes it goes to the generate signature line.

When the signature button is clicked, it goes to the signature setup method. After that one executes it goes to the generate signature line.

can you please upload your entire solution in a zip file to here because i really cant find the problem like that.

Sure you can. When I click Insert Signature, in Microsoft Word, it runs the ShowSignatureSetup method. From there, you can see that calls the Form in the second batch of code.

If I were to click enter, it would run the code I placed in there.
Then it would go back to the Connect class automatically, and run the next method in line. Which is the GenerateSignatureLine method. And that code would run.

If I click cancel on the Signature Setup form, I want the add-in to stop. Office implements this add-in on its own. Connect is the main class. And I don't need to call each method in there because Word automatically does it itself. I just want the cancel button to kill the add-in.

Maybe it has something to do with the add-in instance object? I don't know. But putting this.Close() in the cancel portion continues on to GenerateSignatureLine. I don't want it to make it that far.

Maybe it has something to do with the add-in instance object? I don't know. But putting this.Close() in the cancel portion continues on to GenerateSignatureLine. I don't want it to make it that far.

Maybe...! but we need to solve it.

Have you thought about creating a boolean object in your Connect class (a public static one) with a default value of true, and then if you press the cancel button so its value change to false,
and in the "GenerateSignatureLine" use it as "if()...".


And one last thing:

Sure you can....

What i meant is that i cant find any problem in your code,
but if i had you entire code(solution) so maybe i will be able to help you more.

Technically the if statement would work because you don't return a IPictureDisp which the method needs to continue. But it locks the screen up temporarily. I don't think that would be very sound programming either.

Plus in Word's actual version of a digital signature you can hit cancel and it is instantaneous. And it cancel's fine. It is probably just a few lines of code I am looking for.

There are multiple forms that need a cancel button, and the if wouldn't work for all of them. Look at the link I provided, my code looks somewhat similar to that. With the addition of my forms.
There has to be a way to just end the add in.

How would you end a regular c# program?

Okay I finally figured out one way to do it, and not let it get caught up for a second. You have to create an instance of the Word.Addin
class.
Microsoft.Office.Interop.Word.AddIn addin = (Microsoft.Office.Interop.Word.AddIn)addInInstance;
Does the trick.
Once you create that object you can use addin.Delete() to make the add-in stop for the time being.
This seems like an odd solution, but it works for all forms.
Thanks for the help everyone, and if someone reads this, and finds a much more efficient way. let me know, because I had to add like 5 lines of code for each form. I would love to make it one line per form, if someone knew how.

I'm happy you solved your problem but,

I dont understand why to do addin.Delete() for my understanding is to delete the addin, so every time that you run your app after stoping it manually it creates the addin (register) from scratch, and when it stops alone so it does not create the addin, I'm not sure its the best way to solve the problem, but maybe the delete() function dont do what i described above and if its not so tell me!

This question has already been answered. Start a new discussion instead.