Closing a file and the form

K

Kelly

Hey all -

I need a little more help. I don't quite know why my text file or form isn't
closing.

Short version - this program takes data entered into a textbox, user clicks
Save button, Save As dialog box pops open, user selects file to save to,
data *should* save to the file, file close, form close.

The way I have it written so far, the data *does* save to the file, however,
I have to first close the file and then re-open it to see the changes. I've
been playing with the code a little to get the form and file to close after
the user selects the file from the Save As dialog box and clicks OK.

I got help the other day from a few of you and was hoping for another kick
in the right direction...I'm not too familiar with the .NET syntax yet.

Here's the snippet of code I'm playing with...


Select Case sFileExtension.ToUpper
Case ".TXT"

Shell("notepad.exe " & sFileName, AppWinStyle.MaximizedFocus)

FileOpen(f, sFileName, OpenMode.Append)

PrintLine(f, sInfo)

'FileClose(f) ' doesn't work

'Form1.ActiveForm.Hide() ' doesn't work
 
K

Kelly

I should also mention that I tried using StreamWriter for this as well with
no luck.

-Kelly
 
H

Herfried K. Wagner [MVP]

* "Kelly said:
I need a little more help. I don't quite know why my text file or form isn't
closing.

Short version - this program takes data entered into a textbox, user clicks
Save button, Save As dialog box pops open, user selects file to save to,
data *should* save to the file, file close, form close.

The way I have it written so far, the data *does* save to the file, however,
I have to first close the file and then re-open it to see the changes.

That's because file access is buffered. So, there is no guarantee that
the data will be written to the file immediately. There is a guarantee
that it will be written, and a guarantee that it will be written in the
right order if there are successive calls to methods that write data to
the file.

If you are using 'System.IO.StreamWriter', you can call its 'Flush'
method to make the buffered data persistent.
 
K

Kelly

Ok, that part makes sense. The way it is now though, the file and form stay
open until I close the file manually. I was hoping to be able to close the
file after the data is sent to be written. How would I go about doing that?

Thanks again!

-Kelly
 
K

Kelly

Thanks! I'm trying this out now...

I did put the StreamWriter code back into my program to try it out (was in
one of the examples in the first link you sent me) and it didn't work.
Instead of appending the data, it overwrote the data. The text file and form
also did not close. Here's a snippet of the code:

If myFileDlog.ShowDialog() = DialogResult.OK Then

Dim sFileName As String = myFileDlog.FileName

Dim sw As IO.StreamWriter = New IO.StreamWriter(sFileName)

Dim sInfo As String = txtLName.Text & ", " & txtFName.Text

Dim sFileExtension As String = (System.IO.Path.GetExtension(sFileName))

Select Case sFileExtension.ToUpper

Case ".TXT"

Shell("notepad.exe " & sFileName, AppWinStyle.MaximizedFocus)

sw.Write(sInfo) ' I also tried
sw.WriteLine(sInfo) and got same results....

sw.Close()

........
 
H

Herfried K. Wagner [MVP]

* "Kelly said:
Ok, that part makes sense. The way it is now though, the file and form stay
open until I close the file manually. I was hoping to be able to close the
file after the data is sent to be written. How would I go about doing that?

I am not really sure if I understand what's not working for you. Is the
data written to the file when calling 'FileClose' on its file number?
 
K

Kelly

The data is being written to the file ok. But, when I choose the file to
save to from the dialog box, the file stays open as does the form. I was
hoping to get the file (and form) to close once the user had chosen the file
they wanted the data saved to. (I hope that made sense).

Like I said, I can't get the file or form to close in the program. The file
stays open, and I manually close it (the form closes once I close the file
manually). I open the file up, and my data is there.

-Kelly
 
O

One Handed Man \( OHM - Terry Burns \)

What do you mean 'Manually Close it' ?



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Kelly said:
The data is being written to the file ok. But, when I choose the file to
save to from the dialog box, the file stays open as does the form. I was
hoping to get the file (and form) to close once the user had chosen the file
they wanted the data saved to. (I hope that made sense).

Like I said, I can't get the file or form to close in the program. The file
stays open, and I manually close it (the form closes once I close the file
manually). I open the file up, and my data is there.

-Kelly
 
K

Kelly

I have to close the file myself. The program seems to be running, because
the form stays open, but I have to click the X at the top right of the file
to close it because the program isn't closing it. Once I've closed it like
that, the form closes. ??

-Kelly

One Handed Man ( OHM - Terry Burns ) said:
What do you mean 'Manually Close it' ?



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
H

Herfried K. Wagner [MVP]

* "Kelly said:
The data is being written to the file ok. But, when I choose the file to
save to from the dialog box, the file stays open as does the form. I was
hoping to get the file (and form) to close once the user had chosen the file
they wanted the data saved to. (I hope that made sense).

The file save dialog stays open or the file is not closed? Did you
place breakpoints in your code?
Like I said, I can't get the file or form to close in the program. The file
stays open, and I manually close it (the form closes once I close the file
manually). I open the file up, and my data is there.

How do you manually close the file?
 
H

Herfried K. Wagner [MVP]

* "Kelly said:
I have to close the file myself. The program seems to be running, because
the form stays open, but I have to click the X at the top right of the file
to close it because the program isn't closing it. Once I've closed it like
that, the form closes. ??

A file doesn't have a "X" button. Are you talking about a form that
displays the file?
 
K

Kelly

Um, the notepad file is what I closed. The X button next to the min/max
buttons...?

-Kelly
 
K

