1

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...)

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Ruthg
  • 241
  • 2
  • 4
  • 11
  • 1
    Seriously, you've written an addin for Visual Studio and you don't know how to do *that*? What have you tried, so far? Maybe setting `((Button)sender).Enabled = false;` at the event handler? – Andre Calil Jul 31 '12 at 09:50
  • the buttons are commands.AddNamedCommand2 type... cannot add these events. (it is not windows form... it add-in -> these buttons are exist above the Tools tab, they are not regular buttons) – Ruthg Jul 31 '12 at 10:24
  • But they must inherit from `Controls` or something like it. Are you following any documentation or tutorial? Could you link it at the question? – Andre Calil Jul 31 '12 at 18:57

5 Answers5

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

void Btn2_Click(Object sender, EventArgs e)
{
   Btn1.Enabled = false;
}
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
1

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....

CSharp
  • 1,573
  • 2
  • 14
  • 25
  • the buttons are commands.AddNamedCommand2 type... cannot add these events. (it is not windows form... it add-in -> these buttons are exist above the Tools tab, they are not regular buttons) – Ruthg Jul 31 '12 at 10:21
0

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

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Ursegor
  • 878
  • 8
  • 16
0

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;
    }
SNP
  • 37
  • 11
-1
// 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);
sloth
  • 99,095
  • 21
  • 171
  • 219
arsone
  • 1