Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / Extensibility / August 2004

Tip: Looking for answers? Try searching our database.

Deleting a CommandBar Object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick Strahl [MVP] - 13 Aug 2004 01:48 GMT
I'm trying to clean up after my code. So I'm temporarily creating a
commandBar object like this:

CommandBar commandBar =
applicationObject.Commands.AddCommandBar(HelpBuilderVsAddin.WWHELP_APPNAME,

vsCommandBarType.vsCommandBarTypeMenu,commandBarHelp,InsertionIndex) as
CommandBar;

And then in OnDisconnection  cleaning up with this:

try
{
  // *** Let's remove the Command Bar as well
  CommandBar cbx =
applicationObject.CommandBars[HelpBuilderVsAddin.WWHELP_APPNAME];
  if (cbx != null)
     cbx.Delete();
}
catch(Exception ex)
{
  MessageBox.Show(ex.Message);
}

Problem is the above code is failing with an Unspecified error. I get a ref
to the Commandbar but it won't delete.

When I run through the Controls of the menu that this CommandBar gets added
to I can see that the CommandBar keeps getting added without ever getting
removed so there are a number of entries there...

How do I get rid of hte command bar so it doesn't clutter up the parent menu
controls collection?

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web

Carlos J. Quintero [MVP] - 13 Aug 2004 11:07 GMT
There are several problems here:

- Regarding lifetime, there are 2 kind of command bars in VS.NET: temporary
and permanent. Permanent command bars are added via
DTE.Commands.AddCommandBar (your case) and are intended NOT to be removed
once your add-in is unloaded. So, if you want to remove it when the add-in
is unloaded, you should use a temporary command bar, which are added via
CommandBar.Controls.Add(MsoControlType.msoControlPopup)

- Regarding appearance, there are 3 kind of command bars in VS.NET: menu,
popup and toolbar. If you want to add your command bar as a submenu of the
Help menu, them you must use the POPUP kind.

See my article and change "Code Window" by "Help":

HOWTO: Add a popup command bar to the context menu of a code window of
Visual Studio .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;555153

Signature

Carlos J. Quintero (Visual Developer - .NET MVP)

FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)

> I'm trying to clean up after my code. So I'm temporarily creating a
> commandBar object like this:
>
> CommandBar commandBar =

applicationObject.Commands.AddCommandBar(HelpBuilderVsAddin.WWHELP_APPNAME,

> vsCommandBarType.vsCommandBarTypeMenu,commandBarHelp,InsertionIndex) as
> CommandBar;
[quoted text clipped - 25 lines]
>
> +++ Rick ---
Rick Strahl [MVP] - 14 Aug 2004 02:09 GMT
Carlos,

I remember looking at this a while back but I couldn't make this work mainly
because the example doesn't talk about the rest of the parameters. It would
be really helpful if you could post those examples with Option Strict off if
VB is used <g>.

After wasting another hour on this stuff and looking at the lame a.s docs
for CommandBars/Commands. I managed to get this to work:
CommandBarControl commandBarPopup =
commandBarHelp.Controls.Add(MsoControlType.msoControlPopup,1,"",InsertionInd
ex,true);

commandBarPopup.Caption = HelpBuilderVsAddin.WWHELP_APPNAME;

commandBarPopup.BeginGroup = true;

commandBarPopup.Visible = true;

But the main problem I had was trying to get a CommandBar object so I can
add my Commands to it. The trick is to cast to CommandBarPopup and then use
the CommandBar property which is not part of the managed wrappers. You have
to use Reflection to get a reference to it. With that in place the rest was
easy.

// *** Must retrieve the Command Bar Object directly via COM - not provided
on the object itself

CommandBar commandBar = wwUtils.GetProperty(commandBarPopup,"CommandBar") as
CommandBar;

I suppose I should also not use AddNamedCommand, but Commands.Add() but
that's yet another excercise that I don't feel like wasting my time on
today...

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web

> There are several problems here:
>
[quoted text clipped - 19 lines]
> >
> > CommandBar commandBar =

applicationObject.Commands.AddCommandBar(HelpBuilderVsAddin.WWHELP_APPNAME,

> > vsCommandBarType.vsCommandBarTypeMenu,commandBarHelp,InsertionIndex) as
> > CommandBar;
[quoted text clipped - 28 lines]
> >
> > +++ Rick ---
Dustin Campbell - 14 Aug 2004 04:50 GMT
> I remember looking at this a while back but I couldn't make this work
> mainly because the example doesn't talk about the rest of the
[quoted text clipped - 6 lines]
> commandBarHelp.Controls.Add(MsoControlType.msoControlPopup,1,"",Insert
> ionInd ex,true);

You can pass System.Type.Missing for the optional parameters in this
method. The following should work fine:

CommandBarControl commandBarPopup =
commandBarHelp.Controls.Add(MsoControlType.msoControlPopup,
System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing);

Signature

Best Regards,
Dustin Campbell
Developer Express, Inc

Rick Strahl [MVP] - 15 Aug 2004 02:41 GMT
Dustin,

Blush! That's a great tip that I had never heard of before... Ahem, too easy
to miss useful stuff in the framework.

Thanks!

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web

> > I remember looking at this a while back but I couldn't make this work
> > mainly because the example doesn't talk about the rest of the
[quoted text clipped - 14 lines]
> System.Type.Missing, System.Type.Missing, System.Type.Missing,
> System.Type.Missing);
Carlos J. Quintero [MVP] - 17 Aug 2004 09:48 GMT
> I remember looking at this a while back but I couldn't make this work mainly
> because the example doesn't talk about the rest of the parameters. It would
> be really helpful if you could post those examples with Option Strict off if
> VB is used <g>.

