Autocomplete Method Brackets - c#

Using: Visual Studio Pro 2013
Previous research: [1], [2], [3]
I'm used to working in Java with Eclipse.
My usual flow is:
object. CTRL+SPACE + ENTER
which autocompletes the method and places the correct curly brackets & method inputs in there:
object.myMethod();
or
object.myMethod(input1,input2);
I'm trying to get the same behaviour with VS in C#. I can get the method, but it doesn't want to include the final brackets for some reason.
I get:
object.myMethod
Is there a way to enable this?

When doing autocomplete, instead of just hitting ENTER, you can instead press the key combination "Shift + (" and it will autocomplete and add () all in the one.

You can turn on Automatic Brace Completion at: Tools->Options->Text Editor->C#->General .
Tools->Options->Text Editor->C#->IntelliSense is for auto-completion of variables and keywords.

With the current version of Visual Studio 2019 (Version 16.10.x), press the Tab key twice to insert arguments (also brackets). This is an experimental feature and needs to be enabled in Options -> Text Editor -> C# -> IntelliSense -> Tab twice to insert arguments (experimental).

Even though the question was for Visual Studio 2013, on Visual Studio 2017 you can install ReSharper plugin by JetBrains that among other things, it automatically add brackets to methods when auto-completing.

Related

Visual Studio Auto Suggesting Twice

