Enabling an Addin Button From a Modeless Form

727
5
12-19-2013 07:57 AM
BruceNielsen
Occasional Contributor III
I have an Addin button (called Analyze) that processes some data, then calls a modeless form. I want the button to be disabled until the user closes the form, or else it will be possible for the user to accidentially have multiple instances of the form open at the same time.

To disable the button I use
Me.Enabled = False
in the button's OnClick event. What I can't figure out is how to reference the button from within the form. I was planning on changing the button's Enabled attribute back to True in the form's FormClosing event, unless someone knows of a better way.

Any Ideas?
0 Kudos
5 Replies
LeoDonahue
Occasional Contributor III
What I can't figure out is how to reference the button from within the form

Because you don't have access to the variables you need?  (I'm guessing).

This is solved by using a static factory method in the Form that returns an instance of the form.  I don't know if you can do this in VBA, I'm sure someone will reply with a global variable example.
0 Kudos
BruceNielsen
Occasional Contributor III
I'm using Visual Studio 2010, .NET 3.5 with ArcGIS 10.0.
0 Kudos
NeilClemmons
Regular Contributor III
I have an Addin button (called Analyze) that processes some data, then calls a modeless form. I want the button to be disabled until the user closes the form, or else it will be possible for the user to accidentially have multiple instances of the form open at the same time.

To disable the button I use
Me.Enabled = False
in the button's OnClick event. What I can't figure out is how to reference the button from within the form. I was planning on changing the button's Enabled attribute back to True in the form's FormClosing event, unless someone knows of a better way.

Any Ideas?


Without seeing you actual code I can only offer a possible solution.  We do this all the time, one of our commands opens a modeless dialog and we keep the command button checked for as long as the dialog is open.  To do this, we create a form level variable for the dialog.  In the OnClick event, we create the instance of the dialog and open it.  In the Checked event, we check the visibility of the form and return the value accordingly.

Here's the form variable declaration:
Private m_form As yourFormClass

Here's the code in OnClick:
    Public Sub OnClick() Implements ESRI.ArcGIS.SystemUI.ICommand.OnClick
        If m_form IsNot Nothing AndAlso m_form.Visible Then Return


        m_form = New yourFormClass()
        m_form.Show()
    End Sub

Here's the code in Checked:
    Public ReadOnly Property Checked As Boolean Implements ESRI.ArcGIS.SystemUI.ICommand.Checked
        Get
            Return (m_form IsNot Nothing AndAlso m_form.Visible)
        End Get
    End Property
0 Kudos
LeoDonahue
Occasional Contributor III
The Java version of Neil's example is very similar.

The Button class
public class Button1 extends Button {
    
    private MyForm myForm = null;

    @Override
    public void onClick() throws IOException, AutomationException {
        if(myForm == null){
            myForm = MyForm.getInstance();
        }
        
        myForm.setVisible(true);
    }

}


The Form class
public class MyForm extends JFrame {

    private JPanel contentPane;

    private static MyForm instance = null;
    
    public static MyForm getInstance(){
        if(instance == null){
            instance = new MyForm();
        }
        return instance;
    }

    /**
     * Create the frame.
     */
    public MyForm() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
    }
   
    // handle closing of the form, maybe dispose on close...
}
0 Kudos
HaoliangYu
New Contributor
Hi, here is the code to get an addin button in C#
// Enable the save button
SaveEditingButton saveButton = AddIn.FromID<SaveEditingButton>(ThisAddIn.IDs.RasterEditor_EditorMenu_Edition_SaveEditingButton);
saveButton.IsEnabled = true;


IDs of addin controls are defined at Config.esriaddinx in your solution folder. I think it can solve your problem.

Haoliang
0 Kudos