Load or Save the Content of a RichTextBox : RichTextBox « Windows Presentation Foundation « VB.Net






Load or Save the Content of a RichTextBox

Load or Save the Content of a RichTextBox
   
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="300" Width="300">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Button Content="_New" Name="btnNew" Click="btnNew_Click" />
            <Button Content="_Open" Name="btnOpen" Click="btnOpen_Click" />
            <Button Content="_Save" Name="btnSave" Click="btnSave_Click" />
        </StackPanel>
        <RichTextBox DockPanel.Dock="Bottom" Name="rtbTextBox1"
                     HorizontalScrollBarVisibility="Visible" 
                     VerticalScrollBarVisibility="Visible">
            <FlowDocument>
                <Paragraph>this is a test</Paragraph>
                <Paragraph>this is a test</Paragraph>
            </FlowDocument>
        </RichTextBox>
    </DockPanel>
</Window>

//File:Window.xaml.vb
Imports Microsoft.Win32
Imports System
Imports System.IO
Imports System.Windows
Imports System.Windows.Documents
Imports System.Windows.Markup

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Private currentFileName As [String] = [String].Empty

    Public Sub New()
      InitializeComponent()
    End Sub

    Private Sub btnOpen_Click(sender As Object, e As RoutedEventArgs)
      Dim dialog As New OpenFileDialog()
      dialog.FileName = currentFileName
      dialog.Filter = "XAML Files (*.xaml)|*.xaml"

      If dialog.ShowDialog() = True Then
        currentFileName = dialog.FileName
        If True Then
          Using stream As FileStream = File.Open(currentFileName, FileMode.Open)
            Dim doc As FlowDocument = TryCast(XamlReader.Load(stream), FlowDocument)
            If doc Is Nothing Then
              MessageBox.Show("Could not load document.", Title)
            Else
              rtbTextBox1.Document = doc
            End If
          End Using
        End If
      End If
    End Sub
    Private Sub btnNew_Click(sender As Object, e As RoutedEventArgs)
      rtbTextBox1.Document = New FlowDocument()
      currentFileName = [String].Empty
    End Sub
    Private Sub btnSave_Click(sender As Object, e As RoutedEventArgs)
      Dim dialog As New SaveFileDialog()
      dialog.FileName = currentFileName
      dialog.Filter = "XAML Files (*.xaml)|*.xaml"
      If dialog.ShowDialog() = True Then
        currentFileName = dialog.FileName
        Using stream As FileStream = File.Open(currentFileName, FileMode.Create)
          XamlWriter.Save(rtbTextBox1.Document, stream)
        End Using
      End If
    End Sub
  End Class
End Namespace

   
    
    
  








Related examples in the same category

1.Programmatically Insert Text into a RichTextBoxProgrammatically Insert Text into a RichTextBox
2.Get selected text from RichTextBox by using RichTextBox.Selection.TextGet selected text from RichTextBox by using RichTextBox.Selection.Text
3.Get Caret Position in a RichTextBox by using RichTextBox.CaretPosition.GetPositionAtOffsetGet Caret Position in a RichTextBox by using RichTextBox.CaretPosition.GetPositionAtOffset
4.Get the content from the rich text box.Get the content from the rich text box.
5.Add / Remove TextChanged event for RichTextBoxAdd / Remove TextChanged event for RichTextBox
6.Clear any formatting applied to the text with TextRange.ClearAllProperties();Clear any formatting applied to the text with TextRange.ClearAllProperties();
7.Apply Syntax Highlighting in a Text ControlApply Syntax Highlighting in a Text Control
8.Select all text from RichTextBox and cutSelect all text from RichTextBox and cut
9.Use Application Command to edit RichTextBoxUse Application Command to edit RichTextBox
10.Display a Password Entry Box and get the inputDisplay a Password Entry Box and get the input