473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Closing a file and the form

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
Nov 21 '05 #1
24 1910
I should also mention that I tried using StreamWriter for this as well with
no luck.

-Kelly

"Kelly" <ke**********@amerock.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #2
* "Kelly" <ke**********@amerock.com> 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/>
Nov 21 '05 #3
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
Nov 21 '05 #4
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]" <hi***************@gmx.at> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
* "Kelly" <ke**********@amerock.com> 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/>

Nov 21 '05 #5
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" <no**********@planet.nl> wrote in message
news:Oz****************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #6
* "Kelly" <ke**********@amerock.com> 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/>
Nov 21 '05 #7
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]" <hi***************@gmx.at> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
* "Kelly" <ke**********@amerock.com> 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/>

Nov 21 '05 #8
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" <ke**********@amerock.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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]" <hi***************@gmx.at> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
* "Kelly" <ke**********@amerock.com> 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/>


Nov 21 '05 #9
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:uw*************@TK2MSFTNGP10.phx.gbl...
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" <ke**********@amerock.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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]" <hi***************@gmx.at> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
* "Kelly" <ke**********@amerock.com> 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/>



Nov 21 '05 #10
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #11
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #12
Um, the notepad file is what I closed. The X button next to the min/max
buttons...?

-Kelly

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #13
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #14
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #15
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #16
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #17
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Ol**************@tk2msftngp13.phx.gbl...
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #18
Kelly,

* "Kelly" <ke**********@amerock.com> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #19
* "Kelly" <ke**********@amerock.com> scripsit:
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 :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #20
Ok, I took out the shell command and the file closes now. Yay! I had no idea
that was the problem. Thank you. I added the line:
formName.activeform.hide() to my code and it seems to be working great now.

Thanks for all of your help! :)

-Kelly

"Kelly" <ke**********@amerock.com> wrote in message
news:%2********************@tk2msftngp13.phx.gbl.. .
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Ol**************@tk2msftngp13.phx.gbl...
* "Kelly" <ke**********@amerock.com> scripsit:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #21
Thank you! I did it and it works great now! :)

-Kelly

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ec*************@tk2msftngp13.phx.gbl...
* "Kelly" <ke**********@amerock.com> scripsit:
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 :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #22
Thank you - you've been a great help! :)

-Kelly

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OV**************@TK2MSFTNGP09.phx.gbl...
Kelly,

* "Kelly" <ke**********@amerock.com> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #23
Kelly,
Is that part not needed?


No, as I said already as answer on your question yesterday. And asked you
today again why you did it.

Read the file using the streamreader into the textbox.
You can do that in one time with (copied partially from MSDN and than
changed, so there can be a lot of typos and it is just a kind of pseudo
sample)

Imports System
Imports System.IO
Imports System.Text

Private Sub FormLoad
Dim path As String = "c:\temp\MyTest.txt"
Try
Dim sr As StreamReader = New StreamReader(path)
'This allows you to do one Read operation.
myTextbox.text = sr.ReadToEnd())
sr.Close()
Catch e As Exception
Messagebox.show(e.tostring)
End Try
end sub
Private sub Abutton clicked
Try
File.Delete(path)
'Above is normally not the right way better is to save it with
'a tempory name and when it is ok to change the name (file.move)
Dim sw As StreamWriter = New StreamWriter(path)
sw.Write(Textbox.Text)
sw.Close()
Catch e As Exception
Messagebox.show(e.tostring)
End Try
End Sub
End Class
////

I hope this helps?

Cor
Nov 21 '05 #24
Ok, sorry. I didn't catch the part about taking the shell command out
yesterday. I was told by someone else I needed it and so never really
questioned that part of the code. I see the difference now though. I'm
learnin... :)

-Kelly

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:eE**************@TK2MSFTNGP11.phx.gbl...
Kelly,
Is that part not needed?
No, as I said already as answer on your question yesterday. And asked you
today again why you did it.

Read the file using the streamreader into the textbox.
You can do that in one time with (copied partially from MSDN and than
changed, so there can be a lot of typos and it is just a kind of pseudo
sample)

Imports System
Imports System.IO
Imports System.Text

Private Sub FormLoad
Dim path As String = "c:\temp\MyTest.txt"
Try
Dim sr As StreamReader = New StreamReader(path)
'This allows you to do one Read operation.
myTextbox.text = sr.ReadToEnd())
sr.Close()
Catch e As Exception
Messagebox.show(e.tostring)
End Try
end sub
Private sub Abutton clicked
Try
File.Delete(path)
'Above is normally not the right way better is to save it with
'a tempory name and when it is ok to change the name

(file.move) Dim sw As StreamWriter = New StreamWriter(path)
sw.Write(Textbox.Text)
sw.Close()
Catch e As Exception
Messagebox.show(e.tostring)
End Try
End Sub
End Class
////

I hope this helps?

Cor

Nov 21 '05 #25

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

Similar topics

5
by: Peter yeshew | last post by:
Is it possible to forbid closing the form through the File- Close menu ? On my form i have a command button called CmdDeleteInvoice. When this command button is visible ,i want to forbid the user...
0
by: Andrew | last post by:
I get a Null Reference Exception if I close a modeless form (that is, a form displayed using Show()) when a selection is made from a ComboxBox. If the form is modal (displayed using ShowDialog())...
3
by: Andrew | last post by:
I get a Null Reference Exception if I close a non-modal dialog (that is, a form opened with Show()) when a selection is made from a ComboBox. The error message refers to Unsafe Native Methods, but...
1
by: MuZZy | last post by:
HI, I have a user control, say with one textBox. I need the following: when the user closes form (either calling Form.Close, or pressing "X" or anyhow), UserControl copies a file from a...
1
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
11
by: Simon van Beek | last post by:
Dear reader, By opening an application I get always the main Access window with the closing cross in the above right corner. Is there a possibility to make this closing cross invisible? ...
2
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate...
4
Fr33dan
by: Fr33dan | last post by:
Hi, I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called. Here is my...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.