Ticket Q237765
Visible to All Users
Duplicate

OpenFileDialog - Set initial directory

created 15 years ago

hi you guys changed my issue question into a suggestion without providing any help at all!! re Q237215. I need to know how to bypass the default functionality of the ribbon control open dialog so I can set the intial directory, I would have thought this was straight forward as it is easy to do with an open file dialog and makes no sense at all to be undoable.
Dim myFileDialog As OpenFileDialog = New OpenFileDialog()
        With myFileDialog
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 1
            .InitialDirectory = "C:"
            .Title = "Open File"
            .CheckFileExists = False
        End With

Answers approved by DevExpress Support

created 15 years ago (modified 9 years ago)

Hi Chris,
There is a simple way to resolve this problem. You should create your own class based on the XtraRichEdit control and override the LoadDocument method. Here is some sample code:

Visual Basic
.... Public Class MyRichEditControl Inherits RichEditControl Public Sub New() MyBase.New() End Sub Public Overrides Sub LoadDocument() Using myFileDialog As New OpenFileDialog() myFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" myFileDialog.FilterIndex = 1 myFileDialog.InitialDirectory = "C:\" myFileDialog.Title = "Open File" myFileDialog.CheckFileExists = False Dim result As DialogResult = myFileDialog.ShowDialog() If result = DialogResult.OK Then Me.LoadDocument(myFileDialog.FileName, DocumentFormat.PlainText) End If End Using 'base.LoadDocument(); End Sub ....

In addition, you can use this approach to customize the File save dialog. Here is some sample code:

Visual Basic
.... Public Class MyRichEditControl Inherits RichEditControl Public Sub New() MyBase.New() End Sub Public Overrides Sub SaveDocument() Using myFileDialog As New SaveFileDialog() myFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" myFileDialog.FilterIndex = 1 myFileDialog.InitialDirectory = "C:\" myFileDialog.CheckFileExists = False Dim result As DialogResult = myFileDialog.ShowDialog() If result = DialogResult.OK Then Me.SaveDocument(myFileDialog.FileName, DocumentFormat.PlainText) End If End Using End Sub Public Overrides Sub SaveDocumentAs() SaveDocument() End Sub End Class ....

Try this solution, and let us know whether it helps.
Thanks,
Elliot

Updated by Yulia

The code implemented in the overridden RichEditControl.LoadDocument method is executed when the RichEditControl.LoadDocument method is called in code behind. This code does not affect the "Open document" RichEditControl's command that can be invoked from the RichEditControl Ribbon or by pressing the "Ctrl+O" shortcut. To change the behavior of the built-in RichEditControl's command, it is necessary to substitute the default command with a custom one.

This approach is demonstrated in the How to replace standard XtraRichEdit command with your own custom command Code Example. The RichEditControl exposes the IRichEditCommandFactoryService interface that enables you to substitute the default command with your own custom command. First, create a command class inherited from the XtraRichEdit.Commands.LoadDocumentCommand command. Then, override the ExecuteCore method to call the "OpenFileDialog" with the predefined settings.

As for saving the document, you can customize a default path to the output document used in the "SaveFileDialog" via the DocumentSaveOptions.CurrentFileName property.

    Comments (3)

      thanks

        Sorry, is this no more working ?

        I receive a MissingManifestRessourceException. If it should still work, please provide a small vb sample.

        BTW, savedocument seems to be changed, too

        DevExpress Support Team 9 years ago

          Hello Chris,

          The code implemented in the overridden RichEditControl.LoadDocument method is executed when the RichEditControl.LoadDocument method is called in code behind. This code does not affect the "Open document" RichEditControl's command that can be invoked from the RichEditControl Ribbon or by pressing the "Ctrl+O" shortcut. To change the behavior of the built-in RichEditControl's command, it is necessary to substitute the default command with a custom one.

          This approach is demonstrated in the How to replace standard XtraRichEdit command with your own custom command Code Example. The RichEditControl exposes the IRichEditCommandFactoryService interface that enables you to substitute the default command with your own custom command. First, create a command class inherited from the XtraRichEdit.Commands.LoadDocumentCommand command. Then, override the ExecuteCore method to call the "OpenFileDialog" with the predefined settings.

          As for saving the document, you can customize a default path to the output document used in the "SaveFileDialog" via the DocumentSaveOptions.CurrentFileName property.
          Should you have any questions or concerns while implementing this approach, let me know.

          I have updated our previous answer with this solution.

          >>I receive a MissingManifestRessourceException.<<

          I could not replicate this exception on my side. I recommend you review the What does MissingManifestResourceException mean and how to fix it? article where this issue is described in detail. I hope the information provided there will help you overcome the problem.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.