When I type a variable name it begins to autofill what the variable name is... twice. Does anyone have an idea what setting could be causing this? I am using Resharper Ultimate. Here is what it looks like:
There are two auto suggetion menus. When I use the arrow keys to move down, only the one in the back enumerates the options.
You have to go into the Visual Studio C#
options (Tools -> Options -> Text Editor -> C#) and disable statement
completion (uncheck "Auto list members" and "Parameter Information").

Visual Studio 2017 - Disable auto-completion on Enter key press

I'm using Visual Studio 2017 to edit the scripts for Unity 3D Project.
In a script, I want to create a new function "Move", but I get a problem with autocomplete.
If I write "public void Move"
I get a a proposal "OnAnimatorMove", but I don't need this function!
Now, after "Move" I enter a space character " ", but my function name is then automatically replaced by "OnAnimatorMove" and the "{","}" are automatically inserted.
Same happens, if I try to insert "(" character after "Move".
How can I prevent it?
U can click escape to disable the autocompletion or u can disble it all together by going to
VS > Tools > Options > Tools for unity > and under 'code edition' disable Unity massage code completion.
after that restart
I can solve it, going to Edit->Preferences->External Tools, and in External Script Editor, select Visual Studio 2017 (Enterprise)

Whats the equivalent of TAB +TAB in Visual Studio Code VSCode

In Visual Studio you can use Tab + Tab to automatically create a foreach (or other) structure.
Does VSCode have something similar?
Yes, VSCode does support snippets. They appear in the completion list, and you can insert them by pressing Tab or just Enter. Alternatively, you can use the Insert Snippet command found in the command palette.
The snippet shown in the .gif above is provided by the C# extension.

Collapse all #regions only(!) in C# (Visual Studio)

There's a number of keyboard shortcuts and menu commands to automatically expand or collapse all foldables in the current document. Ctrl+M,
Ctrl+L toggles all foldables recursively, from the top namespace down to the inner methods and comments. Basically everything that has a [+] icon in the left margin. Ctrl+M, Ctrl+O does it bit less, but it still touches methods and comments inside the class.
I'm looking for a function that specifically only regards #region foldables and nothing else. Not namespaces, classes, comments, or methods. I really only want to fold those areas that are explicitly marked as such with the #region keyword.
Is there any hidden shortcut or an extension for that?
I'd prefer a keyboard solution, for Visual Studio 2015 (RC for now).
In case somebody asks why: I want to get an overview of the file, but immediately see all the details when I decide to look inside one region.
Update:
Solution in 2020
As this question has received many answers, some of them useless, others outdated, the working solution for Visual Studio 2019 can be found in this underappreciated answer here: https://stackoverflow.com/a/58999093/143684 Link to VS 2022 version in the comments.
in Visual Studio 2017 I have to activate 'Collapse #regions when collapsing to definitions' in
Tools -> Options -> Text Editor -> C# -> Advanced
explicitly to collapse all when pressing Ctrl+M+O
Ctrl+M+O will collapse all.
Ctrl+M+L will expand all. (in VS 2013 - Toggle All outlining)
Ctrl+M+P will expand all and disable outlining.
Ctrl+M+M will collapse/expand the current section.
These options are also in the context menu under Outlining.
Right click in editor -> Outlining to find all options. (After disabling outlining, use same steps to enable outlinging.)
To collapse regions : Tools > Options > Text Editors > C# > Advanced >
Check "Collapse #regions when collapsing to definitions"
Update for VS2022: https://github.com/mdmower/CollapseRegionExtension/releases/tag/v1.5 (thanks #ygoe)
I know this is an old question, but here's an update for visual studio 2019:
The Quick Tasks solution from #ygoe's answer does not work for Visual Studio 2019, because that version changed the way the Quick Task bar searches. Plus it was never the preferred solution to the original question, since you needed to type a quick action instead of having a keyboard shortcut.
I found a different solution on the marketplace which does exactly the collapsing and expanding of all regions in the currently opened file with keyboard shortcuts: Collapse Region
From the extension page:
Default key bindings are (Ctrl+R, Ctrl+Num+) and (Ctrl+R, Ctrl+Num-).
Can be changed in Tools->Options->Environment->Keyboard.
The Visual Studio extension Productivity Power Tools 2015 from Microsoft has a feature called Quick Launch Tasks that adds new commands to the Quick Launch menu. One of them is CollapseRegions and it does exactly that.
The opposite command is ExpandRegions and it expands all regions for quick browsing of the entire file. These commands can be used pretty quickly by pressing the CtrlQ hotkey and typing Coll resp. Exp, then pressing Enter (supposed you don't have other commands with the same prefix).
tick the Checkmark in Settings -> TextEditor -> C# -> Advanced -> (section Outlining) Collapse #regions when collapsing definitions.
then right click in the editor > outlining > collapse to definitions
I dont know if this is new, but there is a setting for the c# text edior:
Settings -> TextEditor -> C# -> Advanced -> Collapse #regions when collapsing definitions.
When its set CtrlM CtrlO will collapse the #regions.
Looks like the closest thing is Ctrl+M, Ctrl+S
Which will collapse the current region you are in, while Ctrl+M, Ctrl+E will expand the current region you are in.
List of default keyboard shortcuts:
https://msdn.microsoft.com/en-us/library/da5kh0wa(v=vs.140).aspx
I recommend using the free Visual Studio extension "Menees VS Tools Extension for Visual Studio." Just search for it in Tools --> Extensions and Updates in Visual Studio. Once installed you just have to hit Ctrl+M+K and just the regions will be collapsed. Here is their website https://marketplace.visualstudio.com/items?itemName=BillMenees.MeneesVSTools2013
To edit the settings look under Tools --> Options --> Menees VS Tools --> General.
For Visual Studio 2022, here is the tool that works well:
https://marketplace.visualstudio.com/items?itemName=EngineDesigns.CollapseAllRegions
CTRL-M CTRL-R
to collapse all #region groups.
Not sure about previous Visual Studio versions, but in VS 2022 we have new shortcut
CTRL + M + M
It will collapse any part of code like namespace, class, region etc.,
Steps:
Go to #region piece of code.
Select #region or click on #region anywhere in that word.
From Keyboard click CTRL + M + M
If you want for entire namespace to collapse, you can do same steps by selecting or clicking namespace.
Select All via Ctrl A and then Ctrl M M
i.e. Press M twice while holding down Ctrl

What keyboard shortcut is there to organize C# usings in Visual Studio?

Is there a way to organize C# usings (remove and sort, in separate or together) via a shortcut in Visual Studio for one or more files of a project?
I know that this can be done via the menu for one file by selecting
Edit > IntelliSense > Organize Usings > Remove and Sort
but I want to do this much faster.
I am using Visual Studio 2013 Express for C# development (wondering how this can be done in older & other versions too though). Thank you in advance.
Since VS2017, it's a builtin shortcut. Simply press Ctrl + R, Ctrl + G. Credit goes to Emanuel Ve, who mentioned this first in the comments; I'm putting the advice into an answer for greater visibility.
Go to Tools => Options => Environment => Keyboard
Enter the key combination you want to use (click the Press shortcut keys: textbox, press your key combo as you would execute it while editing)
Then type "usings" in the Show commands containing: textbox
And now you can assign whichever version of these remove/sort usings commands that you like to the key combination.
This works for pretty much every command in Visual Studio, and from any addons you have installed. Just type a word involved in the command name to find the command. Assign away!
In addition to #Will's answer,
Productivity Power Tools
is a great addon that has all this - and lots lots more!

Categories

Resources