算法代码自动缩进支架 [英] Algorithm for auto-indenting brackets in code
问题描述
即时通讯工作的一个代码编辑器(的WinForms),我想知道怎么做的功能{和}专门用括号就像在实际的代码编辑器(打开和关闭)自动缩进。
--- |> {和}
块引用>
这样的1
编辑器是一个名为RTB的RichTextBox。
解决方案请阅读以下阅读和使用码之前文本:
- 的我已经没有足够的时间写出更好的代码。我只是试着写的示例为您服务。的
- 的我只写一个简单的方法的代码,没有OOP 的
- 的您可以用提高代码的枚举 属性 类和其他OOP的事情 的
- 的您可以提高在逻辑代码;您可以使用多线程的实现更好的性能 的
- 的此示例的不彻底。我只执行了一个自动缩进示例分号(;)字符 的
<。 p>我应该说一些的提示的使用代码:
- rtbCodes 是中的 RichTextBox控件的示例项目表单上的名称。
- frmCodeEditor 是形式的名称的示例项目
您可以从以下地址下载示例项目:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;使用System.Diagnostics程序
;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;
命名空间WindowsFormsApplication2
{
公共部分类frmCodeEditor:表
{
的char [] chrTracingKeyChars =新的char [] {';','} ','\\\
'};
的char [] chrCheckingKeyChars =新的char [] {'{','('};
点ptCurrentCharPosition;
布尔bolCheckCalling = FALSE;
INT intInitialCursorPosition = 0;
INT intRemainingCharsOfInitialText = 0;
INT intNextCharIndex = 0;
INT intPrevCharIndex = 0;
公共frmCodeEditor()
{
的InitializeComponent();
}
私人无效richTextBox1_TextChanged(对象发件人,EventArgs五)
{
AutoIndention(rtbCodes);
}
///<总结>
///实现自动缩进
///< /总结>
///< PARAM NAME =实时出价>在RichTextBox的控制< /参数>
私人无效AutoIndention(RichTextBox的RTB)
{
焦炭chrLastChar =的getchar(RTB);
如果(chrLastChar == chrTracingKeyChars [0] )
{
intRemainingCharsOfInitialText = rtb.TextLength - rtb.SelectionStart;
intInitialCursorPosition = rtb.SelectionStart;
ImplementIndentionForSemicolon(RTB);
}
,否则如果(chrLastChar == chrTracingKeyChars [1])$ B $ B {
ImplementIndentionForRightCurlyBracket(RTB);
}
,否则如果(chrLastChar == chrTracingKeyChars [2])
{
ImplementIndentionForNewLineCharacter(RTB);
}
}
///<总结>
///指定基于光标位置当前字符。
///< /总结>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
///<退货和GT;返回一个char< /回报>
私人焦炭的getchar(RichTextBox的RTB)
{
返回的getchar(rtb.SelectionStart,RTB);
}
///<总结>
///指定基于指定索引一个char。
///< /总结>
///< PARAM NAME =intCharIndex>将焦炭指数与LT; /参数>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
///<退货和GT;返回一个char< /回报>
私人焦炭的getchar(INT intCharIndex,RichTextBox的RTB)
{
如果(intCharIndex = rtb.TextLength!)
{
ptCurrentCharPosition = rtb.GetPositionFromCharIndex(intCharIndex - 1 );
}
,否则
{
ptCurrentCharPosition = rtb.GetPositionFromCharIndex(intCharIndex);
}
返回rtb.GetCharFromPosition(ptCurrentCharPosition);
}
///<总结>
///指定基于光标位置当前行号。
///< /总结>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
///<退货和GT;返回的行号< /回报>
私人诠释GetLineNumber(RichTextBox的RTB)
{
返回GetLineNumber(rtb.GetFirstCharIndexOfCurrentLine(),RTB);
}
///<总结>
///指定基于指定索引的行号。
///< /总结>
///< PARAM NAME =intCharIndex>将焦炭指数与LT; /参数>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
///<退货和GT;返回的行号< /回报>
私人诠释GetLineNumber(INT intCharIndex,RichTextBox的RTB)
{
返回rtb.GetLineFromCharIndex(intCharIndex);
}
///<总结>
///实现了分号缩进;字符。
///< /总结>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
私人无效ImplementIndentionForSemicolon(RichTextBox的RTB)
{
&字典LT;焦炭,INT> dicResult = IsExistCheckingKeyChars(RTB);
如果(!dicResult [chrCheckingKeyChars [0]] = -1)
{
INT intIndentionLevel = CheckingIndentionLevel(dicResult [chrCheckingKeyChars [0]],RTB);
ImplementIndention(dicResult [chrCheckingKeyChars [0],intIndentionLevel,RTB);
}
}
私人无效ImplementIndentionForRightCurlyBracket(RichTextBox的RTB)
{
}
私人无效ImplementIndentionForNewLineCharacter (RichTextBox的RTB)
{
}
///<总结>
///检查当前和以前行寻找关键字符。
///< /总结>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
///< PARAM NAME =bolSearchCurrentLine>在搜索状态下及/参数>
///<退货和GT;返回键的字符出现的第一个字符当前与之前LT; /回报>
私人字典<焦炭,INT> IsExistCheckingKeyChars(RTB的RichTextBox,布尔bolSearchCurrentLine = FALSE)
{
的getchar(RTB);
&字典LT;焦炭,INT> dicCheckingKeyCharsIndexes =新词典<焦炭,INT>();
表示(中间体intCntr = 0; intCntr&下; chrCheckingKeyChars.Length; intCntr ++)
{
dicCheckingKeyCharsIndexes.Add(chrCheckingKeyChars [intCntr],0);
}
为(INT intCntr = 0; intCntr< chrCheckingKeyChars.Length; intCntr ++)
{
INT intFirstIndexForChecking = 0;
INT intLastIndexForChecking = 0;
为(INT intLineCounter = GetLineNumber(RTB); intLineCounter> = 0; intLineCounter--)
{
如果(intLineCounter == GetLineNumber(RTB))
{
intLastIndexForChecking = rtb.GetCharIndexFromPosition(ptCurrentCharPosition);
}
,否则
{
intLastIndexForChecking = intFirstIndexForChecking - 1;
}
intFirstIndexForChecking = rtb.GetFirstCharIndexFromLine(intLineCounter);
试
{
dicCheckingKeyCharsIndexes [chrCheckingKeyChars [intCntr] = rtb.Find(chrCheckingKeyChars [intCntr]的ToString(),intFirstIndexForChecking,
rtb.GetCharIndexFromPosition(ptCurrentCharPosition ),RichTextBoxFinds.NoHighlight | RichTextBoxFinds.None);
如果(dicCheckingKeyCharsIndexes [chrCheckingKeyChars [intCntr]!= -1)
{
做
{
如果(rtb.Find(chrCheckingKeyChars [intCntr]的ToString( ),intFirstIndexForChecking,rtb.GetCharIndexFromPosition(ptCurrentCharPosition),
RichTextBoxFinds.NoHighlight | RichTextBoxFinds.None)= - 1)
{
dicCheckingKeyCharsIndexes [chrCheckingKeyChars [intCntr] = rtb.Find(chrCheckingKeyChars! [intCntr]的ToString(),intFirstIndexForChecking,
rtb.GetCharIndexFromPosition(ptCurrentCharPosition),RichTextBoxFinds.NoHighlight | RichTextBoxFinds.None);
}
intFirstIndexForChecking ++;
},而(intFirstIndexForChecking = rtb.GetCharIndexFromPosition(ptCurrentCharPosition)!);
中断;
}
}
抓
{
dicCheckingKeyCharsIndexes [chrCheckingKeyChars [intCntr] = -1;
中断;
}
如果(bolSearchCurrentLine)
{
中断;
}
}
}
返回dicCheckingKeyCharsIndexes;
}
///<总结>
///检查线路为计算其缩进水平。
///< /总结>
///< PARAM NAME =intCharIndex>将焦炭指数与LT; /参数>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
///<退货和GT;返回的行缩进水平与LT; /回报>
私人诠释CheckingIndentionLevel(INT intCharIndex,RichTextBox的RTB)
{
INT intLineNumber = GetLineNumber(intCharIndex,RTB);
INT intIndentionLevelNumber = 0;
intCharIndex = rtb.GetFirstCharIndexFromLine(intLineNumber);
焦炭chrChar =的getchar(intCharIndex,RTB);
如果(chrChar =='\\\
')
{
chrChar =的getchar(++ intCharIndex,RTB);
}
如果(chrChar =''!)
{
返回0;
}
,否则
{
INT intSpaceCntr = 0;
,而(chrChar =='')
{
chrChar =的getchar(++ intCharIndex,RTB);
如果(chrChar =='')
{
intSpaceCntr ++;
}
如果(intSpaceCntr%4 == 0安培;&安培;!intSpaceCntr = 0)
{
intIndentionLevelNumber ++;
intSpaceCntr = 0;
}
}
如果(intSpaceCntr%4 = 0!)
{
intIndentionLevelNumber ++;
}
}
返回intIndentionLevelNumber;
}
///<总结>
///实现缩进的代码
///< /总结>
///< PARAM NAME =intCharIndex>将焦炭指数与LT; /参数>
///< PARAM NAME =intIndentionLevel>缩进级别1所述的数量; /参数>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
私人无效ImplementIndention(INT intCharIndex,诠释intIndentionLevel,RichTextBox的RTB)
{
intNextCharIndex = intCharIndex;
intPrevCharIndex = intCharIndex;
INT intKeyCharsNumberInLine = 1;
INT intCurrentLineNumber = GetLineNumber(RTB);
INT intKeyCharLineNumber = GetLineNumber(intNextCharIndex,RTB);
的String [] strLinesTexts;
&字典LT;焦炭,INT> dicResult;
做
{
rtb.SelectionStart = intPrevCharIndex;
dicResult = IsExistCheckingKeyChars(RTB);
如果(!dicResult [chrCheckingKeyChars [0]] = -1)
{
intKeyCharsNumberInLine ++;
intPrevCharIndex = dicResult [chrCheckingKeyChars [0]];
}
}当(dicResult [chrCheckingKeyChars [0]] = -1!);
如果
{
如果(intCurrentLineNumber == intKeyCharLineNumber)
{
为(INT intCntr = 1(bolCheckCalling!); intCntr< = intKeyCharsNumberInLine ; intCntr ++)
{
做
{
rtb.SelectionStart = intPrevCharIndex;
dicResult = IsExistCheckingKeyChars(RTB,真);
如果(dicResult [chrCheckingKeyChars [0]] = - 1!)
{
intPrevCharIndex = dicResult [chrCheckingKeyChars [0]];
}
}当(dicResult [chrCheckingKeyChars [0]] = -1!);
bolCheckCalling = TRUE;
ImplementIndention(intPrevCharIndex,RTB);
}
的回报;
}
}
bolCheckCalling = FALSE;
rtb.SelectionStart = intNextCharIndex;
rtb.SelectionLength = 1;
rtb.SelectedText =\\\
+ rtb.SelectedText;
intCurrentLineNumber = GetLineNumber(RTB);
strLinesTexts = rtb.Lines;
strLinesTexts [intCurrentLineNumber] = strLinesTexts [intCurrentLineNumber] .Trim();
为(INT intIndentionCntr = 1; intIndentionCntr< = intIndentionLevel; intIndentionCntr ++)
{
为(INT intSpaceCntr = 1; intSpaceCntr< = 4; intSpaceCntr ++)
{
strLinesTexts [intCurrentLineNumber] =''+ strLinesTexts [intCurrentLineNumber]
}
}
rtb.Lines = strLinesTexts;
rtb.SelectionStart = intNextCharIndex +((intIndentionLevel * 4)+ 1);
intNextCharIndex = rtb.SelectionStart;
rtb.SelectionLength = 1;
rtb.SelectedText = rtb.SelectedText +\\\
intCurrentLineNumber = GetLineNumber(RTB);
strLinesTexts = rtb.Lines;
strLinesTexts [intCurrentLineNumber] = strLinesTexts [intCurrentLineNumber] .Trim();
为(INT intIndentionCntr = 1; intIndentionCntr< = intIndentionLevel + 1; intIndentionCntr ++)
{
为(INT intSpaceCntr = 1; intSpaceCntr< = 4; intSpaceCntr ++)
{
strLinesTexts [intCurrentLineNumber] =''+ strLinesTexts [intCurrentLineNumber]
}
}
rtb.Lines = strLinesTexts;
rtb.SelectionStart = intInitialCursorPosition +((rtb.TextLength - intInitialCursorPosition) - intRemainingCharsOfInitialText);
intNextCharIndex = rtb.SelectionStart;
intPrevCharIndex = intNextCharIndex;
}
///<总结>
///实现缩进的代码
///< /总结>
///< PARAM NAME =intCharIndex>将焦炭指数与LT; /参数>
///< PARAM NAME =RTB>将RichTextBox控件< /参数>
私人无效ImplementIndention(INT intCharIndex,RichTextBox的RTB)
{
INT intIndentionLevel = CheckingIndentionLevel(intCharIndex,RTB);
ImplementIndention(intCharIndex,intIndentionLevel,RTB);
}
}
}
我希望这个示例代码可以帮助你。
请更新和共享代码,如果你改善。
im working on a code-editor (WinForms) and i want to know how to do function of { and } specifically the auto-indention using brackets (open and close) like in actual code editor .
---|> { and }
like this 1:
Editor was a richtextbox named rtb.
解决方案Please read the following texts before reading and using codes:
- I've not enough time for writing better codes. I have only tried to write a sample for you.
- I have only written the codes in a simple way, not in OOP.
- You can improve the codes with using Enums, Properties, Classes and other things of OOP.
- You can improve the logic of the codes; and you can use Multi-Threading for achieve to better performance.
- This sample isn't thorough. I has only implemented an Auto-Indention example for "semicolon (;)" character.
I should say some tips for using the codes:
- rtbCodes is the name of the RichTextBox control on the form in the sample project.
- frmCodeEditor is the name of the form in the sample project.
You can download the sample project from the following addresses:
4Shared -> Auto-Indention for Code Editor
SendSpace -> Auto-Indention for Code Editor
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class frmCodeEditor : Form { char[] chrTracingKeyChars = new char[] { ';', '}', '\n' }; char[] chrCheckingKeyChars = new char[] { '{', '(' }; Point ptCurrentCharPosition; bool bolCheckCalling = false; int intInitialCursorPosition = 0; int intRemainingCharsOfInitialText = 0; int intNextCharIndex = 0; int intPrevCharIndex = 0; public frmCodeEditor() { InitializeComponent(); } private void richTextBox1_TextChanged(object sender, EventArgs e) { AutoIndention(rtbCodes); } /// <summary> /// Implements Auto-Indention. /// </summary> /// <param name="rtb">A RichTextBox control</param> private void AutoIndention(RichTextBox rtb) { char chrLastChar = GetChar(rtb); if (chrLastChar == chrTracingKeyChars[0]) { intRemainingCharsOfInitialText = rtb.TextLength - rtb.SelectionStart; intInitialCursorPosition = rtb.SelectionStart; ImplementIndentionForSemicolon(rtb); } else if (chrLastChar == chrTracingKeyChars[1]) { ImplementIndentionForRightCurlyBracket(rtb); } else if (chrLastChar == chrTracingKeyChars[2]) { ImplementIndentionForNewLineCharacter(rtb); } } /// <summary> /// Specifies current char based on the cursor position. /// </summary> /// <param name="rtb">A RichTextBox control</param> /// <returns>Returns a char.</returns> private char GetChar(RichTextBox rtb) { return GetChar(rtb.SelectionStart, rtb); } /// <summary> /// Specifies a char based on the specified index. /// </summary> /// <param name="intCharIndex">A char index</param> /// <param name="rtb">A RichTextBox control</param> /// <returns>Returns a char.</returns> private char GetChar(int intCharIndex, RichTextBox rtb) { if (intCharIndex != rtb.TextLength) { ptCurrentCharPosition = rtb.GetPositionFromCharIndex(intCharIndex - 1); } else { ptCurrentCharPosition = rtb.GetPositionFromCharIndex(intCharIndex); } return rtb.GetCharFromPosition(ptCurrentCharPosition); } /// <summary> /// Specifies current line number based on the cursor position. /// </summary> /// <param name="rtb">A RichTextBox control</param> /// <returns>Returns the line number.</returns> private int GetLineNumber(RichTextBox rtb) { return GetLineNumber(rtb.GetFirstCharIndexOfCurrentLine(), rtb); } /// <summary> /// Specifies the line number based on the specified index. /// </summary> /// <param name="intCharIndex">A char index</param> /// <param name="rtb">A RichTextBox control</param> /// <returns>Returns the line number.</returns> private int GetLineNumber(int intCharIndex, RichTextBox rtb) { return rtb.GetLineFromCharIndex(intCharIndex); } /// <summary> /// Implements indention for semicolon ";" character. /// </summary> /// <param name="rtb">A RichTextBox control</param> private void ImplementIndentionForSemicolon(RichTextBox rtb) { Dictionary<char, int> dicResult = IsExistCheckingKeyChars(rtb); if (dicResult[chrCheckingKeyChars[0]] != -1) { int intIndentionLevel = CheckingIndentionLevel(dicResult[chrCheckingKeyChars[0]], rtb); ImplementIndention(dicResult[chrCheckingKeyChars[0]], intIndentionLevel, rtb); } } private void ImplementIndentionForRightCurlyBracket(RichTextBox rtb) { } private void ImplementIndentionForNewLineCharacter(RichTextBox rtb) { } /// <summary> /// Checks current and previous lines for finding key-chars. /// </summary> /// <param name="rtb">A RichTextBox control</param> /// <param name="bolSearchCurrentLine">The search state</param> /// <returns>Returns first occurrences of key-chars before current char.</returns> private Dictionary<char, int> IsExistCheckingKeyChars(RichTextBox rtb, bool bolSearchCurrentLine = false) { GetChar(rtb); Dictionary<char, int> dicCheckingKeyCharsIndexes = new Dictionary<char, int>(); for (int intCntr = 0; intCntr < chrCheckingKeyChars.Length; intCntr++) { dicCheckingKeyCharsIndexes.Add(chrCheckingKeyChars[intCntr], 0); } for (int intCntr = 0; intCntr < chrCheckingKeyChars.Length; intCntr++) { int intFirstIndexForChecking = 0; int intLastIndexForChecking = 0; for (int intLineCounter = GetLineNumber(rtb); intLineCounter >= 0; intLineCounter--) { if (intLineCounter == GetLineNumber(rtb)) { intLastIndexForChecking = rtb.GetCharIndexFromPosition(ptCurrentCharPosition); } else { intLastIndexForChecking = intFirstIndexForChecking - 1; } intFirstIndexForChecking = rtb.GetFirstCharIndexFromLine(intLineCounter); try { dicCheckingKeyCharsIndexes[chrCheckingKeyChars[intCntr]] = rtb.Find(chrCheckingKeyChars[intCntr].ToString(), intFirstIndexForChecking, rtb.GetCharIndexFromPosition(ptCurrentCharPosition), RichTextBoxFinds.NoHighlight | RichTextBoxFinds.None); if (dicCheckingKeyCharsIndexes[chrCheckingKeyChars[intCntr]] != -1) { do { if (rtb.Find(chrCheckingKeyChars[intCntr].ToString(), intFirstIndexForChecking, rtb.GetCharIndexFromPosition(ptCurrentCharPosition), RichTextBoxFinds.NoHighlight | RichTextBoxFinds.None) != -1) { dicCheckingKeyCharsIndexes[chrCheckingKeyChars[intCntr]] = rtb.Find(chrCheckingKeyChars[intCntr].ToString(), intFirstIndexForChecking, rtb.GetCharIndexFromPosition(ptCurrentCharPosition), RichTextBoxFinds.NoHighlight | RichTextBoxFinds.None); } intFirstIndexForChecking++; } while (intFirstIndexForChecking != rtb.GetCharIndexFromPosition(ptCurrentCharPosition)); break; } } catch { dicCheckingKeyCharsIndexes[chrCheckingKeyChars[intCntr]] = -1; break; } if (bolSearchCurrentLine) { break; } } } return dicCheckingKeyCharsIndexes; } /// <summary> /// Checks a line for calculating its indention level. /// </summary> /// <param name="intCharIndex">A char index</param> /// <param name="rtb">A RichTextBox control</param> /// <returns>Returns indention level of the line.</returns> private int CheckingIndentionLevel(int intCharIndex, RichTextBox rtb) { int intLineNumber = GetLineNumber(intCharIndex, rtb); int intIndentionLevelNumber = 0; intCharIndex = rtb.GetFirstCharIndexFromLine(intLineNumber); char chrChar = GetChar(intCharIndex, rtb); if (chrChar == '\n') { chrChar = GetChar(++intCharIndex, rtb); } if (chrChar != ' ') { return 0; } else { int intSpaceCntr = 0; while(chrChar == ' ') { chrChar = GetChar(++intCharIndex, rtb); if (chrChar == ' ') { intSpaceCntr++; } if (intSpaceCntr % 4 == 0 && intSpaceCntr != 0) { intIndentionLevelNumber++; intSpaceCntr = 0; } } if (intSpaceCntr % 4 != 0) { intIndentionLevelNumber++; } } return intIndentionLevelNumber; } /// <summary> /// Implements Indention to the codes /// </summary> /// <param name="intCharIndex">A char index</param> /// <param name="intIndentionLevel">The number of indention level</param> /// <param name="rtb">A RichTextBox control</param> private void ImplementIndention(int intCharIndex, int intIndentionLevel, RichTextBox rtb) { intNextCharIndex = intCharIndex; intPrevCharIndex = intCharIndex; int intKeyCharsNumberInLine = 1; int intCurrentLineNumber = GetLineNumber(rtb); int intKeyCharLineNumber = GetLineNumber(intNextCharIndex, rtb); string[] strLinesTexts; Dictionary<char, int> dicResult; do { rtb.SelectionStart = intPrevCharIndex; dicResult = IsExistCheckingKeyChars(rtb); if (dicResult[chrCheckingKeyChars[0]] != -1) { intKeyCharsNumberInLine++; intPrevCharIndex = dicResult[chrCheckingKeyChars[0]]; } } while (dicResult[chrCheckingKeyChars[0]] != -1); if (!bolCheckCalling) { if (intCurrentLineNumber == intKeyCharLineNumber) { for (int intCntr = 1; intCntr <= intKeyCharsNumberInLine; intCntr++) { do { rtb.SelectionStart = intPrevCharIndex; dicResult = IsExistCheckingKeyChars(rtb, true); if (dicResult[chrCheckingKeyChars[0]] != -1) { intPrevCharIndex = dicResult[chrCheckingKeyChars[0]]; } } while (dicResult[chrCheckingKeyChars[0]] != -1); bolCheckCalling = true; ImplementIndention(intPrevCharIndex, rtb); } return; } } bolCheckCalling = false; rtb.SelectionStart = intNextCharIndex; rtb.SelectionLength = 1; rtb.SelectedText = "\n" + rtb.SelectedText; intCurrentLineNumber = GetLineNumber(rtb); strLinesTexts = rtb.Lines; strLinesTexts[intCurrentLineNumber] = strLinesTexts[intCurrentLineNumber].Trim(); for (int intIndentionCntr = 1; intIndentionCntr <= intIndentionLevel; intIndentionCntr++) { for (int intSpaceCntr = 1; intSpaceCntr <= 4; intSpaceCntr++) { strLinesTexts[intCurrentLineNumber] = ' ' + strLinesTexts[intCurrentLineNumber]; } } rtb.Lines = strLinesTexts; rtb.SelectionStart = intNextCharIndex + ((intIndentionLevel * 4) + 1); intNextCharIndex = rtb.SelectionStart; rtb.SelectionLength = 1; rtb.SelectedText = rtb.SelectedText + "\n"; intCurrentLineNumber = GetLineNumber(rtb); strLinesTexts = rtb.Lines; strLinesTexts[intCurrentLineNumber] = strLinesTexts[intCurrentLineNumber].Trim(); for (int intIndentionCntr = 1; intIndentionCntr <= intIndentionLevel + 1; intIndentionCntr++) { for (int intSpaceCntr = 1; intSpaceCntr <= 4; intSpaceCntr++) { strLinesTexts[intCurrentLineNumber] = ' ' + strLinesTexts[intCurrentLineNumber]; } } rtb.Lines = strLinesTexts; rtb.SelectionStart = intInitialCursorPosition + ((rtb.TextLength - intInitialCursorPosition) - intRemainingCharsOfInitialText); intNextCharIndex = rtb.SelectionStart; intPrevCharIndex = intNextCharIndex; } /// <summary> /// Implements Indention to the codes /// </summary> /// <param name="intCharIndex">A char index</param> /// <param name="rtb">A RichTextBox control</param> private void ImplementIndention(int intCharIndex, RichTextBox rtb) { int intIndentionLevel = CheckingIndentionLevel(intCharIndex, rtb); ImplementIndention(intCharIndex, intIndentionLevel, rtb); } } }
I hope that this sample codes can help you.
Please update and share codes, if you improve them.
这篇关于算法代码自动缩进支架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!