繁体   English   中英

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

[英]Curly braces autocomplete in Visual Studio 2012

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

public static void myFunc() {

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

此外,在工具 - >选项 - >文本编辑器 - > c# - >格式化 - >自动格式化已完成的块开启}默认情况下已打开..

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

在此输入图像描述

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

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

生产力2012年的Power Tools现已上市,有自动支撑完成,OP几乎肯定使用2010版本。

适用于2013年的生产力电动工具

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM