472,995 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 software developers and data experts.

OpenFileDialog and SaveFileDialog

Hello!

The object called dlgOpenFile is of type OpenFileDialog.

In this example the eventhandler OnFileOpen is called when the Open File in
a menu is clicked.
Here we call the OpenFileDialog and select a filename to be opened and then
if the Open
button is selected we call the OpenFile method.

Now to my question: I just wonder when is the file opened. Is it at the same
time as
the static method File.ReadAllText(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

//Tony
Jul 8 '08 #1
3 2744
Thats correct. That functions opens the file, reads all the text from it and
closes it again. The OpenFileDialog just returns a file name to you by
default and doesnt open the file. It does have functions on it to Open the
file and return a stream but that would mean you would have to write the code
to read the contents of the file.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Tony Johansson" wrote:
Hello!

The object called dlgOpenFile is of type OpenFileDialog.

In this example the eventhandler OnFileOpen is called when the Open File in
a menu is clicked.
Here we call the OpenFileDialog and select a filename to be opened and then
if the Open
button is selected we call the OpenFile method.

Now to my question: I just wonder when is the file opened. Is it at the same
time as
the static method File.ReadAllText(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

//Tony
Jul 8 '08 #2
Neither OpenFileDialog, nor SaveFileDialog actually open a file. The
filename returned is for you to use in code after opening. ReadAllText will
open and close the file as part of its code.

"Tony Johansson" wrote:
Hello!

The object called dlgOpenFile is of type OpenFileDialog.

In this example the eventhandler OnFileOpen is called when the Open File in
a menu is clicked.
Here we call the OpenFileDialog and select a filename to be opened and then
if the Open
button is selected we call the OpenFile method.

Now to my question: I just wonder when is the file opened. Is it at the same
time as
the static method File.ReadAllText(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

//Tony
Jul 8 '08 #3
Hello Tony,

it is opend while during executing File.ReadAllText(filename);

The FileOpenDialog only returns the Filename (as string) of the
selected file an thus does not open it.
Jul 9 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Allen | last post by:
I have enocuntered something wierd. If I invoke an instance of the above dialog and elect to save the file in a directory where a file of that name does not exist, then the return is...
3
by: cider123 | last post by:
Everything I've read so far states you can't inherit or extend these dialogs because they're "sealed". I considered building my own custom control, but I haven't been able to find: 1) How to...
3
by: josh | last post by:
How do I make it actually save or open a file? It only opens the dialogs. What do I type to get it to save? Here's what I have so far: Public Class frmMainApp Inherits...
11
by: Rasmus Teglgaard | last post by:
Hi I need to know how to store the OpenfileDialog.OpenFile() in a memorystream. Do I need to do some kind of conversion before this works I can't write dim mem as new memorystream( mem =...
4
by: NickP | last post by:
Hi there, This is really crazy! 1. Make a folder 2. Put a text file in the folder 3. Run the following code... Dim pop As New OpenFileDialog Using pop
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
3
by: Martijn Mulder | last post by:
It strikes me that System.Windows.Forms.OpenFileDialog seems te 'remember' which directory it was in last, even when a new OpenFileDialog-object is created for every access to the file system....
4
by: Jollywg | last post by:
How do you use a path other than the initialdirectory in a savefiledialog in c#?? Thanks
8
by: Joe Duchtel | last post by:
Hello - I have the following code to detemine a file name when my application is saving a file. The problem is that if the file already exists and I select the Yes button in the "Do you want to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.