在Visual Studio 2012中使用Curly括号自动完成

时间:2012-08-29 12:27:40

标签: c# ide visual-studio-2012 visual-studio-power-tools

刚刚从vs10迁移到vs12,似乎花括号与C#中的缩进等其他功能完全一致 例如类型:

public static void myFunc() {

在visual studio 10中,它会自动添加闭合花括号。 是否有一些电动工具或某些东西可以解决这个并给出相同的行为? Brace Completer需要在函数后点击Enter以添加结束括号。

同样在工具中 - >选项 - > text-editor-> c# - > formatting->自动格式化}上已完成的块 默认情况下已启用..

4 个答案:

答案 0 :(得分:43)

如果有人在VS 2013中遇到此问题,那么现在就有了这样的设置。我只是重置我的VS设置,它又开始重新完成我的大括号。对我来说,这不是生产力工具。你可以在这里打开/关闭它:

enter image description here

答案 1 :(得分:22)

默认情况下,Visual Studio 2010不会这样做(至少在我的情况下不是这样)。您确定没有使用像Productivity Power Tools

这样的扩展程序吗?

这个支持VS2012: http://visualstudiogallery.msdn.microsoft.com/0e33cb22-d4ac-4f5a-902f-aff5177cc94d

答案 2 :(得分:8)

Productivity Power Tools for 2012现已推出,有自动支撑完成,OP几乎肯定使用2010版本。

Productivity Power Tools for 2013

如果您之前没有使用它,您可以打开/关闭它在选项>生产力电动工具中添加的每个功能。

答案 3 :(得分:-1)

以下是使用C#为RichTextBox创建Auto Complete Brackets的代码。

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;  
                }  
            }  
        }  
    }  
}