Visual Studio - Keyboard Shortcut to close Solution Explorer - visual-studio

CtrlAltL Opens Solution Explorer in Visual Sutdio.
But I'm unable to close it with the same keyboard shortcut.
As I need to do this often (to get more real estate on the screen), how do you close the Solution Explorer with a keyboard shortcut?

You can create the following command with Visual Commander (language: C#) and assign a shortcut to close Solution Explorer:
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
var serviceProvider = package as System.IServiceProvider;
var shell = (Microsoft.VisualStudio.Shell.Interop.IVsUIShell)serviceProvider.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SVsUIShell));
var SolutionExplorer = new System.Guid(Microsoft.VisualStudio.Shell.Interop.ToolWindowGuids80.SolutionExplorer);
Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame frame;
shell.FindToolWindow(0, ref SolutionExplorer, out frame);
frame.Hide();
}
}

Related

Is there a facility to select a section of code and save it a seperate file in Visual Studio 2019?

I work with large html files that I would like to fragment into seperate files. The process of doing this is quite tedious as it requires copying the code, creating a new file, pasting it in the new file, and then selecting a folder and a name to save it under.
Is there a built-in shortcut or extension in Visual Studio 2017 and higher for making this easier?
You can automate it in Visual Studio with my Visual Commander extension. Select the code and call the following command (C# language):
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
string currentFileName = DTE.ActiveDocument.FullName;
string newFileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(currentFileName), System.IO.Path.GetFileNameWithoutExtension(currentFileName) + "NewPart" + System.IO.Path.GetExtension(currentFileName));
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
System.IO.File.WriteAllText(newFileName, ts.Text);
}
}

Visual Studio Resources: Default Location for Add Existing File

In the Visual Studio Resources area, is there a way to set the default location in the file select dialog, when selecting "Add Existing File..."?
The default location, when clicking on that menu is:
C:\Program Files (x86)\Microsoft\Visual Studio\2017\Enterprise\Common7\IDE
True, it is a small thing, as selecting my Downloads folder or something else is easy, but I keep having to go somewhere else, usually my Downloads folder, and it would definitely save time to have the default location go somewhere else.
I know that the Open File common dialog, which Visual Studio uses, has the option in the initial structure to specify the initial (default) directory, so the only question is, if it is possible for the default directory to point somewhere else.
I am using Visual Studio 2017.
You can use my Visual Commander extension to monitor for the Resources.AddExistingFile command and set your preferred directory before its execution. See the following C# extension example:
public class E : VisualCommanderExt.IExtension
{
public void SetSite(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
events = DTE.Events;
commandEvents = events.get_CommandEvents(null, 0);
commands = DTE.Commands as EnvDTE80.Commands2;
commandEvents.BeforeExecute += OnBeforeExecute;
}
public void Close()
{
commandEvents.BeforeExecute -= OnBeforeExecute;
}
private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
string name = GetCommandName(Guid, ID);
if (name == "Resources.AddExistingFile")
System.IO.Directory.SetCurrentDirectory(#"c:\downloads");
}
private string GetCommandName(string Guid, int ID)
{
if (Guid == null)
return "null";
try
{
return commands.Item(Guid, ID).Name;
}
catch (System.Exception)
{
}
return "";
}
private EnvDTE.Events events;
private EnvDTE.CommandEvents commandEvents;
private EnvDTE80.Commands2 commands;
}

Can I hide the smiley in Visual Studio 2015 final?

