Visual Studio New Line Curly Brace Formatting Issue
Asked Answered
G

3

6

My problem is when I create a new method it looks like this...

public string firstName {
}

The cursor is stays right before the }, and then if I type more code it looks like...

public string firstName {
    get;}

And...

public string firstName {
    get;
    set;}

Then I have to press enter for it to look like...

public string firstName {
    get;
    set;
}

What I want for it to look like from the beginning is...

public string firstName {
    <cursor here>
}

I've looked all over in the formatting options, and can't seem to find anything. I used to get it like this before, and I don't know what happened that caused this issue. It's very annoying! Please help.

Greyso answered 23/4, 2016 at 23:12 Comment(4)
What version of VS are you using? That might help people figure out the right option.Boomerang
I'm using the latest update of the community Visual Studio 2015.Greyso
Wait, I’ve always just typed one brace, hit the right arrow, and hit enter, and it’s always where I need it to be…Nestor
What I hate even more is how it makes curly braces on a new line. That's how noob programmers do it. I remember when this was a hot topic because it was the new way to format code. I like it the old way, where the curly brace is on the same line as the statement. You can't count lines of code if most of it is just an opening curly brace.Rabbinism
B
12

Try this. Tools->Options->Text Editor->C#->Formatting->New Lines. In there, under New line options for braces, make sure those are all checked. That's what determines where the braces go when you type the left brace and then hit enter.

edit: That didn't do it, but second suggestion of Wrapping then checking "Leave block on single line" did do it for OP.

Boomerang answered 23/4, 2016 at 23:36 Comment(3)
I've already tried that and it causes the same problem except the opening brace is on a new line and the closing brace is on the next line. There is no empty line between the two. This problem is happening on both my laptop and desktop, and it didn't happen before.Greyso
@Nasir Maybe Wrapping -> Leave block on single line? Mine is checked. My VS does what you want yours to do. Other than that, maybe gnomes in your computers?Boomerang
@Nasir You've very welcome. Please upvote/accept since it worked. I'll edit my answer to include it.Boomerang
F
0

In Visual Studio 2022, Try to disable some or all checkboxs at:

Tools > Options (CTRL + ,) > C# (or another language) > Code Style > Formatting > New Lines

But, if the problem is the cursor behind the close braces, I solved it by check Leave block on single line and Leave statements and member declarations on the same line at:

Tools > Options (CTRL + ,) > C# (or another language) > Code Style > Formatting > Wrapping

Maybe other version of Visual Studio have different settings location, You could explore it.

I hope that helps :)

Felony answered 25/12, 2023 at 9:19 Comment(0)
B
0

I had this problem in the latest VS2022 too but for C++, not C#.

I managed to fix it by enabling "Automatically format braces when they are automatically completed" in C/C++ -> Code style -> Formatting -> General

Bochum answered 17/10 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.