Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Unique CommandBar Hell


:P
On this page:

I guess I'm getting desperate <g>. I've been trying to figure out a way to get the correct command bar for the ASP.NET visual editor when a table or table cell is the active element and pop up a context menu through an add-in.

 

Now this seems trivial right? Find the command bar (using good old debugging code that loops through all the command bars and echo's back each command bar and associated pads), then just find the matching bar and voila you got your command bar.

 

Works great usually, except for a few command bars which seem to have several different instances of the command bar with the same fucking name! Bite me, but this is really lame.

 

So far I've not found a way to uniquely identify the commandbar and finally I got an answer in a Microsoft newsgroup that the only way to uniquely identify a CommandBar is by its GUID. Unfortunately the GUIDs are not accessible anywhere on the Commandbars (as properties) nor are they published anywhere so unless you are a Microsoftie and have access to the right people you're shit out of luck.

 

Now this sorta thing really pisses me off. The guy in the newsgroup was nice enough to offer looking up the GUID, but so far he hasn't come through. Not to knock him, but c'mon what kind of lousy design is this to have non-unique indexers and provide only an internal non-published ID that makes this work?

 

Anyway, I need to get this working and so I managed to find a sort of workaround which is this sort of messy code:

 

CommandBar cellBar = null;

foreach (CommandBar Bar in bars)

{

    if (Bar.Name == "Context")

    {

        foreach (CommandBarControl Ctl in Bar.Controls)

        {

            if (Ctl.Caption == "&Merge Cells")

            {

                cellBar = Bar;

                break;

            }

 

        }

        if (cellBar != null)

            break;

    }

}

 

I basically look through the Commands on each of the different bars that match the Context Command bar (of which there will be at least 4).

 

Previously I thought I can get away with a CommandBar.Id property, but that is not unique. In fact running on another machine resulted in a different Id.

 

The above code works, but it works only with English since the caption is localized. At this point that's better than nothing since most of the audience will be US.

 

I've got several CommandBars hooked so it also works when no tables are active or if a control is selected in a table, so I'm not completely hosed. Still this is really, really lame…


The Voices of Reason


 

Bobby DeRosa
June 01, 2006

# re: Unique CommandBar Hell

Dude, I'm right there with you. Trying to find a specific CommandBar w/out something unique to identify them by is ultra weak and doesn't sit well (feels like a hack).

I really hope MS invests some resources in better design next time or at least documentation.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2025