In Visual Studio 2015 RC, there was that Feedback smiley in the upper right of the main window.
Unfortunately, it is still there in the final release of Visual Studio 2015:
I've searched all through the options and settings and found no way to hide this smiley.
My question:
Any option or other (Registry etc.) hack to remove the smiley?
Update 2015-12-01:
Yesterday Update 1 for Visual Studio was released.
While I still find no option to hide the smiley, they at least provided a less distracting icon for it:
Edit:
Visual Studio 2015 Update 1 changes the feedback icon to an understated black and white one so no more smiley! It's not mentioned in the release notes.
Original answer:
As in OPs answer, this icon is specified in this registry key:
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0_Config\MainWindowFrameControls{F66FBC48-9AE4-41DC-B1AF-0D64F0F54A07}
if you delete the key, Visual Studio recreates it, but if you invalidate the "Package" value of the registry key and restart Visual Studio then the smiley icon is gone:
However, when you install a Visual Studio update (eg SSDT, Resharper) the installer restores the package value, and the smiley is back. So I have created a registry file like this to run when the smiley reappears:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0_Config\MainWindowFrameControls\{F66FBC48-9AE4-41DC-B1AF-0D64F0F54A07}]
#="Feedback Button"
"Package"="{00000000-AA51-43B1-97EE-509A33B681F3}"
"DisplayName"="#1001"
"ViewFactory"="{060EAB95-139E-407D-BEDC-CC2B7A9B39D4}"
"ViewId"=dword:00000064
"Alignment"="TitleBarRight"
"Sort"=dword:00000064
"FullScreenAlignment"="MenuBarRight"
"FullScreenSort"=dword:00000064
This doesn't seem to affect startup time or stability, but there are no guarantees, registry changes are bad mmm, etc.
Thanks to Jehof's hint for Visual Studio 2013, I was able to resolve this:
Deleting the following Registry key actually helped.
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0_Config\MainWindowFrameControls\{F66FBC48-9AE4-41DC-B1AF-0D64F0F54A07}
(Please note the 14.0 instead of the 12.0 in the linked blog post)
After restarting Visual Studio, the Feedback button is now gone.
Update one day later
Suddenly the Feedback icon is here again. And the registry key is here again, too.
How on earth can this happen?
Seems the smiley resurrected from its grave. I'm really frightened now…
Following the advice in this thread, I modified also the registry key and it worked at first, but VS2015 keeps recreating the key to its original value after a while.
To remedy that, I added a VS2015 shortcut in Taskbar, and then Shift+Right Click to access 'Properties'. Replace the Target path of the shortcut pointing to devenv.exe with a own local c:\tools\vs.bat file.
Additionally, I changed Run to 'Minimized' in the shortcut properties. The vs.bat looks as follows:
#echo off
reg ADD HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0_Config\MainWindowFrameControls\{F66FBC48-9AE4-41DC-B1AF-0D64F0F54A07} /v Package /t REG_SZ /d {00000000-AA51-43B1-97EE-509A33B681F3} /f 2> nul
start /B devenv.exe
This ensures the key is overwritten every time I start VS2015 via Taskbar shourtcut. I found this to be reliably working for me.
If you ever start VS without this (e.g., on a VS restart), you may need to run the above twice.
I have removed value of Alignment key (just left it empty). The icon has gone until now but I didn't install no updates yet. Will keep posting.
UPDATE Restoring VS2015 not only restored this button but also changed the way it is configured: now my method doesn't work. Moreover, removing the registry key no longer helps. Wrote to MS through this helpful tool.
The Visual Commander extension allows creation of extensions which can hook into events. One provided example is hiding the feedback icon.
// References: System.Xaml
public class E : VisualCommanderExt.IExtension
{
public void SetSite(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = System.TimeSpan.FromMilliseconds(1000);
timer.Tick += OnTimer;
timer.Start();
}
public void Close()
{
timer.Stop();
}
private void OnTimer(System.Object o, System.EventArgs a)
{
try
{
if (HideSignIn() && HideFeedback())
;
}
catch (System.Exception e)
{
}
}
private bool HideSignIn()
{
System.Windows.FrameworkElement e =
FindElement(System.Windows.Application.Current.MainWindow,
"PART_MenuBarFrameControlContainer");
if (e != null)
{
e.Visibility = System.Windows.Visibility.Collapsed;
return true;
}
return false;
}
private bool HideFeedback()
{
System.Windows.FrameworkElement e =
FindElement(System.Windows.Application.Current.MainWindow,
"PART_TitleBarFrameControlContainer");
if (e != null)
{
System.Windows.DependencyObject o1 =
System.Windows.Media.VisualTreeHelper.GetChild(e, 0);
System.Windows.DependencyObject o2 =
System.Windows.Media.VisualTreeHelper.GetChild(o1, 0);
System.Windows.DependencyObject o3 =
System.Windows.Media.VisualTreeHelper.GetChild(o2, 0);
if (System.Windows.Media.VisualTreeHelper.GetChildrenCount(o3) == 3)
{
System.Windows.DependencyObject o4 =
System.Windows.Media.VisualTreeHelper.GetChild(o3, 1);
(o4 as System.Windows.FrameworkElement).Visibility =
System.Windows.Visibility.Collapsed;
return true;
}
}
return false;
}
private System.Windows.FrameworkElement FindElement(System.Windows.Media.Visual v, string name)
{
if (v == null)
return null;
for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(v); ++i)
{
System.Windows.Media.Visual child =
System.Windows.Media.VisualTreeHelper.GetChild(v, i) as
System.Windows.Media.Visual;
if (child != null)
{
System.Windows.FrameworkElement e =
child as System.Windows.FrameworkElement;
if (e != null && e.Name == name)
return e;
}
System.Windows.FrameworkElement result = FindElement(child, name);
if (result != null)
return result;
}
return null;
}
private System.Windows.Threading.DispatcherTimer timer;
}
Although, comparing this example with the hide publish button example it seems the OnTimer function should stop the timer once the icons have been hidden.
private void OnTimer(System.Object o, System.EventArgs a)
{
try
{
if (HideSignIn() && HideFeedback())
timer.Stop();
}
catch (System.Exception e)
{
}
}
This line of PowerShell removes the icon until an update is installed:
Set-ItemProperty -Path "HKCU:\Software\Microsoft\VisualStudio\14.0_Config\MainWindowFrameControls\{F66FBC48-9AE4-41DC-B1AF-0D64F0F54A07}" Package "{00000000-AA51-43B1-97EE-509A33B681F3}"

