locked
CommandBarControl.ID is zero in vs2010 RRS feed

  • Question

  • I'm migrating my addin from VS2005 to VS2010, and find a regression blocking me. Here's demo VB.net code:

        Sub Test()
            Dim cmdBarCtl 'As CommandBarControl
            For Each cmdBarCtl In DTE.CommandBars("Code Window").Controls
                Trace.WriteLine(cmdBarCtl.Caption & ", " & cmdBarCtl.Id)
            Next
        End Sub

    In VS2005, it prints all captions and coresponding Id. But In VS2010, captions are correct, but Id of all controls are 0!

    Does anyone know the reason? Or is it a VS2010 bug?

    Tuesday, March 13, 2012 1:06 PM

Answers

  • Is the item you are looking for on the top level of the Code Window menu or nested?  You can use the indexer of the controls collection with the string name (English name will work, even in localized builds) as long as the search doesn't require recursion into nested menus (the indexer does not recurse, only FindControl does that).

    Ryan

    • Marked as answer by skyshore2001 Wednesday, March 14, 2012 2:43 AM
    Wednesday, March 14, 2012 12:35 AM

All replies

  • It does appear that by default it will be 0 in 2010, though it was not guaranteed unique even pre-2010.  What were you using it for exactly?

    Ryan

    Tuesday, March 13, 2012 5:23 PM
  • Thanks for your reply. I need the Id to locate a VS button, then put my button after it, for example:

    Dim contextPopupMenu As CommandBar = DTE.CommandBars("Code Window") 
    Dim btnSetNextStmt As CommandBarControl = contextPopupMenu.FindControl(Id:=1813) ' Set Next Statement 
    If Not btnSetNextStmt Is Nothing Then pos = btnSetNextStmt.Index + 1 
    myCmd.AddControl(contextPopupMenu, pos) 

    FindControl method can only find by Type, Id or Tag. Tag is empty for almost all standard VS buttons, and type is also useless. Maybe It works to scan all controls and compare the caption, but I don't think the caption is reliable. Do you have any ideas?

    • Edited by skyshore2001 Wednesday, March 14, 2012 2:31 AM format
    Wednesday, March 14, 2012 12:20 AM
  • Is the item you are looking for on the top level of the Code Window menu or nested?  You can use the indexer of the controls collection with the string name (English name will work, even in localized builds) as long as the search doesn't require recursion into nested menus (the indexer does not recurse, only FindControl does that).

    Ryan

    • Marked as answer by skyshore2001 Wednesday, March 14, 2012 2:43 AM
    Wednesday, March 14, 2012 12:35 AM
  • Great! I tried it in macro window:

    cmdBar As CommandBar = DTE.CommandBars("Code Window") 
    Trace.WriteLine(cmdBar.Controls("Set N&ext Statement").Caption) 
    Trace.WriteLine(cmdBar.Controls("Set Next Statement").Caption) 

    Both work! Now I use the one without "&" in my code. I wish it is available for other transalations.

    Thank you Ryan!

    Skyshore.

    • Edited by skyshore2001 Wednesday, March 14, 2012 2:29 AM format
    Wednesday, March 14, 2012 2:25 AM