'how to disable and enable add-in button after click

I am writing an add-on for visual studio that includes two buttons. I want that when the user hits one of them, this button will be disabled and the another one will be enabled. How can I do it?

The buttons are Command type (commands.AddNamedCommand2...)



Solution 1:[1]

void Btn1_Click(Object sender, EventArgs e)
{
   Btn2.Enabled = false;
}

void Btn2_Click(Object sender, EventArgs e)
{
   Btn1.Enabled = false;
}

Solution 2:[2]

You can try this:

private void button1_Click(object sender, EventArgs e)
    {
        this.button1.Enabled = false;
        this.button2.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.button2.Enabled = false;
        this.button1.Enabled = true;
    }  

Hope this will help you....

Solution 3:[3]

You can associate some state variable with your buttons wich can express whether your button should be enabled in the recent state of your Add-In. When your Add-In itinializes, you should set the state as you whish. In the query you can check the recent state of your Add-In and set the buttuns into their proper enabled / unsupported state.

The state can be modified through the QueryStatus method of the IDTCommandTarget interface (If your Add-In is loaded).

Default template implementation:

public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
    if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
    {
        if(commandName == "YourAddin.Connect.YourAddinCommandName")
        {
            status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
            return;
        }
    }
}

You can disable a button by setting the status to vsCommandStatus.vsCommandStatusUnsupported

Solution 4:[4]

Furthermore If you want to enable or disable those buttons in their initial status.As an example you want to disable button1 in the program.After stating program you want to enable button1 after clicking button2,then You want to add button status on page loading as follows;

 private void Form1_Load(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            this.button2.Enabled = true;
        }
 private void button2_Click(object sender, EventArgs e)
    {       
        this.button1.Enabled = true;
    }

Solution 5:[5]

// Remove event handler
this.button_Ping.Click -= new System.EventHandler(this.button_Ping_Click);

Some action...

// Add event handler
this.button_Ping.Click += new System.EventHandler(this.button_Ping_Click);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aghilas Yakoub
Solution 2 CSharp
Solution 3 Josh Crozier
Solution 4 SNP
Solution 5 sloth