Visual studio 2010: limiting the number of editor tabs

Visual studio doesn't appear to limit the number of opened editor tabs.
I'm using ReSharper and at a certain number of opened editor tabs things get really slow. So I have to keep track of opened tabs and periodically close old ones.
It would be cool if I could set a limit so that it would close old tabs when the limit is reached.
Is there a setting in VS / ReSharper or any VS addons that can help to achieve this?
I'm trying to solve this with a primitive addin at the moment. Seems to be working fine. Still testing it.
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_applicationObject.Events.WindowEvents.WindowCreated +=
window =>
{
if (window.Document != null)
{
documentWindows.AddFirst(window);
if(documentWindows.Count > 7)
{
Window lastWindow = documentWindows.Last.Value;
documentWindows.Remove(lastWindow);
lastWindow.Close(vsSaveChanges.vsSaveChangesYes);
}
}
};
_applicationObject.Events.WindowEvents.WindowClosing +=
window =>
{
if(window.Document != null)
{
documentWindows.Remove(window);
}
};
}

Hide Properties/Toolbox Pane when not in Resource View?

Every time I view a form or dialog in Visual Studio (2005) the Properties and Toolbox panes show up on the right side of my screen. That's good to have because they are useful for manipulating dialogs.
However once I switch back to source code these panes just get in the way... is there a way to get them to go away automatically?
I've done something recently in VS2010 using a macro that shows and hides the Tools panel when switching back and forth from code to design view in asp.net MVC3 views. It could be easily adapted to do the same for your situation I think.
This goes in the EnvironmentEvents class file in in the VS Macro IDE after the pre-generated content.
<System.ContextStaticAttribute()> Public WithEvents CommandEvents As EnvDTE.CommandEvents
Public Sub DTEEvents_OnMacrosRuntimeReset() Handles _
DTEEvents.OnMacrosRuntimeReset
CommandEvents = DTE.Events.CommandEvents
End Sub
Private Sub DTEEvents_OnStartupComplete() Handles _
DTEEvents.OnStartupComplete
CommandEvents = DTE.Events.CommandEvents
End Sub
Public Sub CommandEvents_AfterExecute( _
ByVal Guid As String, _
ByVal ID As Integer, _
ByVal CustomIn As Object, _
ByVal CustomOut As Object) _
Handles CommandEvents.AfterExecute
If DTE.Commands.Item(Guid, ID).Name = "View.ViewDesigner" Then
DTE.ExecuteCommand("View.Toolbox")
End If
If DTE.Commands.Item(Guid, ID).Name = "View.ViewMarkup" Then
DTE.Windows.Item(Constants.vsWindowKindToolbox).Close()
End If
End Sub
It could probably be better optimized using the guids of the event rather than the if statements. It works when you use the hot keys for switching views as well as the view menu, but not the context menu.
for vs2015:
Menu > Tools > Extensions and Updates
install "Visual Commander". (Now you have New Menu called "VCmd")
Menu > "VCmd" > Extensions ... (You will see an Extensions Pane At Right)
Press Add Button at the Extensions Pane. (New tab Wİndow will open.)
write a name for extention.
select language as C#.
paste the code below:
Press Save. Then Press Compile. Then Press Install
using EnvDTE;
using EnvDTE80;
public class E : VisualCommanderExt.IExtension
{
private EnvDTE80.DTE2 DTE;
private EnvDTE.WindowEvents windowEvents;
public void SetSite(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) {
this.DTE = DTE;
DTE.Events.WindowEvents.WindowActivated += OnWindowActivated;
}
public void Close() {
// i read somewhere this has to be done on close.
// but it gives gives me error on every IDE close. so i commented it .
//DTE.Events.WindowEvents.WindowActivated -= OnWindowActivated;
}
private void OnWindowActivated(Window gotFocus, Window lostFocus) {
HidePropertiesWindowInCodeOrTextView(gotFocus );
}
public void HidePropertiesWindowInCodeOrTextView(Window gotFocus ) {
if (gotFocus.Document == null) return;
var pwin = DTE.Windows.Item(Constants.vsWindowKindProperties);
pwin.AutoHides = !gotFocus.Caption.EndsWith(" [Design]") ;
}
}
Rather than give up the space on the right side of the screen, I dragged my properties and toolbox panes over to the left-side frame that hosts the solution explorer and class view, etc. I'd rather have one multi-purpose box on one side of the screen than to have the code surrounded. If you need them both, you can put the toolbox in the solution explorer pane, then stack the properties pane beneath the solution explorer, which keeps a few properties in view at all times along with the toolbox.
I know it's not quite the answer you were looking for, but it's a different way of keeping that screen real estate available for code without messing with auto-hide (I find auto-hide to be really an annoyance more than a help.)
For vs2019:
I improve bh_earth0's solution. Now it saves visibility states of Properties and ToolBox when you jump to code. And when design tab is activated it loads previous state of panes.
Menu > Extensions > Manage Extensions
Find and install "Visual Commander". (Now you have New Menu called "VCmd")
Menu > "VCmd" > Extensions ... (You will see an Extensions Pane At Right)
Press Add Button at the Extensions Pane. (New tab Wİndow will open.)
write a name for extention (e.g. AutoHide).
select language as C#.
copy and paste the code below:
Press Save. Then Press Compile. Then Press Install
Restart the Visual Studio and enjoy :-)
using EnvDTE;
using EnvDTE80;
public class E : VisualCommanderExt.IExtension
{
private EnvDTE80.DTE2 DTE;
private EnvDTE.WindowEvents windowEvents;
private bool bPropWinVisible = false;
private bool bToolWinVisible = false;
public void SetSite(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) {
this.DTE = DTE;
DTE.Events.WindowEvents.WindowActivated += OnWindowActivated;
}
public void Close() {
}
private void OnWindowActivated(Window gotFocus, Window lostFocus) {
if (gotFocus.Document == null) return;
if (lostFocus.Document == null) return;
var pwin = DTE.Windows.Item(Constants.vsWindowKindProperties);
var twin = DTE.Windows.Item(Constants.vsWindowKindToolbox);
if(gotFocus.Caption.EndsWith(".cs [Design]") && lostFocus.Caption.EndsWith(".cs") ) {
pwin.AutoHides = bPropWinVisible;
twin.AutoHides = bToolWinVisible;
}
else if(gotFocus.Caption.EndsWith(".cs") && lostFocus.Caption.EndsWith(".cs [Design]")) {
bPropWinVisible = pwin.AutoHides;
bToolWinVisible = twin.AutoHides;
pwin.AutoHides = true;
twin.AutoHides = true;
}
}
}
If you click the 'pin' icon on those tool windows, you can toggle whether the windows stay open all the time, or only when the mouse is near them. Of course, sometimes my mouse strays over in that direction and they pop out when I don't want them to, but such is life...

Resources