How to check which toolbar button was clicked and then cancel the action

There are two parts to this question:

  1. Determine which toolbar button was clicked and
  2. Cancel a save action (or more generally, any action from clicking a button on the toolbar) if a condition is met.

When the baseToolbarsManager_ToolClick() event is triggered, how can I determine which button was clicked? I need to check if the save button was clicked, and then cancel the save (oTrans.Update(), correct?) if the OKCancel message box returns a cancel DialogResult.

I’m wondering if the BeforeToolClick event would be better suited for this. I don’t know enough about the toolbar order of actions, but I’m thinking I could either let the Save happen if OK is pressed, or cancel it and prevent the toolclicked() action from occuring. I’d love to be enlightened on this process.

Look at this for question 1. I think this will answer that one.

I got that part down. Now it’s figuring out how to stop the toolclick action to prevent the save.

Whatever the condition is that makes you want to stop the save, do an if or switch to test the condition and either throw an error or allow the event to continue.

Here’s how I handled it. In its own function I stored the dialog result from message box. The OK result returned false and Cancel returned true. It returned to the ToolClick function, where, if the result was true, then args.handled = true. Setting args.handled to true cancelled the proceeding actions. To reset the form to how it was before clicking save, I called oTrans.Refresh();

private bool DisplayOKCancelMessage()
{
    if ((System.Decimal)materialCost.Value != 0 ||
        (System.Decimal)burdenCost.Value != 0 ||
        (System.Decimal)subcontractCost.Value != 0 ||
        (System.Decimal)materialBurdenCost.Value != 0 ||
        (System.Decimal)laborCost.Value != 0)
    {
        DialogResult dialogResult = MessageBox.Show("Please make sure all cost fields are set to $0 before receiving this RMA. Continue?", "Warning",
                                                    MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        if (dialogResult == DialogResult.OK)
        {
            // Do nothing and let the form save
            return false;
        }
        else if (dialogResult == DialogResult.Cancel)
        {
            // Cancel the save action
            return true;
        }
    }
    return false;
}


private void baseToolbarsManager_ToolClick(object sender, Ice.Lib.Framework.BeforeToolClickEventArgs args)
{
    if (args.Tool.Key == "SaveTool")
        if (DisplayOKCancelMessage())
            args.Handled = true;
}
3 Likes