Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbered list on Richtextbox

I'm trying to add numbered list functionality to a text editor. RichTextbox already provides the SelectionBullet property to change a selection to a bulleted list. But i was unable to find a similar property to generate numbered list. Is there any standard way to create a numbered list on Richtextbox. If not, i would have to implement it myself so code snips that could help me do that will help, Thank you.

like image 262
yohannist Avatar asked Nov 02 '12 15:11

yohannist


2 Answers

I know that a link is not gernerally accepted as a good answer, however the article RichTextBox with Search Line Numbering, Bulleting, Printing, Searching Support on CodeProject could probably help you out quite a bit with what you are looking for.

In this article, the author extends the RichTextBox control into something that can do what you are asking (and more), plus the code is posted there for all to see.

like image 69
iMortalitySX Avatar answered Oct 24 '22 03:10

iMortalitySX


Well, i implemented it as follows.

  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{}
        }
like image 1
yohannist Avatar answered Oct 24 '22 01:10

yohannist