visual-studio Visual Studio 2005/2012:如何将第一个花括号保留在同一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:25:41  来源:igfitidea点击:

Visual Studio 2005/2012: How to keep first curly brace on same line?

visual-studio

提问by Shawn

Trying to get my css / C# functions to look like this:

试图让我的 css/C# 函数看起来像这样:

body {
    color:#222;
}

instead of this:

而不是这个:

body 
{
    color:#222;
}

when I auto-format the code.

当我自动格式化代码时。

回答by Stormenet

C#

C#

  1. In the ToolsMenu click Options
  2. Click Show all Parameters(checkbox at the bottom left) (Show all settingsin VS 2010)
  3. Text Editor
  4. C#
  5. Formatting
  6. New lines
  1. 工具菜单中单击选项
  2. 单击显示所有参数(左下角的复选框)(显示VS 2010 中的所有设置
  3. 文本编辑器
  4. C#
  5. 格式化
  6. 新线路

And there check when you want new lines with brackets

然后检查何时需要带括号的新行

Css:

css:

almost the same, but fewer options

几乎相同,但选项较少

  1. In the ToolsMenu click Options
  2. Click Show all Parameters(checkbox at the bottom left) (Show all settingsin VS 2010)
  3. Text Editor
  4. CSS
  5. Format
  1. 工具菜单中单击选项
  2. 单击显示所有参数(左下角的复选框)(显示VS 2010 中的所有设置
  3. 文本编辑器
  4. CSS
  5. 格式

And than you select the formatting you want (in your case second radio button)

然后选择所需的格式(在您的情况下为第二个单选按钮)

For Visual Studio 2015:

对于 Visual Studio 2015:

Tools → Options

工具 → 选项

In the sidebar, go to Text Editor → C# → Formatting → New Lines

在侧边栏中,转到文本编辑器 → C# → 格式 → 新行

and uncheck every checkbox in the section "New line options for braces"

并取消选中“大括号的新行选项”部分中的每个复选框

enter image description here

在此处输入图片说明

回答by FlySwat

Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines -> New Line Options for braces -> Uncheck all boxes.

工具 -> 选项 -> 文本编辑器 -> C# -> 格式 -> 换行 -> 大括号的新行选项 -> 取消选中所有框。

回答by Seb Nilsson

The official MS guidelines (at the time in 2008)tells you to have the curly brace on the same line as the method/property/class and many other things which are not enforced in Visual Studio.

MS 官方指南(2008 年)告诉您将大括号与方法/属性/类以及许多其他在 Visual Studio 中未强制执行的内容放在同一行上。

You can change all these auto-text settings under:
Tools -> Options -> Text Editor -> [The language you want to change]

您可以在以下位置更改所有这些自动文本设置:
工具 -> 选项 -> 文本编辑器 -> [您要更改的语言]

UPDATE:This was based on the book "Framework Design Guidelines" written by some of the core-people from the .NET-team. If you look at the source-code for the likes of ASP.NET MVC, this is no longer accurate.

更新:这是基于 .NET 团队的一些核心人员编写的“框架设计指南”一书。如果您查看 ASP.NET MVC 之类的源代码,这将不再准确

回答by Gabriel Nahmias

Go to Tools -> Options -> Text Editor -> CSS -> Formatting. Click "Semi-expanded," which matches the style you defined.

Tools -> Options -> Text Editor -> CSS -> Formatting。单击与您定义的样式相匹配的“半展开”。

Options screen

选项屏幕

回答by Huppie

For CSS you'll need the 'Semi Expanded' option.

对于 CSS,您需要“半展开”选项。

回答by stoofy

There is a specific formatting setting in VS 2008/2010 to keep the open brace on the same line:

VS 2008/2010 中有一个特定的格式设置,可以将左大括号保持在同一行:

Click Tools->Options
Select 'CSS' within 'Text Editor' tree node
Select 'Formatting' under 'CSS' node
Click 'Semi-expanded' radio button

单击“工具”->“选项
” 在“文本编辑器”树节点中
选择“CSS” 在“CSS”节点下选择“格式”
单击“半展开”单选按钮

You will see a preview what the various radio buttons avail will do to the formatting

您将看到各种单选按钮对格式的作用的预览

回答by Luke

If you're looking for this option within Visual Studio 2014, then it's under advanced and is now a 'Brace positions' drop down box:

如果您正在Visual Studio 2014 中寻找此选项,那么它位于高级之下,现在是一个“支撑位置”下拉框:

enter image description here

在此处输入图片说明

回答by Reitenator

For Visual Studio Mac OS (Community edition) version 8.3 you need to do the following:

对于 Visual Studio Mac OS(社区版)8.3 版,您需要执行以下操作:

Preferences -> Source Code (in left menu) -> Code Formatting -> C# source code -> C# Format -> Press Edit

Preferences -> Source Code (in left menu) -> Code Formatting -> C# source code -> C# Format -> Press Edit

enter image description here

在此处输入图片说明