473,574 Members | 2,927 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 1937
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
2784
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
8140
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
28305
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
2196
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
2941
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
4220
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
2208
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
7805
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7726
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
8053
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
8237
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...
0
6454
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3741
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
3752
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2245
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.