C# Richtextbox上的编号列表

C# Richtextbox上的编号列表,c#,winforms,richtextbox,C#,Winforms,Richtextbox,我正在尝试向文本编辑器添加编号列表功能。RichTextbox已经提供了SelectionBullet属性来将选择更改为项目符号列表。但我找不到类似的属性来生成编号列表。是否有任何标准方法在Richtextbox上创建编号列表如果不是,我将不得不自己实现它,这样可以帮助我实现的代码片段将有所帮助,谢谢。我知道链接通常不会被认为是一个好的答案,但是关于CodeProject的文章可能会帮到你解决你正在寻找的问题 在本文中,作者将RichTextBox控件扩展为可以满足您要求的功能(以及更多功能),

我正在尝试向文本编辑器添加编号列表功能。RichTextbox已经提供了
SelectionBullet
属性来将选择更改为项目符号列表。但我找不到类似的属性来生成编号列表。是否有任何标准方法在Richtextbox上创建编号列表如果不是,我将不得不自己实现它,这样可以帮助我实现的代码片段将有所帮助,谢谢。

我知道链接通常不会被认为是一个好的答案,但是关于CodeProject的文章可能会帮到你解决你正在寻找的问题


在本文中,作者将RichTextBox控件扩展为可以满足您要求的功能(以及更多功能),并将代码发布在那里,供大家查看。

好吧,我实现了它,如下所示

  private void btnNumbers_Click(object sender, EventArgs e)
        {
            string temptext = rtbMain.SelectedText;

            int SelectionStart = rtbMain.SelectionStart;
            int SelectionLength = rtbMain.SelectionLength;

            rtbMain.SelectionStart = rtbMain.GetFirstCharIndexOfCurrentLine();
            rtbMain.SelectionLength = 0;
            rtbMain.SelectedText = "1. ";

            int j = 2;
            for( int i = SelectionStart; i < SelectionStart + SelectionLength; i++)
                if (rtbMain.Text[i] == '\n')
                {
                    rtbMain.SelectionStart = i + 1;
                    rtbMain.SelectionLength = 0;
                    rtbMain.SelectedText = j.ToString() + ". ";
                    j++;
                    SelectionLength += 3;
                }

        }

        private void rtbMain_KeyDown(object sender, KeyEventArgs e)
        {//this piece of code automatically increments the bulleted list when user //presses Enter key
            int tempNum;
            if (e.KeyCode == Keys.Enter)
                try
                    {
                        if (char.IsDigit(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine()]))
                        {
                            if (char.IsDigit(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 1]) && rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 2] == '.')
                                tempNum = int.Parse(rtbMain.Text.Substring(rtbMain.GetFirstCharIndexOfCurrentLine(),2));
                            else tempNum = int.Parse(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine()].ToString());

                            if (rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 1] == '.' || (char.IsDigit(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 1]) && rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 2] == '.'))
                            {
                                tempNum++;
                                    rtbMain.SelectedText = "\r\n" + tempNum.ToString() + ". ";
                                e.SuppressKeyPress = true;
                            }
                        }  
                    }
                 catch{}
        }
private void btnNumbers\u单击(对象发送者,事件参数e)
{
字符串试探文本=rtbMain.SelectedText;
int-SelectionStart=rtbMain.SelectionStart;
int-SelectionLength=rtbMain.SelectionLength;
rtbMain.SelectionStart=rtbMain.GetFirstCharIndexOfCurrentLine();
rtbMain.SelectionLength=0;
rtbMain.SelectedText=“1。”;
int j=2;
for(int i=SelectionStart;i
这是我的答案。。。易于阅读和提炼。我采用了一种非常不同的方法,但添加了删除选择中已编号列表(如果该列表已经存在)的功能。请注意,到目前为止,我只是轻轻地测试它,它似乎工作良好。。。但它可能需要进一步完善

    private void btnOrdered_Click(object sender, EventArgs e)
    {
        string[] splitSelection = null;
        // If selection split selection else split everything
        if (this.txtCaptionEditor.SelectionLength > 0)
        {
            splitSelection = this.txtCaptionEditor.SelectedText.Replace("\r\n", "\n").Split("\n".ToCharArray());
        }
        else
        {
            splitSelection = this.txtCaptionEditor.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());
        }
        bool Exists = false;
        for (int i = 0; i < splitSelection.GetLength(0); i++)
        {
            // If Ordered List Allready exists in selection then remove else add
            if (!string.IsNullOrEmpty(splitSelection[i]))
            {
                if (splitSelection[i].Substring(0, 2) == "1.") { Exists = true; }
            }
        }

        for (int i = 0; i < splitSelection.GetLength(0); i++)
        {
            int lineCount = (i + 1);
            if (Exists)
            {
                this.txtCaptionEditor.Text = this.txtCaptionEditor.Text.Replace(Convert.ToString(lineCount) + ".  ", "");
            }
            else
            {
                if(!string.IsNullOrEmpty(splitSelection[i]))
                {
                    this.txtCaptionEditor.Text = this.txtCaptionEditor.Text.Replace(splitSelection[i], Convert.ToString(lineCount) + ".  " + splitSelection[i]);
                }

            }
        }
    }

    private void txtCaptionEditor_KeyDown(object sender, KeyEventArgs e)
    {
        string[] splitSelection = this.txtCaptionEditor.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());
        if (e.KeyCode == Keys.Enter)
        {
            // Get Current Line Position
            int currentLine = this.txtCaptionEditor.GetLineFromCharIndex(this.txtCaptionEditor.SelectionStart);
            // Only Run if the previous line is greater than zero
            if ((currentLine) >= 0)
            {
                // Loop through 100 possible numbers for match you can go higher 
                // If you think your numbered list could go above 100
                for (int i = 0; i < 100; i++)
                {
                    if (splitSelection[(currentLine)].Substring(0, 2) == Convert.ToString((i + 1)) + ".")
                    {
                        // If the substring of the current line equals a numbered list                value.. enumerate next line
                        this.txtCaptionEditor.SelectedText = "\n" + (i + 2) + ".  ";
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }
    }
private void btnOrdered\u单击(对象发送方,事件参数e)
{
string[]splitSelection=null;
//如果选择拆分选择否则拆分所有内容
如果(this.txtCaptionEditor.SelectionLength>0)
{
splitSelection=this.txtCaptionEditor.SelectedText.Replace(“\r\n”,“\n”).Split(“\n.tocharray()”);
}
其他的
{
splitSelection=this.txtCaptionEditor.Text.Replace(“\r\n”,“\n”).Split(“\n”.ToCharArray());
}
bool Exists=false;
for(int i=0;i=0)
{
//通过100个可能的数字循环匹配,您可以更高
//如果你认为你的名单可能超过100
对于(int i=0;i<100;i++)
{
if(splitSelection[(currentLine)].Substring(0,2)=Convert.ToString((i+1))+“)
{
//如果当前行的子字符串等于编号的列表值..枚举下一行
this.txtCaptionEditor.SelectedText=“\n”+(i+2)+”;
e、 抑制键