I am missing something here..., do you mean that you are using C# which does
not support missing parameters?

> But the main problem I had was trying to get a CommandBar object so I can
> add my Commands to it. The trick is to cast to CommandBarPopup and then use
[quoted text clipped - 3 lines]
>
> // *** Must retrieve the Command Bar Object directly via COM - not
provided on the object itself

> CommandBar commandBar = wwUtils.GetProperty(commandBarPopup,"CommandBar")
as CommandBar;

Again, I am also missing something here: if you cast to CommandBarPopup,
this class exposes a CommandBar property directly, no need for Reflection or
COM:

        CommandBarControl objMyCommandBarControl;
        CommandBar objCodeWindowCommandBar;
        CommandBarPopup objMyCommandBarPopup;

           objMyCommandBarControl =
objCodeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,System.T
ype.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing);

           objMyCommandBarPopup = (CommandBarPopup) objMyCommandBarControl;

           // Now you can use objMyCommandBarPopup.CommandBar ...

> I suppose I should also not use AddNamedCommand, but Commands.Add() but
> that's yet another excercise that I don't feel like wasting my time on
> today...

No, you should stick to Commands.AddNamedCommand. I was not aware of
Commands.Add() until you mentioned it and now I see it is a hidden method.

Signature

Carlos J. Quintero (Visual Developer - .NET MVP)

FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)

Rick Strahl [MVP] - 19 Aug 2004 04:22 GMT
> I am missing something here..., do you mean that you are using C# which does
> not support missing parameters?

Yeah, C# must pass all parameters unless you pass in System.Type.Missing as
Dustin
suggested which I didn't know about. Eventually I figured out by trial and
error
what to use for default parameters so it ended up working before Dustins
post,
but it took a while to figure out which values to use.

> Again, I am also missing something here: if you cast to CommandBarPopup,
> this class exposes a CommandBar property directly, no need for Reflection or
[quoted text clipped - 5 lines]
>
>             objMyCommandBarControl =

objCodeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,System.T
> ype.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing);
>
>             objMyCommandBarPopup = (CommandBarPopup) objMyCommandBarControl;

Ok, I'm a moron <g>... looking at the code again I didn't cast to
CommandBarPopup <g>.
I ended up with a CommandBarControl which of course doesn't have the
property and
off it I could get the CommandBar property via Reflection.

> > I suppose I should also not use AddNamedCommand, but Commands.Add() but
> > that's yet another excercise that I don't feel like wasting my time on
> > today...
>
> No, you should stick to Commands.AddNamedCommand. I was not aware of
> Commands.Add() until you mentioned it and now I see it is a hidden method.

Right. I ended up writing some generic Command Add routine that handles all
of this
in a single method to make it easy to port this stuff later to Whidbey
(since apparently
the whole CommandBar model has changed). The code checks to see if hte
command exists
before creating a new one which guarantees that you never end up getting
errros and
that things won't break if VS crashes and the cleanup code doesn't fire.
Using a non
named command bar and named commands works well now and it finally feels
like I'm doing
this the right way <g>...

+++ rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web

> > I remember looking at this a while back but I couldn't make this work
> mainly
[quoted text clipped - 6 lines]
> I am missing something here..., do you mean that you are using C# which does
> not support missing parameters?

Yeah, C# must pass all parameters unless you pass in System.Type.Missing as
Dustin
suggested which I didn't know about. Eventually I figured out by trial and
error
what to use for default parameters so it ended up working, but it took a
while.

> Again, I am also missing something here: if you cast to CommandBarPopup,
> this class exposes a CommandBar property directly, no need for Reflection or
[quoted text clipped - 5 lines]
>
>             objMyCommandBarControl =

objCodeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,System.T
> ype.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing);
>
>             objMyCommandBarPopup = (CommandBarPopup) objMyCommandBarControl;

Ok, I'm a moron <g>... looking at the code again I didn't cast to
CommandBarPopup <g>.
I ended up with a CommandBarControl which of course doesn't have the
property and
off it I could get the CommandBar property via Reflection.

> > I suppose I should also not use AddNamedCommand, but Commands.Add() but
> > that's yet another excercise that I don't feel like wasting my time on
> > today...
>
> No, you should stick to Commands.AddNamedCommand. I was not aware of
> Commands.Add() until you mentioned it and now I see it is a hidden method.

Right. I ended up writing some generic Command Add routine that handles all
of this
in a single method to make it easy to port this stuff later to Whidbey
(since apparently
the whole CommandBar model has changed). The code checks to see if hte
command exists
before creating a new one which guarantees that you never end up getting
errros and
that things won't break if VS crashes and the cleanup code doesn't fire.
Using a non
named command bar and named commands works well now and it finally feels
like I'm doing
this the right way <g>...

+++ rick ---
Carlos J. Quintero [MVP] - 19 Aug 2004 09:47 GMT
I have just updated the article to include System.Type.Missing values just
in case someone else ports it to C#. It can take some minutes to appear on
the MSDN site.

Signature

Carlos J. Quintero (Visual Developer - .NET MVP)

FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)

> Yeah, C# must pass all parameters unless you pass in System.Type.Missing
as Dustin
> suggested which I didn't know about. Eventually I figured out by trial and
error
> what to use for default parameters so it ended up working before Dustins
post,
> but it took a while to figure out which values to use.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.