刚从vs10迁移到vs12,发现花括号以及其他一些功能(如C#中的缩进)似乎完全失效了。 例如:type:
public static void myFunc() {
在Visual Studio 10中,它会自动添加相应的右括号。是否有一些插件或工具可以修复这个问题并提供相同的行为?Brace Completer需要在函数之后按Enter键才能添加闭合括号。同时,在"工具->选项->文本编辑器->C#->格式化->自动格式化完成的块"默认已开启。
刚从vs10迁移到vs12,发现花括号以及其他一些功能(如C#中的缩进)似乎完全失效了。 例如:type:
public static void myFunc() {
在Visual Studio 10中,它会自动添加相应的右括号。是否有一些插件或工具可以修复这个问题并提供相同的行为?Brace Completer需要在函数之后按Enter键才能添加闭合括号。默认情况下,Visual Studio 2010不会这样做(至少在我的情况下)。您确定没有使用像Productivity Power Tools这样的扩展吗?
这个扩展支持VS2012: http://visualstudiogallery.msdn.microsoft.com/0e33cb22-d4ac-4f5a-902f-aff5177cc94d
如果您以前没有使用过它,可以在“选项”>“生产力工具”中开启或关闭它所添加的几乎所有功能。
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;
}
}
}
}
}