简体   繁体   English

单击后如何禁用和启用加载项按钮

[英]how to disable and enable add-in button after click

I write addin for visual studio that includes two button.我为包含两个按钮的 Visual Studio 编写插件。 I want that when user will hit one of them, this button will be disable and the another one will be enable.我希望当用户点击其中一个按钮时,此按钮将被禁用,而另一个按钮将被启用。 how can I do it?我该怎么做?

The buttons are Command type (commands.AddNamedCommand2...)按钮是命令类型 (commands.AddNamedCommand2...)

thanks in advanced!提前致谢!

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

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

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....希望能帮到你....

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).可以通过IDTCommandTarget接口的QueryStatus方法修改状态(如果加载了加载项)。

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您可以通过将状态设置为vsCommandStatus.vsCommandStatusUnsupported来禁用按钮

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;此外,如果您想在初始状态下启用或禁用这些按钮。例如,您想在程序中禁用按钮 1。在声明程序后,您想在单击按钮 2 后启用按钮 1,然后您想在页面加载时添加按钮状态为如下;

 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;
    }
// 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM