By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Closing a file and the form

P: n/a
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
Share this Question
Share on Google+
24 Replies


P: n/a
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

P: n/a
* "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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* "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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* "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

P: n/a
* "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

P: n/a
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

P: n/a
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

P: n/a
* "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

P: n/a
* "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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* "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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.