Kelly

No, the file save dialog closes once I choose my file (it's a notepad txt
file). The actual file (notepad) is what doesn't close.

The program only consists of one form. The form has a few text boxes and a
command button to save the info. The user enters the info into the text
boxes, clicks my Save command button, and the File save dialog box comes up.
The user browses to the file they want the info saved to (the notepad file
in my case) and clicks the OK button on the file save dialog box.

From there, the file save dialog box closes and the file I chose (notepad
file) opens up. The form and the file are both left open. I'd like for them
both to close (program to end, file saved and closed) but I can't get the
program to do that. I have to actually close the file myself (the X button
of my notepad file) to get the file and form to close.

-Kelly
 
H

Herfried K. Wagner [MVP]

* "Kelly said:
Um, the notepad file is what I closed. The X button next to the min/max
buttons...?

I missed that you launch Notepad...

Why do you open the file using notepad? Can you describe what exactly
you want to archieve?
 
H

Herfried K. Wagner [MVP]

* "Kelly said:
The program only consists of one form. The form has a few text boxes and a
command button to save the info. The user enters the info into the text
boxes, clicks my Save command button, and the File save dialog box comes up.
The user browses to the file they want the info saved to (the notepad file
in my case) and clicks the OK button on the file save dialog box.

From there, the file save dialog box closes and the file I chose (notepad
file) opens up. The form and the file are both left open. I'd like for them
both to close (program to end, file saved and closed) but I can't get the
program to do that. I have to actually close the file myself (the X button
of my notepad file) to get the file and form to close.

So, why do you start notepad at all?
 
K

Kelly

I just read my post and I hope it didn't come across as snarky or rude. :)
Not what I meant to do if it did.

I figured that I'd have to launch Notepad.exe in order to open the file (if
it was a .txt file anyway). Is there another way to open a simple txt file?
(I'm also going to try this with .xls and .doc files in the same way.)

I'm really new at .NET and we just started using it here at work (from vb6)
so I thought I'd start writing a few programs to get a feel for the new
language. All I'm really trying to do is create a csv file.

I figured I could have the user input the data (in the txtboxes) and have
them pick which type of file they'd like to save it to (.doc, .txt. or .xls)
and then save the data. For right now, I'm only testing this for txt files
and figured I'd have to launch Notepad.exe to get the txt files to open at
all.

The problem is, I'm unclear of how to close the file once the user has
selected it to save to, you know? They should be able to browse to the file,
"test.txt", choose it, and click OK (from the file save dialog).

I really don't even need for the user to SEE the file open up at all. All
that really needs to happen is the user selects the file, clicks OK and it's
done. But, my form (with the txtboxes and cmd button) and the file they
selected (test.txt) stays open.

-Kelly
 
K

Kelly

LOL...well, I was told I had to launch Notepad.exe to get the txt file to
open. So, I looked up the Shell command and implemented it into my code.
Since the file opened up, I assumed I was doing it correctly.

Is that part not needed?

-Kelly
 
H

Herfried K. Wagner [MVP]

* "Kelly said:
LOL...well, I was told I had to launch Notepad.exe to get the txt file to
open. So, I looked up the Shell command and implemented it into my code.
Since the file opened up, I assumed I was doing it correctly.

Is that part not needed?

I think you misunderstood what "opening a file" means. 'FileOpen' will
already open the file for reading or writing. Simply remove the
'Shell(...)' line :).
 
H

Herfried K. Wagner [MVP]

Kelly,

* "Kelly said:
I just read my post and I hope it didn't come across as snarky or rude. :)

You didn't.
I figured that I'd have to launch Notepad.exe in order to open the file (if
it was a .txt file anyway). Is there another way to open a simple txt file?
(I'm also going to try this with .xls and .doc files in the same way.)

The meaning of the term "open" in relation to files is overloaded:

(1) Opening a file means that you start an application that is able to
read/process/display/edit the file. In this case, the /user/ opens
the file, for example, by doubleclicking it in Explorer. This can
be done in VB using the 'Shell' command too. This command will
launch an application with a certain file.

(2) Opening a file from the developer point of view means to perform
steps that are necessary to access the data stored in a file and/or
make it ready for reading data from the file and/or writing data to
the file. In VB.NET, this can be done using 'FileOpen', etc., or
the 'System.IO.StreamWriter'/'System.IO.StreamReader' class.

I assume that you want to do (1).
I'm really new at .NET and we just started using it here at work (from vb6)
so I thought I'd start writing a few programs to get a feel for the new
language. All I'm really trying to do is create a csv file.

There is everything OK with that. Glad to see that people are using
VB.NET.
I figured I could have the user input the data (in the txtboxes) and have
them pick which type of file they'd like to save it to (.doc, .txt. or .xls)
and then save the data. For right now, I'm only testing this for txt files
and figured I'd have to launch Notepad.exe to get the txt files to open at
all.

Text files can be simply written using 'FileOpen', etc., or the
'Stream*' classes. For Excel files and other binary formats, it's not
as easy because often the formats are not documented. In these cases,
Office automation can be used to write the data to the file.
The problem is, I'm unclear of how to close the file once the user has
selected it to save to, you know? They should be able to browse to the file,
"test.txt", choose it, and click OK (from the file save dialog).
OK.

I really don't even need for the user to SEE the file open up at all. All
that really needs to happen is the user selects the file, clicks OK and it's
done. But, my form (with the txtboxes and cmd button) and the file they
selected (test.txt) stays open.

For simple text files, skip the 'Shell(...)' line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top