473,587 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Max imizedFocus)

FileOpen(f, sFileName, OpenMode.Append )

PrintLine(f, sInfo)

'FileClose(f) ' doesn't work

'Form1.ActiveFo rm.Hide() ' doesn't work
Nov 21 '05 #1
24 1940
I should also mention that I tried using StreamWriter for this as well with
no luck.

-Kelly

"Kelly" <ke**********@a merock.com> wrote in message
news:u7******** ******@TK2MSFTN GP11.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.Max imizedFocus)

FileOpen(f, sFileName, OpenMode.Append )

PrintLine(f, sInfo)

'FileClose(f) ' doesn't work

'Form1.ActiveFo rm.Hide() ' doesn't work

Nov 21 '05 #2
* "Kelly" <ke**********@a merock.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.Stre amWriter', 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******** ******@TK2MSFTN GP12.phx.gbl...
* "Kelly" <ke**********@a merock.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.Stre amWriter', 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.Show Dialog() = DialogResult.OK Then

Dim sFileName As String = myFileDlog.File Name

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(s FileName))

Select Case sFileExtension. ToUpper

Case ".TXT"

Shell("notepad. exe " & sFileName, AppWinStyle.Max imizedFocus)

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

sw.Close()

........
"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:Oz******** ********@TK2MSF TNGP09.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**********@a merock.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******** ******@TK2MSFTN GP09.phx.gbl...
* "Kelly" <ke**********@a merock.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**********@a merock.com> wrote in message
news:O9******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP09.phx.gbl...
* "Kelly" <ke**********@a merock.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******** *****@TK2MSFTNG P10.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**********@a merock.com> wrote in message
news:O9******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP09.phx.gbl...
* "Kelly" <ke**********@a merock.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

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

Similar topics

5
2787
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 from closing the form through the menu commands file-close.I want to make him click the button and not to allow him to do any other actions.And to...
0
3172
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()) or the selection is made from, say, a ListBox, no exception is thrown. I have included a simple example below. The error message refers to Unsafe...
3
8142
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 the code is 100% managed. The exception is not thrown if the dialog was modal (opened with ShowDialog()) or if the selection is made from, say, a...
1
28309
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 predefined folder to a folder in the textBox. UserControl doesn't have a "Closing" event, so i tried to override UserControl.Dispose() : //...
1
2197
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
4002
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms,...
11
2943
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? All the settings in the Start-up form under Tools are disabled but the closing cross in the above right corner is still visible.
2
4222
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 forms opening & closing. An example of this issue is as follows. I have a logon form open as the first thing. The main functional form opens when a...
4
2209
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 on _FormClosing method: private void CycleBox_FormClosing(object sender, FormClosingEventArgs e) { // Set the boolean flags...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.