Home > Net >  Visual Studio 2022 view code button missing on solution explorer
Visual Studio 2022 view code button missing on solution explorer

Time:12-30

I'm doing a WinForms app (which I haven't done in a long time) and noticed that in VS 2022, the "view code" button (looks like <>) which used to be on the toolbar on the "Solution Explorer" window is missing. I know I can right-click on a form and reach it through the context menu, or hit F7, but is there any way to add it back? I can't seem to find any option for it.

CodePudding user response:

We can see “View Code” button in a solution which contain a .NET Framework winform project.

enter image description here

If we create a solution which contain a .NET 6 winform project the “View Code” button will disappear. We should right-click and reach it through the context menu or hit F7.

enter image description here

This issue has been submitted and is waiting to be resolved. enter image description here

You can download or clone the solution here:

Here is the step by step guide on creating this extension:

  1. Add new project and choose 'VSIX Project' and leave the default name.
    Please note: To have this project type, you need to install 'Visual Studio extension development' when installing/modifying VS installation.

  2. Add new item and choose 'Command' which is under 'Extensibility' group.

  3. Open 'VSIXProject1Package.cs' file and add the following line of code to the class:

    public const string UIContextGuid = "8B40D5E2-5626-42AE-99EF-3DD1EFF46E7B";
    

    It could be any GUID.

  4. Open 'VSIXProject1Package.vsct' file and add the following as child of <Symbols> node:

    <GuidSymbol name="UIContextGuid" value="{8B40D5E2-5626-42AE-99EF-3DD1EFF46E7B}" />
    

    The GUID here should be same as the GUID in previous step.

  5. Modify the attributes of the '' class to be like this:

    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [Guid(VSIXProject1Package.PackageGuidString)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [ProvideUIContextRule(VSIXProject1Package.UIContextGuid,
    name: "Supported Files",
    expression: "CSharp",
    termNames: new[] { "CSharp" },
    termValues: new[] { "HierSingleSelectionName:.cs$" })]
    public sealed class VSIXProject1Package : AsyncPackage
    
  6. Open 'Command1.cs' and modify the ServiceProvider property to this:

    private IServiceProvider ServiceProvider
    {
        get
        {
            return this.package;
        }
    }
    
  7. Replace the ExecuteCommand with this:

    private void Execute(object sender, EventArgs e)
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        var dte =  ServiceProvider.GetService(typeof(DTE)) as DTE; 
        ProjectItem item = dte.SelectedItems.Item(1)?.ProjectItem;
        if (item != null)
        {
            VsShellUtilities.OpenDocumentWithSpecificEditor(package,
                item.FileNames[1],
                Microsoft.VisualStudio.VSConstants
                    .VsEditorFactoryGuid.TextEditor_guid,
                Microsoft.VisualStudio.VSConstants.LOGVIEWID.Code_guid);
        }
    }
    
  8. Add the following method:

    private void MyQueryStatus(object sender, EventArgs e)
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        var button = (MenuCommand)sender;
        button.Visible = false;
        var dte = ServiceProvider.GetService(typeof(DTE)) as DTE;
        ProjectItem item = dte.SelectedItems.Item(1)?.ProjectItem;
        if (item != null)
        {
            string fileExtension = 
                Path.GetExtension(item.Name).ToLowerInvariant();
            string[] supportedFiles = new[] { ".cs"};
            button.Visible = supportedFiles.Contains(fileExtension);
        }
    }
    
  9. Open the '' file and replace the content of the Groups node with the following:

    <Group guid="guidVSIXProject1PackageCmdSet" 
    id="SolutionToolbarGroup" priority="0xF000">
       <Parent guid="guidSHLMainMenu" id="IDM_VS_TOOL_PROJWIN"/>
    </Group>
    
  10. Replace the child of the GuidSymbol which has name = guidVSIXProject1PackageCmdSet with the following:

    <IDSymbol name="Command1Id" value="0x0100" />
    <IDSymbol name="SolutionToolbarGroup" value="0x0190"/>
    
  11. Add the visibility constraint, right after the end of </Commands> tag:

    <VisibilityConstraints>
       <VisibilityItem guid="guidVSIXProject1PackageCmdSet"
       id="Command1Id"
       context="UIContextGuid" />
    </VisibilityConstraints>
    
  12. Modify the generated button tag to:

    <Button guid="guidVSIXProject1PackageCmdSet" id="Command1Id" priority="0x0100" type="Button">
       <Parent guid="guidVSIXProject1PackageCmdSet" id="SolutionToolbarGroup" />
       <Icon guid="guidImages" id="bmpPic1" />
       <CommandFlag>DefaultInvisible</CommandFlag>
       <CommandFlag>DynamicVisibility</CommandFlag>
       <Strings>
          <ButtonText>Invoke Command1</ButtonText>
       </Strings>
    </Button>
    
  13. Run the project and see the result.

More information:

  • Related