Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to remove keyboard shortcut for CUT in experimental instance.

With the above code, able to remove keyboard shortcut.

But getting exception in this line command.Bindings = string.empty; like
ERROR
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))


What I have tried:

C#
DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
 Command command = dte.Commands.Item("Edit.Cut", -1);
 if (command.Bindings != null)
 {
   command.Bindings =string.Empty;
 }
Posted
Updated 28-Nov-23 6:24am
v2
Comments
Richard MacCutchan 28-Nov-23 4:01am    
Which line throws the exception?

Make use of a Try Catch block, this might tell you what and exactly where teh error is. You did state that your code errors at - 'command.Bindings =string.Empty;' but the catch might give you way more information -

C#
if (command.Bindings != null)
{
    try
    {
        //Clear your bindings for the Edit.Cut command...
        command.Bindings = string.Empty;

        //Try and save changes to the command...
        command.SaveChanges();
        
        //Show a message if successful...
        //You can remove this if not needed, might help to fetch each line...
        System.Diagnostics.Debug.WriteLine("Bindings cleared successfully.");
    }
    catch (System.Exception ex)
    {
        //Handle any exceptions that may occur during the operation...
        System.Diagnostics.Debug.WriteLine($"Error: {ex.Message}");
    }
}
 
Share this answer
 
Strange: according to the documentation[^], the Bindings property expects a SafeArray of objects[^]. Which would suggest that setting it to a single string shouldn't work.

But then the example on that same page (VB, as there's no C# example) shows the Bindings being set to a single string.

Perhaps that's a peculiarity of VB; try setting the property to an empty array instead:
C#
command.Bindings = new object[];
 
Share this answer
 
Comments
Meghana Mekala 30-Nov-23 0:12am    
Thank you it worked
Meghana Mekala 30-Nov-23 0:25am    
Now the Shortcut removed globally, can we restrict the shortcut disabling only to the solution explorer, i.e, if we click any particular file in solution explorer that shortcut shouldn't work
Richard Deeming 1-Dec-23 6:09am    
As far as I can see, that's not possible. You can assign a binding to use in a particular scope by adding a scope name at the start of the binding. But there doesn't seem to be a way to disable a global scope in a specific scope.

Instead, you would need to remove the global binding, and add bindings for each scope where you wanted the binding to apply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900