C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,897 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am trying to remove keyboard shortcut for CUT and Delete in experimental instance for Solution Explorer. The below code is working globally (For entire visual studio its restricting). I need to restrict it to Solution explorer, when we click on any specific file, Cut and Delete keyboard shortcuts should not work.
This is my code snippet that I have tried:
DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
Command command = dte.Commands.Item("Edit.Cut", -1);
if (command.Bindings != null)
{
command.Bindings = new object[];
}
Deleting item in SolutionExplore using keyboard or mouse, it shuld be need the SolutionExolore window is active.
So, this can be achieved by canceling SolutionExolore only if it is active.
using Microsoft.VisualStudio.Shell;
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Task = System.Threading.Tasks.Task;
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.EmptySolution, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasSingleProject, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasMultipleProjects, PackageAutoLoadFlags.BackgroundLoad)]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[Guid(VSIXProject1Package.PackageGuidString)]
public sealed class VSIXProject1Package : AsyncPackage
{
public const string PackageGuidString = "f41ec426-229f-4e09-8e88-1fa351c64561";
#region Package Members
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
dte = (EnvDTE80.DTE2)await GetServiceAsync(typeof(EnvDTE.DTE));
if (dte == null) return;
cev = dte.Events.CommandEvents;
cev.BeforeExecute += Cev_BeforeExecute;
var cmdDelete = dte.Commands.Item("Edit.Delete");
edit_delete = new GuidID(cmdDelete.Guid, cmdDelete.ID);
var cmdCut = dte.Commands.Item("Edit.Cut");
edit_cut = new GuidID(cmdCut.Guid, cmdCut.ID);
}
EnvDTE80.DTE2 dte;
EnvDTE.CommandEvents cev;
GuidID edit_delete;
GuidID edit_cut;
private void Cev_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
ThreadHelper.ThrowIfNotOnUIThread();
var cmd = new GuidID(Guid, ID);
if (object.Equals(cmd, edit_delete) || object.Equals(cmd, edit_cut))
{
var sol = dte.ToolWindows.SolutionExplorer;
var solutionWindow = dte.ToolWindows.SolutionExplorer.Parent;
if (solutionWindow == dte.ActiveWindow)
{
CancelDefault = true;
}
}
}
struct GuidID
{
public string Guid;
public int ID;
public GuidID(string guid, int id)
{
Guid = guid;
ID = id;
}
}
#endregion
}