When editing really long code blocks (which should definitely be refactored anyway, but that's beyond the scope of this question), I often long for the ability to collapse statement blocks like one can collapse function blocks. That is to say, it would be great if the minus icon appeared on the code outline for everything enclosed in braces. It seems to appear for functions, classes, regions, namespaces, usings, but not for conditional or iterative blocks. It would be fantastic if I could collapse things like ifs, switches, foreaches, that kind of thing!
Googling into that a bit, I discovered that apparently C++ outlining in VS allows this but C# outlining in VS does not. I don't really get why. Even notepad++ will so these collapses if I select the C# formatting, so I don't get why Visual Studio doesn't.
Does anyone know of a VS2008 add-in that will enable this behavior? Or some sort of hidden setting for it?
Edited to add: inserting regions is of course an option and it did already occur to me, but quite frankly, I shouldn't have to wrap things in a region that are already wrapped in braces... if I was going to edit the existing code, I would just refactor it to have better separation of concern anyway. ("wrapping" with new methods instead of regions ;)
Starting with Visual Studio 2017, statement collapsing is built-in.
There are several extensions that perform this task for pre-2017 versions of VS, starting with VS 2010 version:
C# outline
C# outline
2012 (#MSDN)
C# outline
2013 (#MSDN)
C# outline
2015 (#MSDN)
Visual Basic and C# Outliner
The last extension supports only VS 2015 and VS 2017, but it's the most powerful one.
It supports syntax coloring inside collapsed blocks, it is more fault-tolerant and optimized.
If the extension doesn't seem to install after you used a browser to download it, try using the built-in Visual Studio extension manager.
I'm not aware of add-ins, but you mentioned regions and I see nothing wrong with doing something like this...
foreach (Item i in Items)
{
#region something big happening here
...
#endregion
#region something big happening here too
...
#endregion
#region something big happening here also
...
#endregion
}
EDIT: In response to the question's EDIT: You're right, sticking a bunch of regions everywhere isn't ideal and refactoring is probably the way to go. But it seems that you're looking for something magical that will "organize" the code for you, and I don't think that exists.
You can collapse specific blocks of text within visual studio, but you have to turn off automatic outlining.
Right click in your code window and select (Outlining | Stop Outlining)
Then, select some text, right click and select (Outlining | Hide Selection)
When you turn on automatic outlining again, your custom "Regions" will no longer collapse.
Visual Studio 2008 supports regions inside of functions as long as you keep them in the same code hierarchical level
#region Won't work
for(int i = 0; i<Count; i++)
{
//do something
#endregion
}
for(int i=0; i<Count; i++)
{
#region Works fine
//do lots of stuff
#endregion
}
Let me say something different: press(ctrl+m,ctrl+h) or in edit>outlining>hide selection
its so useful.
This feature has been added to Visual Studio 2010's C# editor. I can't find the source verifying it was actually put in, but I remember seeing it on one of the Dev 10 team member blogs talking about changes since Beta 1 or something. As a consolation, here's one Microsoft comment suggesting they wanted to add it.
I will add here that in VS 2010 Microsoft has added WPF adorner capabilities using Managed Extensibility Framework (MEF), this will allow us to extend the source code editor to organize them in a much better way to make it more readable and accessible.
For instance the Summary Comments visualizer that Scott Gu demoed at PDC 2008.
So look forward to a better tomorrow for developers :)
Coderush will outline all code blocks for you. Not sure if it allows you to expand/collapse the blocks, but outlining is the next best thing. I use resharper instead of coderush which as far as I know doesn't provide block collapsing either :(
I have found this for Visual Studio 2013 and found it very helpful. It works even if you put simple braces around your code with { ..... }
After sharing I found somebody else also mentioned this link. My vote is for this tool also.
C# Outlining Tool for Visual Studio 2013
In VS2017 you can highlight a section of code, right-click, Outlining > Hide selection. This will collapse the code and provide a toggle to the section highlighted.
In Visual Studio 2019, if you want to collapse braces in catch & finally, collapse switch, case, default, collapse multiple lines of comments, etc.
Try to use C# outline 2019
# region ,#endregion is the smart option.
Related
For WPF/C# where are all the default code snippets for things like Dependency Properties (wpfdp) or Routed Commands (wpfrc). I couldn't find these snippets listed anywhere in Visual Studio.
For a dependency property, use propdp.
You can find out all available snippets easily if you're using visual studio 2017:
Type a letter, for example a.
Click on the last icon at the bottom of the Intellisense Window.
This displays all available snippets.
Personally, I think it's wise to learn c# if you're trying to learn wpf. WPF has a notoriously tricky learning curve and you will want all the help you can get in the process. C# developers are much more active blogging and posting samples than VB. It's way easier to find C# help. Certainly in my area of the world it's also way easier to find work using c# than vb.
There's a snippet manager.
Press Ctrl+K, Ctrl+B and you get a window up.
They are organised by language.
You can write your own fairly easily, although the support in standard vs is pretty limited.
Some people particularly like resharper for it's macro snippet support.
Roslyn supports refactoring and some prefer that route for complicated snippet-like functionality.
But anyhow, take a look in there.
Under c# do you not see the snippets?
If not this could be an install issue.
Maybe you installed just for VB initially if you're moving to c#.
And as Xanimax said. Set routed command aside for now. Learn MVVM and pick one of the nuget packages intended to give you a more convenient way to define commands. I like mvvmlight.
The keyboard commands are propdp and propa. If you need to find them otherwise type in CTRL - X and the Insert Snippet dialog will pop up and you will be given the list of keyboard snippets to insert via folders:
The two you are interested in are in the NetFX30 folder and not the Visual C# folder:
I'm sure this is a foolish question, but I can't seem to find an answer. I'm required to print out my code for a program, but I want to not print regions that are collapsed, as this would save about a dozen pages of paper per print. Where is the setting in Visual Studio 2015 to do this, if there is one? I am working in C#.
Prior to VS2015, the option to hide collapsed regions while printing was available on the Print dialog (as discussed here). Unfortunately, however, that feature has been removed (as noted in the "Known Issues" section of this release). Please consider voting to restore this capability on this UserVoice Request.
Cut/Paste your collapsed regions into functions and put those functions into a different file. Its easy "low lying fruit" in terms of refactoring and making you code so much easier to read.
Regions are like totally gross
I have been trying to get my head around the SDK API for visual studio for a while now. The problem is I know what I want/need to do. However I can not find a code example or API documentation anywhere that allows me to do what I want :(
I know its possible just I cant find documentation to show how.
I want to add another sidebar ribbon for all open files similar to how the breakpoint works (And many test add-ins for VS, NCrunch comes to mind...) that is blank as standard but when clicked allows some logic to be run to add a comment on that line.
Once added a Icon will be then displayed on that line allowing you to click the icon to view/edit the left comment. (The storage of this etc. is not a problem with the implementation I am doing) I just really need to know how to tell the API to add a new ribbon/side thing and plumb in the required logic :/
If this isn't possible I also had the idea of highlighting the word and again a icon popping up but that seems even more annoying to implement hence why I chose the side option if it is at all possible :/
Thanks in Advance to anyone who helps :D
The "sidebar ribbon" is called an editor margin. See Editor Extension Points.
Implement an IWpfTextViewMargin interface to define a margin. You must
also implement the IWpfTextViewMarginProvider interface to create the
margin.
I would like to make a support plug-ins in my program.
For example in my program there are several tabs in one tab is the editor in which the code is written also in that tab has a button run.
After pressing the button run occurs a compilation of source code and its execution.
The results of work are displayed in the other tab.
I would like to find such a component in which there are:
Syntax Highlighting, Debugger, Analogue of solution explorer
Thank you very much for your answers.
I would like to bring more of clarity to my question.
I want to do something similar to that is shown in the screenshots below
On a single tab there is the editor and at the other tab displays the results.
To write plug-ins I'd like to use C #.
I guess the best place to start is AvalonDock from CodePlex, specifically what you are trying to do is a Tabbed User Interface.
Keep in mind that even with a TabbedWindows framework build/debug and syntax highlighting are not for free and you will have to find icons and design the UI mostly yourself.
for code coloring there are also many components, also free, like Scintilla .NET
You obviously understand that Visual Studio is a very complex application, so rewriting portions of it will be difficult. There are components available to help you, like the ICSharpCode text editor. In fact, that whole project is probably quite valuable.
However, when thinking of plugins and actually writing code for it, I'd personally go down the MEF route. In fact, this is the very framework that VS.NET 2010 uses for extensibility. Provide your user/developer with a set of libraries to code against (like an SDK), and let them use a Visual Studio Express edition to write proper code :)
As source code editor you can use AvalonEdit (it is great, in some aspects even better than VS code editor), solution explorer is fairly easy to create and debugger is way too language-specific to be a reusable component (you didn't specify what language are you developing for!).
The whole thing can be packaged into AvalonDock, so you get the draggable and dockable panels - it even has VS 2010-like skin (and again - is very easy to implement even with only very basic WPF knowledge).
Or you can use the Visual Studio Isolated Shell - it allows you to use the Visual Studio interface in your program (the end users don't have to have VS installed!), but it requires extensive knowledge of VS API (if you ever developed VS extension you know what I am talking about). For example Civilization V used this approach for it's modding environment, but the result smells as stripped VS with custom splash screen, not as professional product. There are many buttons and config. options that don't work, some features that would be expected from such program (and easy to do in custom app) didn't get in because it would be nigh impossible to implant them into the VSIS etc...
EDIT: You may also eventually be interested in this.
How does Visual Studio and other similar programs display a form in their IDE?
Is it possible to achieve the same or a similar effect using C# or VB.NET?
Please see the picture below to get what I mean.
If you are talking about hosting a Winforms editor in your code, it is entirely possible and is actually built in to the .NET framework!
The Essence is the IDesignerHost interface. The whole system is complicated, but can be done (I have done it in production code for runtime layout configuration editing). There is a sample of code from Microsoft here.
I'm sure if you search fir 'IDesignerHost' you'll find enough reference material to figure it out.
Are you speaking about UI creating tools?
Refer to http://www.icsharpcode.net/opensource/sd/ - SharpDevelop for deep dive. It's open sourse, so you'll be able to find out more details.
I believe what you want is a multiple document interface (MDI) see http://msdn.microsoft.com/en-us/library/ms973874.aspx for more info.