Visual studio 2010: limiting the number of editor tabs - visual-studio

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);
}
};
}

Related

Get active document when changed in Visual Studio extension

So I'm currently writing an extension for VS using their extensibility API, and I've run into a bit of a snag. I need some kind of event (or other solution) that triggers when you tab between files, and when that happens I need to get the path to the new active document.
The problem is everything in the API that I have tried either fires at the wrong time, or when it is called, the current active document hasn't been updated yet so when I get the path it's still the file I just tabbed out of.
I've tried implementing the IVsRunningDocTableEvents interface, I've tried using the Events.WindowEvents.WindowActivated event, nothing so far has worked correctly.
EditorClassifier1Provider.myEnvDTE.DTE.Events.WindowEvents.WindowActivated += WindowEvents_WindowActivated;
class RdtEvents : IVsRunningDocTableEvents
{
public RdtEvents()
{
ThreadHelper.ThrowIfNotOnUIThread();
var rdt = (IVsRunningDocumentTable)Package.GetGlobalService(typeof(IVsRunningDocumentTable));
rdt.AdviseRunningDocTableEvents(this, out _);
}
int IVsRunningDocTableEvents.OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
{
OpenConnectionCommand.docView = VS.Documents.GetActiveDocumentViewAsync().Result;
Message.Send(null, null);
OpenConnectionCommand.docView.Document.DirtyStateChanged += (s,e) => Message.Send();
OpenConnectionCommand.docView.TextView.Selection.SelectionChanged += (s,e) => Message.Send();
return Microsoft.VisualStudio.VSConstants.S_OK;
}
//...etc

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}"

In a Visual Studio Add-In, how to disable menu items during a build?

When you write a VS addin that proffers menu items to VS, it calls your QueryStatus implementation to check whether your menu items should be visible, enabled, etc.
My menu items are not appropriate to run during certain situations, e.g. when a build is happening. How do I detect whether there's a build running?
You're looking for the VsShellUtilities.IsSolutionBuilding method. An example of using it can be found the Managed Package Framework for Visual Studio 2010 (MPFProj10) in the ProjectNode class:
protected internal virtual bool IsCurrentStateASuppressCommandsMode()
{
if (VsShellUtilities.IsSolutionBuilding(this.Site))
{
return true;
}
DBGMODE dbgMode = VsShellUtilities.GetDebugMode(this.Site) & ~DBGMODE.DBGMODE_EncMask;
if (dbgMode == DBGMODE.DBGMODE_Run || dbgMode == DBGMODE.DBGMODE_Break)
{
return true;
}
return false;
}

Add button to the Visual Studio unloaded project context menu (Can't find the CommandBar its name)

NuGet package restore does not play well with the PostSharp package, this is of great annoyance to me and manually editing the csproj files is quickly becoming old.
I want to make a small Visual Studio AddIn that fixes the project for me by adding an extra button to the unloaded project context menu.
The problem I am facing sounds simple but haunts me: I somehow can't locate the CommandBar for the unloaded project context menu.
Thus, summarizing the question: What is the CommandBar name for the unloaded project context menu.
Many thanks!
Apparantly: "Stub Project". Found in the list at: CommandBar names
To sum up the OnConnection method to register this:
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName = "Tools";
//Place the command on the tools menu.
//Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
//Find the Tools command bar on the MenuBar command bar:
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBars cmdBars = (CommandBars)(_applicationObject.CommandBars);
CommandBar vsBarProject = cmdBars["Stub Project"];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
// just make sure you also update the QueryStatus/Exec method to include the new command names.
try
{
//Add a command to the Commands collection:
Command command = commands.AddNamedCommand2(_addInInstance, "FixNuGetPostSharp", "Fix NuGet PostSharp", "Executes the command for VsextensionTest", true, 0, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
//Add a control for the command to the tools menu:
if ((command != null) && (toolsPopup != null))
{
command.AddControl(vsBarProject);
}
}
catch (System.ArgumentException)
{
//If we are here, then the exception is probably because a command with that name
// already exists. If so there is no need to recreate the command and we can
// safely ignore the exception.
}
}
}

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