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.

- 120,393
- 18
- 203
- 398

- 1,813
- 3
- 25
- 31
-
1I'm not aware of any settings for that. You may want to create a VS extension that adds the command to Solution explorer, then set the visibility rule for .cs files. Then in the action of the command, find the selected item and open it with code editor. – Reza Aghaei Dec 30 '21 at 00:56
3 Answers
We can see “View Code” button in a solution which contain a .NET Framework winform project.
If we create a solution which contains 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.
This issue has been submitted and is waiting to be resolved. https://developercommunity.visualstudio.com/t/Solution-Explorers-tool-bar-does-not-in/1528389?space=8&q=vs+2022+Missing+%3C%3E+%22View+Code%22+option+in+Solution+Explorer&stateGroup=active

- 2,076
- 1
- 3
- 10
-
So it's a known issue with .NET 6 projects. That would explain it since I am using .NET 6 for this. Guess it'll just reappear at some point after an update. :) Thanks. – Starfleet Security Dec 30 '21 at 14:57
Possibly a bug, as mentioned in the other answer, meanwhile, as a workaround you may want to create a VS extension that adds the command to Solution explorer, then set the visibility rule for .cs files. Then in the action of the command, find the selected item and open it with code editor.
Add a button to solution explorer to open .cs files in code editor
This extension adds a toolbar button to the Solution Explore which will be visible, only if you choose a C# file, and when you click on it, it opens the file in code editor:
You can download or clone the solution here:
Here is the step by step guide on creating this extension:
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.Add new item and choose 'Command' which is under 'Extensibility' group.
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.
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.
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
Open 'Command1.cs' and modify the
ServiceProvider
property to this:private IServiceProvider ServiceProvider { get { return this.package; } }
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); } }
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); } }
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>
Replace the child of the
GuidSymbol
which has name =guidVSIXProject1PackageCmdSet
with the following:<IDSymbol name="Command1Id" value="0x0100" /> <IDSymbol name="SolutionToolbarGroup" value="0x0190"/>
Add the visibility constraint, right after the end of
</Commands>
tag:<VisibilityConstraints> <VisibilityItem guid="guidVSIXProject1PackageCmdSet" id="Command1Id" context="UIContextGuid" /> </VisibilityConstraints>
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>
Run the project and see the result.
More information:

- 120,393
- 18
- 203
- 398
-
-
1@Poul Bak Step 5 sets the visibility rule, you can set several file types. – Reza Aghaei Dec 31 '21 at 21:12
This is a far from perfect workaround. But:
You can add the button to your normal toolbar:
Right click the toolbar area, 'Customize...', 'Commands', 'Toolbar', select 'Text editor' (or what you want), 'Add command...', 'View', 'View code' and 'Ok'.
Now you have the button on your toolbar.
However, it's far from perfect, because you must have the Form/Razor page open in the editor, or the button will be disabled.
I don't think Microsoft will ever add the button back in Solution Explorer - it's after all removed on purpose, but 'mouse men' like us surely miss it.

- 10,450
- 5
- 32
- 57