Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

How do I change the current path for OpenFileDialog ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Per - 31 Mar 2008 16:39 GMT
I want to change the current path in the OpenFileDialog in runtime. When the
user presses OK-button I'm checking the selected path if it's valid for my
application. If it's not valid I want to set the path to a valid one in the
FileOk-event and set the e.Cancel = true.
CSharper - 31 Mar 2008 17:33 GMT
> I want to change the current path in the OpenFileDialog in runtime. When the
> user presses OK-button I'm checking the selected path if it's valid for my
> application. If it's not valid I want to set the path to a valid one in the
> FileOk-event and set the e.Cancel = true.

Can't you use Reset() or InitialDirectory() method?
Set a valid initial directory and on invalid path perform a reset() on
the file dialog.
Per - 01 Apr 2008 09:15 GMT
> > I want to change the current path in the OpenFileDialog in runtime. When the
> > user presses OK-button I'm checking the selected path if it's valid for my
[quoted text clipped - 4 lines]
> Set a valid initial directory and on invalid path perform a reset() on
> the file dialog.

When I do this operation I want the dialog to update and to show the path
that I'm setting in the method that handles the FileOk-event. If I performs a
reset the dialog is not updated to the right path it stays on the wrong path.
CSharper - 01 Apr 2008 14:21 GMT
> > > I want to change the current path in the OpenFileDialog in runtime. When the
> > > user presses OK-button I'm checking the selected path if it's valid for my
[quoted text clipped - 8 lines]
> that I'm setting in the method that handles the FileOk-event. If I performs a
> reset the dialog is not updated to the right path it stays on the wrong path.

I see your problem.  Only way you can do this to make the open dialog
not to validate the the directory exists or not, this can be done by
either one of them. First one for directory validation and second one
is file validation.

openFileDialog1.CheckPathExists = false;
openFileDialog1.CheckFileExists = false;

by default both are set to true;
Per - 02 Apr 2008 13:47 GMT
This still did not help. The dialog do not uppdate and show the path that I
set in my event handler for the OK button.

> > > > I want to change the current path in the OpenFileDialog in runtime. When the
> > > > user presses OK-button I'm checking the selected path if it's valid for my
[quoted text clipped - 18 lines]
>
> by default both are set to true;
CSharper - 02 Apr 2008 15:17 GMT
> This still did not help. The dialog do not uppdate and show the path that I
> > > > > I want to change the current path in the OpenFileDialog in runtime. When the
[quoted text clipped - 19 lines]
>
> > by default both are set to true;

With the change I suggested, when user press ok button, even if the
file or directory is bad, it should close the fie dialog and you have
to validate the data yourself and if the data is bad show the file
dialog again.
My question, when you press the open button, did the window close or
did it show the error again?

If you have the code, please send it and let me have a look.
Thanks.
Per - 02 Apr 2008 16:06 GMT
This is not an option to close the dialog and then open it again.

Here is a code snippet of what I want to do.

       private void button1_Click(object sender, EventArgs e)
       {
           OpenFileDialog dlg = new OpenFileDialog();
           string fileName = "";

           dlg.FileName = "";
           dlg.Title = "Test";
           dlg.InitialDirectory = "C:\\Temp";
           dlg.CheckFileExists = true;
           dlg.CheckPathExists = true;

           // Setup event to validate if it's ok to proceed with open
command.
           dlg.FileOk += new CancelEventHandler(OnFileOk);

           if(dlg.ShowDialog() == DialogResult.OK)
           {
               fileName = dlg.FileName;                
           }
       }

       void OnFileOk(object sender, CancelEventArgs e)
       {
           OpenFileDialog dlg = sender as OpenFileDialog;
           
           string path = Path.GetDirectoryName(dlg.FileName);

           if (path.ToLower() != dlg.InitialDirectory.ToLower())
           {
               // Not a valid path.

               // Here I want to set the dialog to show the
InitalDirectory.                
               // I want the dialog to show the files under C:\Temp not the
files in
               // the right now selected directory that is not valid.      
                       

               // The Open command is canceled
               e.Cancel = true;                
           }
           else
           {
               // This is a valid path.

               // Proceed with the Open command.
               e.Cancel = false;                
           }
       }

> > This still did not help. The dialog do not uppdate and show the path that I
> > > > > > I want to change the current path in the OpenFileDialog in runtime.. When the
[quoted text clipped - 29 lines]
> If you have the code, please send it and let me have a look.
> Thanks.
CSharper - 02 Apr 2008 16:21 GMT
> This is not an option to close the dialog and then open it again.
>
[quoted text clipped - 80 lines]
> > If you have the code, please send it and let me have a look.
> > Thanks

Based on my limited knowledge I think it is not possible with
FileDialog if you do not want to close and open, may be you can create
a custom filedialog inherit from filedialog and in there, overwrite
the open method to handle this situation..

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.