Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1878
Different toolbar button in ToolClickEventArgs
posted

I've created a context menu within ToolbarManager containing several buttons. I set the Tag property for a toolbar button within Visual Studio Designer to a string that I want to get in the ToolClick event handler so I do like this:

private void OnToolClick(object sender, ToolClickEventArgs e)
{
   switch (e.Tool.Key)
   {
      case "New":
         string s=e.Tool.Tag as string;
         break;
   }
}

But this string is always null. If I explicitely get the 'New'-button from the ToolbarManager and get the Tag from it it is working correctly:

private void OnToolClick(object sender, ToolClickEventArgs e)
{
   switch (e.Tool.Key)
   {
      case "New":
         ToolBase toolBase = ToolbarsManager.Tools["New"];
         string s=toolBase.Tag as string;
         break;
   }
}

I verified the Key-property and in both cases it is the toolbar button with the same Key. I also ran a loop to output all tools of ToolbarsManager to see if I have two buttons with same Key but that's not the case. When I output the Hashcode of both objects I get different hashcodes so it seems to me that the Toolbase object in ToolClickEventArgs is a different one to that I get from ToolbarsManager.

I also tried if it is has something to do with that I'm using this button in a context menu which is some kind of dynamically created but even when I add this toolbar button into the toolbar it seems to be a different object.

Is the toolbar button object kind of copied when the event is fired? If this is the case what is the reason and why is the Tag property not copied.

Regards, Wolfgang