PC Review


Reply
Thread Tools Rate Thread

Closing a file and the form

 
 
Kelly
Guest
Posts: n/a
 
      19th Aug 2004
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


 
Reply With Quote
 
 
 
 
Kelly
Guest
Posts: n/a
 
      19th Aug 2004
I should also mention that I tried using StreamWriter for this as well with
no luck.

-Kelly



"Kelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      19th Aug 2004
* "Kelly" <(E-Mail Removed)> scripsit:
> 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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      19th Aug 2004
Kelly,

Have a look at the streamreader/writer class and tell us what that shell
notepad does in this code.

http://msdn.microsoft.com/library/de...ClassTopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps anyway?

Cor


 
Reply With Quote
 
Kelly
Guest
Posts: n/a
 
      19th Aug 2004
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

"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * "Kelly" <(E-Mail Removed)> scripsit:
> > 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.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



 
Reply With Quote
 
Kelly
Guest
Posts: n/a
 
      19th Aug 2004
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()

........


"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:Oz4%(E-Mail Removed)...
> Kelly,
>
> Have a look at the streamreader/writer class and tell us what that shell
> notepad does in this code.
>
>

http://msdn.microsoft.com/library/de...ClassTopic.asp
>
>

http://msdn.microsoft.com/library/de...classtopic.asp
>
> I hope this helps anyway?
>
> Cor
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      19th Aug 2004
* "Kelly" <(E-Mail Removed)> scripsit:
> 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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Kelly
Guest
Posts: n/a
 
      19th Aug 2004
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
"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * "Kelly" <(E-Mail Removed)> scripsit:
> > 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?
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      19th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:O9u$(E-Mail Removed)...
> 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
> "Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > * "Kelly" <(E-Mail Removed)> scripsit:
> > > 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?
> >
> > --
> > M S Herfried K. Wagner
> > M V P <URL:http://dotnet.mvps.org/>
> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

>
>



 
Reply With Quote
 
Kelly
Guest
Posts: n/a
 
      19th Aug 2004
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 )" <news.microsoft.com> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:O9u$(E-Mail Removed)...
> > 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
> > "Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > * "Kelly" <(E-Mail Removed)> scripsit:
> > > > 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?
> > >
> > > --
> > > M S Herfried K. Wagner
> > > M V P <URL:http://dotnet.mvps.org/>
> > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File Size Grows When Opening or Closing a Form In Access 2007 JCS Microsoft Access 2 14th Aug 2009 08:23 PM
Showing form , closing login form but not closing app, how Bob Microsoft VB .NET 3 21st Dec 2005 07:54 PM
Closing event in a MID Child form I don't know if the child form is closing or the main form is closing **Developer** Microsoft C# .NET 1 19th Oct 2005 04:51 PM
Stopping a form from closing in its Closing hander Tom Jones Microsoft Dot NET Framework Forms 2 26th Aug 2004 01:24 PM
Multiple OutputTo (Called on Closing Form) Fails on Closing Database John Andrews Microsoft Access Macros 3 21st May 2004 08:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:43 AM.