【问题标题】:Curly braces autocomplete in Visual Studio 2012Visual Studio 2012 中的花括号自动完成
【发布时间】:2012-08-24 01:33:39
【问题描述】:

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

public static void myFunc() {

在 Visual Studio 10 中,它会自动为其添加右花括号。 是否有一些电动工具或其他东西可以解决这个问题并给出相同的行为? Brace Completer 需要在函数后按 Enter 才能添加右大括号。

同样在工具->选项->文本编辑器->c#->格式化->自动格式化}上的已完成块 默认开启..

【问题讨论】:

  • "tools->options->text-editor->c#->formatting-> automaticcly format completed block on }" 不会自动添加结束括号。当您添加结束大括号时,它会格式化封闭的代码......正确缩进等。
  • 谢谢,我需要这个来关闭自动支架,真烦人!
  • 我有相反的问题。似乎 2013 默认情况下正在执行此操作。你到底是怎么关掉这个的!?
  • 找到它,在文本编辑器下(没有子组,只选择父“文本编辑器”节点),取消选中(或者如果你像 OP 一样疯狂,选中)“自动大括号完成”。
  • 想在 C#/VB 中为 RichTextBox 创建自动完成括号然后转到 -- AutoComplete Brackets In C#.NET/VB .NET

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


【解决方案1】:

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

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

【讨论】:

  • 谢谢,但是大括号完成者需要 Enter 来完成花括号,这不是我想要的行为。
  • 我假设您使用的是第一个链接上的那个,它看起来还没有针对 VS2012 进行更新。那个似乎不需要按回车键。 “自动大括号完成通过在为 VB 和 C# 键入打开构造时自动插入关闭代码构造来提高编写代码的效率。”
【解决方案2】:

Productivity Power Tools for 2012 现在可用,它具有自动大括号完成功能,OP 几乎肯定使用的是 2010 版本。

Productivity Power Tools for 2013

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

【讨论】:

    【解决方案3】:

    如果有人在 VS 2013 中遇到此问题,现在有一个设置。我刚刚重置了我的 VS 设置,它又开始完成我的牙套。对我来说,这不是生产力电动工具。您可以在此处打开/关闭它:

    【讨论】:

    • 感谢上帝,如果您不习惯使用它,那会非常烦人。
    • 好的,如果 Visual Studio 正确返回了搜索词“brace”...(它没有)
    【解决方案4】:

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

    【讨论】:

    • 这与问题有什么关系??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2013-04-17
    • 2013-05-27
    • 2012-06-18
    • 2018-04-29
    • 1970-01-01
    相关资源
    最近更新 更多