Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#匹配RichTextBox中的括号和缩进?_C#_Text_Formatting - Fatal编程技术网

使用C#匹配RichTextBox中的括号和缩进?

使用C#匹配RichTextBox中的括号和缩进?,c#,text,formatting,C#,Text,Formatting,我想做一个代码编辑控件,它可以像Visual Studio一样格式化文本,直到现在我已经实现了语法高亮显示和自动补全,但我想用嵌套的大括号格式化文本。例如:考虑一个For循环 for(int i=0;i<=10;i++) { Function_One(); //This should be a tab away from first brace Function_Two(); //So with this if(a==b)

我想做一个代码编辑控件,它可以像Visual Studio一样格式化文本,直到现在我已经实现了语法高亮显示和自动补全,但我想用嵌套的大括号格式化文本。例如:考虑一个For循环

for(int i=0;i<=10;i++)
{
Function_One();              //This should be a tab away from first brace
Function_Two();              //So with this
if(a==b)                     //So with this
{                            //This should be four tabs away from first brace
MessageBox.Show("Some text");//This should be six tabs away from first brace
}                            //This should be four tabs away from first brace
}

for(int i=0;i这绝不是一个简单的壮举,我不知道您可以利用任何工具或插件,我唯一的建议是研究Monodevelop的实现


有关详细信息,请参见MonoDevelop。

我认为实现此功能的最佳方法是为表单创建一些全局变量:

private int _nestedBracketCount = 0;
private const string TabString = "    ";
private const int TabSpaces = 4;
然后在富文本框的
按键
事件处理程序中处理所有内容:

private void richTextBox1_OnKeyPress(object sender, KeyPressEventArgs e) {
    var currentLength = richTextBox1.Text.Length;

    if (e.KeyChar == '{') {
        // need to increment bracket counter
        _nestedBracketCount++;
    } else if (e.KeyChar == '}') {
        // remove last #(TabSpaces) characters from the richtextbox
        richTextBox1.Text.Remove(currentLength - TabSpaces);
        _nestedBracketCount--;
        richTextBox1.AppendText("}");
        e.Handled = true;
    } else if (e.KeyChar == (char)13) {
        // append newline and correct number of tabs.
        var formattedSpaces = string.Empty;
        for (var i = 0; i < _nestedBracketCount; i++)
            formattedSpaces += TabString;
        richTextBox1.AppendText("\n" + formattedSpaces);
        e.Handled = true;
    }
}
private void richTextBox1_OnKeyPress(对象发送方,KeyPressEventArgs e){
var currentLength=richTextBox1.Text.Length;
如果(e.KeyChar=='{'){
//需要增加括号计数器
_嵌套的BracketCount++;
}else if(e.KeyChar=='}'){
//从richtextbox中删除最后的#(制表符空格)字符
richTextBox1.Text.Remove(currentLength-TabSpaces);
_嵌套括号计数--;
richTextBox1.AppendText(“}”);
e、 已处理=正确;
}else if(e.KeyChar==(char)13){
//追加换行符和正确的制表符数。
var formattedSpaces=string.Empty;
对于(变量i=0;i<_嵌套括号计数;i++)
formattedSpaces+=TabString;
richTextBox1.AppendText(“\n”+格式化空格);
e、 已处理=正确;
}
}

我认为这应该为您提供一个相当不错的起点。

您的问题是什么?太笼统了,没有与实际实现相关的代码。而且,为什么突然要在MessageBox.Show()上从2个选项卡跳到6个选项卡行?我看到了你的编辑--仍然是常规的,仍然没有实际的代码。我不想从2个标签跳到6个标签,我想从第一个大括号缩进这么多。
private void richTextBox1_OnKeyPress(object sender, KeyPressEventArgs e) {
    var currentLength = richTextBox1.Text.Length;

    if (e.KeyChar == '{') {
        // need to increment bracket counter
        _nestedBracketCount++;
    } else if (e.KeyChar == '}') {
        // remove last #(TabSpaces) characters from the richtextbox
        richTextBox1.Text.Remove(currentLength - TabSpaces);
        _nestedBracketCount--;
        richTextBox1.AppendText("}");
        e.Handled = true;
    } else if (e.KeyChar == (char)13) {
        // append newline and correct number of tabs.
        var formattedSpaces = string.Empty;
        for (var i = 0; i < _nestedBracketCount; i++)
            formattedSpaces += TabString;
        richTextBox1.AppendText("\n" + formattedSpaces);
        e.Handled = true;
    }
}