Visual Studio 2012中的花括号自动完成

26

刚从vs10迁移到vs12,发现花括号以及其他一些功能(如C#中的缩进)似乎完全失效了。 例如:type:

public static void myFunc() {
在Visual Studio 10中,它会自动添加相应的右括号。是否有一些插件或工具可以修复这个问题并提供相同的行为?Brace Completer需要在函数之后按Enter键才能添加闭合括号。
同时,在"工具->选项->文本编辑器->C#->格式化->自动格式化完成的块"默认已开启。

4
“tools->options->text-editor->c#->formatting-> automaticcly format completed block on }”并不会自动添加结束括号。它会在您添加结尾花括号时格式化所包含的代码,包括适当的缩进等。 - Thelonias
2
谢谢,我需要这个来关闭自动括号,真的很烦人! - DevDave
4
我有相反的问题。似乎默认情况下2013会这样做。到底怎么关闭呢?! - BrainSlugs83
4
找到它了,在“文本编辑器”下(没有子组,只需选择父级“文本编辑器”节点),取消勾选“自动括号完成”(或者如果您像OP那样疯狂,可以勾选)。 - BrainSlugs83
想要在C#/VB中为RichTextBox创建自动完成括号功能,请访问--C#.NET/VB .NET中的自动完成括号 - Pritam Zope
4个回答

44
如果有人在使用VS 2013时遇到此问题,现在有一个设置可以解决。我刚刚重置了我的VS设置,它开始再次自动补全括号了。对我来说,这不是生产力工具。您可以在此处打开/关闭:

12
谢天谢地,如果你不习惯于与它一起工作,那将是极其恼人的。 - Carlos P
好的,也许如果Visual Studio对于搜索词“brace”正确返回结果的话...(它没有) - zastrowm

22

1
谢谢,但是括号自动补全需要按Enter键才能完成花括号的补全,这不是我想要的行为。 - aromasca
1
我假设你使用的是第一个链接上的插件,它似乎还没有更新到VS2012。那个插件似乎不需要按回车键。自动括号完成功能通过在输入VB和C#的开头结构时自动插入相应的结尾结构来提高编写代码的效率。 - coolmine

8

-1
这是使用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;  
                }  
            }  
        }  
    }  
}  

2
这与问题有什么关系? - Vahid Amiri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接