Insert numbered list programmatically

3 posts, 1 answers
  1. Igor
    Igor avatar
    2 posts
    Member since:
    Mar 2013

    Posted 21 Mar 2013 Link to this post

    How can a numbered (or bulleted) list be inserted into a rich text box programmatically?
  2. Answer
    Jack
    Admin
    Jack avatar
    2335 posts

    Posted 26 Mar 2013 Link to this post

    Hello Igor,

    You can do this by using the RadRichTextBox API for text search and selection. Please consider the following code snippet:
    radRichTextBox1.Document.Selection.Clear();
    DocumentTextSearch search = new DocumentTextSearch(radRichTextBox1.Document);
    TextRange range = search.Find("start");
    TextRange range2 = search.Find("end", range.EndPosition);
    if (range != null && range2 != null)
    {
        this.radRichTextBox1.Document.Selection.AddSelectionStart(range.StartPosition);
        this.radRichTextBox1.Document.Selection.AddSelectionEnd(range2.EndPosition);
        this.radRichTextBox1.ChangeListStyle(DefaultListStyles.Bulleted);
    }

    I hope it helps.
     
    All the best,
    Jack
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Igor
    Igor avatar
    2 posts
    Member since:
    Mar 2013

    Posted 29 Mar 2013 Link to this post

    Thank you for your help. I managed to achieve this in the following way (VB.NET):

    For Each item As String In itemList
        radRichTextBox1.Insert(item & Environment.NewLine)
    Next
     
    'Format the item list as numbered list
    radRichTextBox1.Document.Selection.Clear()
    Dim search As New DocumentTextSearch(radRichTextBox1.Document)
    Dim range1 As TextRange = search.Find(itemList(0))
    Dim range2 As TextRange = search.Find(itemList(itemList.Count - 1), range1.EndPosition)
    If range1 IsNot Nothing And range2 IsNot Nothing Then
        radRichTextBox1.Document.Selection.AddSelectionStart(range1.StartPosition)
        radRichTextBox1.Document.Selection.AddSelectionEnd(range2.EndPosition)
        radRichTextBox1.ChangeListStyle(DefaultListStyles.Numbered)
    End If
    radRichTextBox1.Document.Selection.Clear()
Back to Top