C# Visual Studio 2012 中的花括号自动完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12177963/
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
Curly braces autocomplete in Visual Studio 2012
提问by aromasca
Just migrated from vs10 to vs12 and it seems like the curly braces is completely broken along side with some other features like indentation in C# (?) for example type:
刚刚从 vs10 迁移到 vs12,似乎大括号与其他一些功能一起完全断开,例如 C# 中的缩进 (?) 例如类型:
public static void myFunc() {
In visual studio 10 it would automatically add the closing curly brace for it. Is there some power tool or something that can fix this and give the same behavior? the Brace Completerrequires to hit Enter after the function for it to add the closing braces.
在 Visual Studio 10 中,它会自动为其添加右花括号。是否有一些电动工具或其他东西可以解决这个问题并提供相同的行为?在布雷斯完成者需要打功能后,输入它以添加结束括号。
Also in tools->options->text-editor->c#->formatting-> automatically format completed block on }is turned on by default..
同样在工具->选项->文本编辑器->c#->格式化->自动格式化完成的块}默认情况下是打开的..
采纳答案by coolmine
Visual Studio 2010 doesn't do that by default (at least not in my case). Are you sure you weren't using an extension like Productivity Power Tools
默认情况下,Visual Studio 2010 不会这样做(至少在我的情况下不会)。你确定你没有使用像Productivity Power Tools这样的扩展程序吗?
This one supports VS2012: http://visualstudiogallery.msdn.microsoft.com/0e33cb22-d4ac-4f5a-902f-aff5177cc94d
这个支持VS2012:http://visualstudiogallery.msdn.microsoft.com/0e33cb22-d4ac-4f5a-902f-aff5177cc94d
回答by mattmanser
Productivity Power Tools for 2012are available now which has auto-brace completion, OP was almost definitely using the 2010 version.
Productivity Power Tools for 2012现在可用,它具有自动支撑完成功能,OP 几乎肯定使用 2010 版本。
Productivity Power Tools for 2013
If you haven't used it before, you can turn on/off pretty much every feature it adds in options>productivity power tools.
如果您以前没有使用过它,您可以打开/关闭它在选项>生产力电动工具中添加的几乎所有功能。
回答by Bob Horn
If anyone is having this issue with VS 2013, there is a setting for this now. I just reset my VS settings and it started to complete my braces again. For me, it wasn't productivity power tools. You can turn it on/off here:
如果有人在 VS 2013 中遇到这个问题,现在有一个设置。我刚刚重置了我的 VS 设置,它又开始完成我的牙套了。对我来说,它不是生产力动力工具。您可以在此处打开/关闭它:


回答by Pritam Zope
Here's the code to create Auto Complete Brackets for RichTextBox using C#.
这是使用 C# 为 RichTextBox 创建自动完成括号的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Auto_Complete_Brackets
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//declare isCurslyBracesKeyPressed variable as Boolean and assign false value
//to check { key is pressed or not
public static Boolean isCurslyBracesKeyPressed = false;
//richTextBox1 KeyPress events
// if key (,{,<,",',[ is pressed then insert opposite key to richTextBox1 at Position SelectionStart+1
// add one line after inserting, e.Handled=true;
//finally set SelectionStart to specified position
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
String s = e.KeyChar.ToString();
int sel = richTextBox1.SelectionStart;
if (checkBox1.Checked == true)
{
switch (s)
{
case "(": richTextBox1.Text = richTextBox1.Text.Insert(sel, "()");
e.Handled = true;
richTextBox1.SelectionStart = sel + 1;
break;
case "{":
String t = "{}";
richTextBox1.Text = richTextBox1.Text.Insert(sel, t);
e.Handled = true;
richTextBox1.SelectionStart = sel + t.Length - 1;
isCurslyBracesKeyPressed = true;
break;
case "[": richTextBox1.Text = richTextBox1.Text.Insert(sel, "[]");
e.Handled = true;
richTextBox1.SelectionStart = sel + 1;
break;
case "<": richTextBox1.Text = richTextBox1.Text.Insert(sel, "<>");
e.Handled = true;
richTextBox1.SelectionStart = sel + 1;
break;
case "\"": richTextBox1.Text = richTextBox1.Text.Insert(sel, "\"\"");
e.Handled = true;
richTextBox1.SelectionStart = sel + 1;
break;
case "'": richTextBox1.Text = richTextBox1.Text.Insert(sel, "''");
e.Handled = true;
richTextBox1.SelectionStart = sel + 1;
break;
}
}
}
// richTextBox1 Key Down event
/*
* when key { is pressed and {} is inserted in richTextBox
* and isCurslyBracesKeyPressed is true then insert some blank text to richTextBox1
* when Enter key is down
* it will look like this when Enter key is down
{
|
}
* */
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
int sel = richTextBox1.SelectionStart;
if (e.KeyCode == Keys.Enter)
{
if(isCurslyBracesKeyPressed==true)
{
richTextBox1.Text = richTextBox1.Text.Insert(sel, "\n \n");
e.Handled = true;
richTextBox1.SelectionStart = sel + " ".Length;
isCurslyBracesKeyPressed = false;
}
}
}
}
}

