473,404 Members | 2,179 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,404 software developers and data experts.

I need help with writing the code to save multiple variables to a text file...

I want to allow users of my program to click File>Save and have a save dialog open and allow them to save all of their input. here is the background of my program.

I have a program that allows students to calculate their grades as the class progresses. I want them to be able to type in their information and i already have the program written for them to be able to calculate everything via different types of work based on weights for homework tests etc etc.

I do not know how to write the code to allow variables to be saved to a text file defined by the user.

I also do not know how to write the code to allow that user to open that file and import all the data back into it.

If someone could write me sample code, based on saving these three variables i would greatly appreciate it.

variables hwPtsPoss (homework points possible), hwPtsAtt (homework points attained), and.... hwWeight (homework weight) all of these are defined as variable type double and standardly use digits and decimals only.

Thank you very much in advance to whomever can help.
Feb 8 '08 #1
1 1450
DrBunchman
979 Expert 512MB
Hi Josh,

The following code is a simple way to create a text file and add some text to it. Hopefully you can see how easy it would be to adapt this for your own uses.

The last bit with the OpenFileDialog is the bit that you can use to select an existing file and append text to it. The AppendAllText method will open the specified file, add the desired text to the end of the file and then close it. You can use the OpenFileDialog to display the contents of a text file in a rich text box or similar as well.

First you need to import the IO namespace.

Expand|Select|Wrap|Line Numbers
  1.  Imports System.IO
Then the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.  
  5. 'CREATE A STREAMWRITER OBJECT TO ACT AS A FILE POINTER 
  6. Dim fp As StreamWriter
  7. Dim sText As String = ""
  8.  
  9. Try
  10.  
  11. 'CREATE THE FILE
  12. fp = File.CreateText("C:\test.txt")
  13.  
  14. 'DEFINE THE TEXT WE ARE GOING TO ENTER
  15. sText += hwPtsAtt.ToString
  16. sText += " out of "
  17. sText += hwPtsPoss.ToString
  18. sText += " with a weighting of "
  19. sText += hwWeight.ToString
  20.  
  21. 'WRITE THE TEXT TO THE FILE
  22. fp.WriteLine(sText)
  23.  
  24. 'CLOSE THE FILE POINTER
  25. fp.Close()
  26.  
  27. Catch ex As Exception
  28. End Try
  29.  
  30. OpenFileDialog1.ShowDialog()
  31.  
  32. File.AppendAllText(OpenFileDialog1.FileName, "HELLO WORLD")
  33.  
  34.  
  35. End Sub
  36.  
  37.  
Have a play around with it and let me know how you get on,

Dr B
Feb 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Neil Trigger | last post by:
Is there a way of creating a seperate text file on a server every time a form is sent? -- ¿ Trigger ? http://www.magic2k.com/ http://www.oddmap.co.uk
4
by: winbatch | last post by:
Hi, I'm trying to play with classes and I'm having trouble with a theoretical problem. I've create a 'Log' class, who creates and writes to log files. I was able to overload the << operator...
2
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server...
0
by: Norm Wong | last post by:
If anyone is interested in using db2uext2 with Cygwin gcc compiler on Windows, I've modified the IBM provided sample with the attached file. There are two main modifications. The mkdir command...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: Rizwan | last post by:
I am writing this code for file upload in asp 3.0 on my webpage........ It is working on local system but when i want to upload an image on my website from client machine it is not working ..........
1
by: method | last post by:
Hi all i am usiing this code to write mulitiple values to registery but it only write one value. could any one help me make this code so that it write multiple values to registery. Currently it only...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
1
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.