New to Telerik UI for WinForms? Download free 30-day trial

How to Save Document in SyntaxEditor

Environment

Product Version Product Author
2020.1.218 RadSyntaxEditor for WinForms Desislava Yordanova

Description

RadSyntaxEditor is purposed to provide editing options for a document loaded in the control. A common requirement is to save the changes that have been made to the document.

Solution

This solution demonstrates how to get very easily the document's content by using the selection functionality and write the content to a file:


public RadForm1()
{
    InitializeComponent();
    using (StreamReader Reader = new StreamReader(@"..\..\Sample.cs"))
    {
        this.radSyntaxEditor1.Document = new TextDocument(Reader);
    }
}

private void radButton1_Click(object sender, EventArgs e)
{
    this.radSyntaxEditor1.SelectAll();
    string selectedText = this.radSyntaxEditor1.SyntaxEditorElement.Selection.GetSelectedText();
    string fileName = @"..\..\FILENAME.txt";
    if (!File.Exists(fileName))
    {
        File.Create(fileName).Close();
        using (StreamWriter sw = File.AppendText(fileName))
        {
            sw.WriteLine(selectedText);
        }
    }
    else
    {
        File.WriteAllText(fileName, string.Empty);
        using (StreamWriter sw = File.AppendText(fileName))
        {
            sw.WriteLine(selectedText);
        }
    }
}



Sub New()
    InitializeComponent()

    Using Reader As StreamReader = New StreamReader("..\..\Sample.cs")
        Me.RadSyntaxEditor1.Document = New TextDocument(Reader)
    End Using
End Sub
Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
    Me.RadSyntaxEditor1.SelectAll()
    Dim selectedText As String = Me.RadSyntaxEditor1.SyntaxEditorElement.Selection.GetSelectedText()
    Dim fileName As String = "..\..\FILENAME.txt"
    If Not File.Exists(fileName) Then
        File.Create(fileName).Close()
        Using sw As StreamWriter = File.AppendText(fileName)
            sw.WriteLine(selectedText)
        End Using
    Else
        File.WriteAllText(fileName, String.Empty)
        Using sw As StreamWriter = File.AppendText(fileName)
            sw.WriteLine(selectedText)
        End Using
    End If
End Sub    

See Also

In this article