Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am developing a rich text editor in wpf.its save as function work perfectly but i also want to provide save option which save the already open document with same name at same location like a notepad or word save option...
i am posting my source code here plz help me..

.Xaml file
<RichTextBox Height="420.724" Margin="6,66,6,5.999" Name="txtcreatelssn" Width="642" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" SpellCheck.IsEnabled="True"/>
                            <ToolBar Height="34" Margin="6,34,6,0" Name="toolBarMenu" VerticalAlignment="Top" >
                                <Button Click="CmdNew" >
                                    <Image Source="/MTDLL_TEACHER;component/Images/filenew.png" >
                                    </Image>
                                </Button>
                                <Button x:Name="Open" Click="Open1"  >
                                    <Image Source="/MTDLL_TEACHER;component/Images/fileopen.png" ></Image>
                                </Button>
                                <Button  x:Name="Save_As" Click="save_as">
                                    <Image Source="/MTDLL_TEACHER;component/Images/filesave.png"></Image>
                                </Button>
                                <Button  x:Name="Save" Click="save">
                                    <Image Source="/MTDLL_TEACHER;component/Images/filesaveas.png"></Image>
                                </Button>
                                <Button  Command="Cut" ToolTip="Cut">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editcut.png"></Image>
                                </Button>
                                <Button  Command="Copy" ToolTip="Copy">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editcopy.png"></Image>
                                </Button>
                                <Button  Command="Paste" ToolTip="Paste">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editpaste.png"></Image>
                                </Button>
                                <Button  Command="Undo" ToolTip="Undo">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editundo.png"></Image>
                                </Button>
                                <Button  Command="Redo" ToolTip="Redo">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editredo.png"></Image>
                                </Button>
                                <Button  Command="EditingCommands.ToggleBold" ToolTip="Bold">
                                    <TextBlock FontWeight="Bold" FontSize="15" Foreground="Black">B</TextBlock>
                                </Button>
                                <Button  Command="EditingCommands.ToggleItalic" ToolTip="Italic">
                                    <TextBlock FontStyle="Italic" FontWeight="Bold" FontSize="15" Foreground="Black">I</TextBlock>
                                </Button>
                                <Button  Command="EditingCommands.ToggleUnderline" ToolTip="Underline">
                                    <TextBlock TextDecorations="Underline" FontWeight="Bold" FontSize="15" Foreground="Black">U</TextBlock>
                                </Button>
                                <Button  Command="EditingCommands.AlignLeft" ToolTip="Align Left">
                                    <Image Source="/MTDLL_TEACHER;component/Images/paragraphleftjustify.png"/>
                                </Button>
                                <Button  Command="EditingCommands.AlignCenter" ToolTip="Align Center">
                                    <Image Source="/MTDLL_TEACHER;component/Images/paragraphcenterjustify.png"/>
                                </Button>
                                <Button  Command="EditingCommands.AlignRight" ToolTip="Align Right">
                                    <Image Source="/MTDLL_TEACHER;component/Images/paragraphrightjustify.png"/>
                                </Button>
                            </ToolBar>


.Xaml.cs file
#region wordfunctionality

       void CmdNew(object sender, RoutedEventArgs e)
       {
           txtcreatelssn.Document = new FlowDocument();
       }
       void Open1(object sender, RoutedEventArgs e)
       {
           System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
           openFile.Filter = "RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";

           if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               // Create a TextRange around the entire document.
               TextRange documentTextRange = new TextRange(txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);

               using (FileStream fs = File.Open(openFile.FileName, FileMode.Open))
               {
                   if (System.IO.Path.GetExtension(openFile.FileName).ToLower() == ".rtf")
                   {
                       documentTextRange.Load(fs, System.Windows.DataFormats.Rtf);
                   }
                   else
                   {
                       documentTextRange.Load(fs, System.Windows.DataFormats.Xaml);
                   }
               }

           }
       }

       void save_as(object sender, RoutedEventArgs e)
       {
           System.Windows.Forms.SaveFileDialog savefile = new System.Windows.Forms.SaveFileDialog();
           //System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
           savefile.Filter = "RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";

           if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               // Create a TextRange around the entire document.
               TextRange documentTextRange = new TextRange(
                   txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);
               MemoryStream ms = new MemoryStream();
               // If this file exists, it's overwritten.
               using (FileStream fs = File.Create(savefile.FileName))
               {
                   if (System.IO.Path.GetExtension(savefile.FileName).ToLower() == ".rtf")
                   {
                       documentTextRange.Save(fs,System.Windows.DataFormats.Rtf);
                   }
                   else
                   {
                       documentTextRange.Save(fs, System.Windows.DataFormats.Xaml);


                   }
               }

           }

       }//save_as

       #endregion wordfunctionality
Posted
Updated 28-Apr-11 19:08pm
v3

Use commands as these can be connected to shortcut keys. The commands then again can be bound to the buttons. Read some about it here: http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands[^] or here: http://msdn.microsoft.com/en-us/library/ms752308.aspx[^]

Best Regards,

-MRB
 
Share this answer
 
Have you tried associating CTRL+S with the menu command, that way the framework deals with the tedium for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900