26

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:

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 Completer requires to hit Enter after the function for it to add the closing braces.

Also in tools->options->text-editor->c#->formatting-> automatically format completed block on } is turned on by default..

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
aromasca
  • 821
  • 2
  • 8
  • 12
  • 4
    "tools->options->text-editor->c#->formatting-> automaticcly format completed block on }" doesn't auto-add the ending bracket. It formats the enclosed code...properly indents, etc. when you add the ending curly bracket. – Thelonias Aug 29 '12 at 12:32
  • 2
    Thanks, I needed this to turn off the auto brace, really annoying! – DevDave Feb 04 '13 at 16:39
  • 4
    I have the opposite problem. It seems 2013 is doing this by default. How the hell do you turn this off!? – BrainSlugs83 Apr 26 '14 at 22:57
  • 4
    Found it, under Text Editor (no sub group, select just the parent "Text Editor" node), uncheck (or if you're crazy like the OP, check) "Automatic brace completion". – BrainSlugs83 Apr 26 '14 at 23:00
  • Want to create Auto Complete Brackets for RichTextBox in C#/VB then go to -- [AutoComplete Brackets In C#.NET/VB .NET](http://www.c-sharpcorner.com/UploadFile/f5a10c/auto-complete-brackets-in-C-Sharpvb-net877/) – Pritam Zope Feb 01 '16 at 08:01

4 Answers4

44

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:

enter image description here

Bob Horn
  • 33,387
  • 34
  • 113
  • 219
22

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

This one supports VS2012: http://visualstudiogallery.msdn.microsoft.com/0e33cb22-d4ac-4f5a-902f-aff5177cc94d

coolmine
  • 4,427
  • 2
  • 33
  • 45
  • 1
    Thanks but brace completer requires Enter to complete the curly braces which isn't the behavior I was looking for. – aromasca Aug 29 '12 at 12:57
  • 1
    I'm assuming that you were using the one on the first link which doesn't look like it's been updated for VS2012 yet. That one doesnt seem to require to press enter. "Automatic Brace Completion improves the productivity of writing code by automatically inserting the closing code construct when the opening construct is typed for VB & C#." – coolmine Aug 29 '12 at 13:21
8

Productivity Power Tools for 2012 are available now which has auto-brace completion, OP was almost definitely using the 2010 version.

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.

mattmanser
  • 5,719
  • 3
  • 38
  • 50
-1

Here's the code to create Auto Complete Brackets for RichTextBox using C#.

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