Retrieving data from a file
Retrieving data from a file
Retrieving data from a file
Retrieving data from a file
Retrieving data from a file
Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file
Retrieving data from a file Retrieving data from a file
Retrieving data from a file
Go Back  Xtreme Visual Basic Talk > > > Retrieving data from a file


Reply
 
Thread Tools Display Modes
  #1  
Old 08-22-2004, 09:18 PM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default Retrieving data from a file


My programs saves into a textfile "results".

Upon the program starting, I want the info from this text file"results" to repopulate the textboxes and checkboxes that they correspond to.

I have created two versions with streamwriter, but neither worked. Please help.
Reply With Quote
  #2  
Old 08-22-2004, 10:02 PM
Dennis DVR's Avatar
Dennis DVRRetrieving data from a file Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,576
Default

you need to show us your code and you need to tell us the format of your textfile but it would be better if you will give us example of data in your textfile
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper
Reply With Quote
  #3  
Old 08-25-2004, 11:41 PM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

There really isn't code yet to show. It is just 7 text boxes and 15 checkboxes. The text boxes hold a number. I would like it so when the program is started all the data repopulates the corresponding box. For example: I place a number in the text box to show how many results I found and then the checkbox get checked to show that I've completed it.
Reply With Quote
  #4  
Old 08-26-2004, 03:21 AM
Dennis DVR's Avatar
Dennis DVRRetrieving data from a file Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,576
Default

Quote:
Originally Posted by riehmc
There really isn't code yet to show. It is just 7 text boxes and 15 checkboxes. The text boxes hold a number. I would like it so when the program is started all the data repopulates the corresponding box. For example: I place a number in the text box to show how many results I found and then the checkbox get checked to show that I've completed it.
but you said on your first post that you have created 2 version of streamwriter but neither worked.

ok, but how about the format of your text you need to show us some example of your data in the textfile so we can give you an approriate answer.

and also what will happen to other textboxes if you just want to display the number of records found?
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper
Reply With Quote
  #5  
Old 08-26-2004, 07:49 AM
MKoslof's Avatar
MKoslofRetrieving data from a file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

On the form load event you want to instatiate a StreamReader. Read until EOF. As you read in each line, you assign these values to an array list or string, or pass the line value directly to the .text property of your text box, since you are just reading in strings. Now your check boxes would depend. What data return would these represent, bit (true/false)? How does this correlate with your text file data?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #6  
Old 08-26-2004, 08:53 PM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

This is what I started with, it saves to the file but I don't know how to bring them back to their assigned positions when I open the file. The checkboxes are just true and false.

Code:
Private Sub mnuFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click 'Saves the results to a file Dim datResults As New StreamWriter("C:\\Power Tune\\Results.txt") datResults.WriteLine(chkMsconfig) datResults.WriteLine(chkCleanup) datResults.WriteLine(chkTemp) datResults.WriteLine(radNever) datResults.WriteLine(radDialIf) datResults.WriteLine(radAlways) datResults.WriteLine(chkFirewall) datResults.WriteLine(chkWinsockFix) datResults.WriteLine(chkLspFix) datResults.WriteLine(txtAdaware.Text) datResults.WriteLine(txtSpybot.Text) datResults.WriteLine(txtAboutBuster.Text) datResults.WriteLine(txtShredder.Text) datResults.WriteLine(txtRemove.Text) datResults.WriteLine(txtHijack.Text) datResults.WriteLine(txtHousecall.Text) datResults.WriteLine(chkDefrag) datResults.WriteLine(chkRam) datResults.WriteLine(chkHardDrive) datResults.WriteLine(chkVideoCard) datResults.WriteLine(chkCdrom) datResults.WriteLine(chkAntivirusPurchase) datResults.WriteLine(chkFirewallPurchase) datResults.WriteLine(chkDefrag) datResults.WriteLine(chkScanDisk) datResults.WriteLine(chkUpdates) datResults.Close() End Sub

Code:
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click 'Opens the file 'Dim datResults As New StreamWriter("C:\CompUSA\PowerTune.txt") 'Try 'txtAdaware.Text = datResults.ReadLine() 'txtSpybot.Text = datResults.ReadLine() 'txtShredder.Text = datResults.ReadLine() 'txtRemove.Text = datResults.ReadLine() 'txtHijack.Text = datResults.ReadLine() 'txtHousecall.Text = datResults.ReadLine() 'Catch 'MessageBox.Show("File does not exist") 'End Try 'Declare any variables Dim sFileName As String = Application.StartupPath & ("C:\\Power Tune\\Results.txt") Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read) 'Create the StreamReader and associate the filestream with it Dim myReader As New System.IO.StreamReader(myFileStream) 'Read the entire text, and set it to a string Dim sFileContents As String = myReader.ReadToEnd() 'Print it to the textbox txtAdaware.Text = sFileContents txtSpybot.Text = sFileContents txtAdaware.Text = sFileContents txtShredder.Text = sFileContents txtRemove.Text = sFileContents txtHijack.Text = sFileContents txtHousecall.Text = sFileContents 'Close everything when you are finished myReader.Close() myFileStream.Close() End Sub
Reply With Quote
  #7  
Old 08-27-2004, 11:20 PM
Dennis DVR's Avatar
Dennis DVRRetrieving data from a file Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,576
Default

Quote:
Originally Posted by riehmc
This is what I started with, it saves to the file but I don't know how to bring them back to their assigned positions when I open the file. The checkboxes are just true and false.
it is true and false because you are just saving the value not the text/caption of checkbox and radiobutton.

and also why are you reading the entire file in one shot and assign the value to a textbox? if you want to get the approriate value of each textboxes you will need to loop through the file and read each line using Readline() function as already been mentioned by MK but you have to remember that assigning each line to textboxes or checkboxes it should be in order the way you save the value in your textfile, so in your case you have first save the chkMsConfig,chkCleanup...etc..
Code:
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click Dim sFileName As String = Application.StartupPath & ("C:\\Power Tune\\Results.txt") Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read) 'Create the StreamReader and associate the filestream with it Dim myReader As New System.IO.StreamReader(myFileStream) chkMsConfig.Value = myReader.ReadLine() 'Read first line 'assign the value to other control here myReader.Close() myFileStream.Close() End Sub
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper
Reply With Quote
  #8  
Old 08-28-2004, 01:56 PM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

I see where my mistake was. Thanks.

Now if I try to save the file if doesn't save anything. What happened?
Code:
Private Sub mnuFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click 'Saves the results to a file Dim datResults As New StreamWriter("C:\Power Tune\Results.txt") datResults.WriteLine(chkMsconfig) datResults.WriteLine(chkCleanup) datResults.WriteLine(txtAdaware.Text) datResults.Close() End Sub
But, what value do I use for the chkboxes? I get an error that "option strict on disallows conversions from string to boolean on the checkboxes. The textboxes are fine.
Code:
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click Dim sFileName As String = Application.StartupPath & ("C:\Power Tune\Results.txt") Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read) 'Create the StreamReader and associate the filestream with it Dim myReader As New System.IO.StreamReader(myFileStream) chkMsconfig.Checked = myReader.ReadLine() chkCleanup.Checked = myReader.ReadLine() txtAdaware.Text = myReader.ReadLine() myReader.Close() myFileStream.Close()

Last edited by riehmc; 08-28-2004 at 02:38 PM.
Reply With Quote
  #9  
Old 08-28-2004, 09:01 PM
MKoslof's Avatar
MKoslofRetrieving data from a file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Regarding your first question..if you are creating a new text file, after writing your lines to file, you need to flush it before closing it:

Code:
datResults.Flush() datResults.Close


OK, this is confusing . The ReadLine method should be assigned to a string object. A check box value is typically a bit value..this can not be assigned the value of the readLine method. Are you trying to assign the check box a specific VALUE within the line returned? Why would you be reading a text file into a check box?

A check box will take a boolean value..so how does this relate to reading in data from your text file?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #10  
Old 08-28-2004, 10:39 PM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

In my program there are checkboxes that show the which steps have been completed. I was trying to get my program to save everytime it was closed, then upon restarting the program, all the checkboxes of the steps that I've already completed would be rechecked.

So unless there is another file that can store the value of a checkbox..........
Reply With Quote
  #11  
Old 08-29-2004, 09:05 AM
MKoslof's Avatar
MKoslofRetrieving data from a file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Again, you can't use a check box to read in a text file. Why don't you just read in the text file normally, using a string or array object, and then change the value of the check box based on what value is returned.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #12  
Old 08-29-2004, 10:06 AM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

I'll do anything to get the checkboxes to work. Can I use the same text file to do it? How do I do it?

Code:
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click Dim sFileName As String = Application.StartupPath & ("C:\Power Tune\Results.txt") Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read) 'Create the StreamReader and associate the filestream with it Dim myReader As New System.IO.StreamReader(myFileStream) chkMsconfig.Checked = myReader.ReadLine() chkCleanup.Checked = myReader.ReadLine() txtAdaware.Text = myReader.ReadLine() myReader.Close() myFileStream.Close()
Reply With Quote
  #13  
Old 08-29-2004, 10:14 AM
MKoslof's Avatar
MKoslofRetrieving data from a file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK lets go through this step by step: Let's start here:

Code:
Dim sw as StreamReader Dim s as string sw = New StreamReader("C:\FileName.txt") Do If Not s is Nothing Then s = sw.ReadLine 'now do something with s End If Loop until s is Nothing

Now we are reading in each line of the text file..so with each iteration, s equals each complete line of data. Now, within this data, what will signify "true/false"..what is relevant to the check box? What does this data have to do with your check box? What are we looking for within this line of text?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #14  
Old 08-29-2004, 10:20 AM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

true or false is the only thing relevant
Reply With Quote
  #15  
Old 08-29-2004, 10:32 AM
MKoslof's Avatar
MKoslofRetrieving data from a file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Right, but maybe you are not understanding my point. Now we have a variable called "s" this contains all the data within the returned row. What is PRESENT within this row..is there a value "true" or "false". How is the text file structured. After reading in each line, if I do a messagebox.Show(s) what is returned to me..what value will I see?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #16  
Old 08-29-2004, 10:36 AM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

Code:
Dim sw As StreamReader Dim s As String sw = New StreamReader("C:\FileName.txt") Do If Not s Is Nothing Then s = sw.ReadLine 'now do something with s s = True 'Is this what you mean????????????? End If Loop Until s Is Nothing
Reply With Quote
  #17  
Old 08-29-2004, 10:38 AM
MKoslof's Avatar
MKoslofRetrieving data from a file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

No. Let me try to explain one more time.

You have a text file. Let's call it "C:\FileName.txt". Within this file you have lines of data. Open up your text file...what is present on each line. Tell me the data that resides on the text file. S is a string and it is reading in each line. S is not equal true and it never will be. Just please open up the text file and post a few lines, what is in this text file.

Let say a line reads like this "Mkoslof, VBRocks, DingDong". When you read this line in, it has NOTHING do with with your check box. Your check box is looking for a bitwise return. If we need to EVALUATE the data in this line and then set your checkbox to true based on a VALUE WITHIN IT we can..but you can't just assign your check box to the readline return.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #18  
Old 08-29-2004, 11:04 AM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

This is the text file. As you can see the first 6 are checkboxes and the next 7 are textboxes and so on.

System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 0
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 0
1
2
3
4
5
6
7
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 1
System.Windows.Forms.CheckBox, CheckState: 0
System.Windows.Forms.CheckBox, CheckState: 1
Reply With Quote
  #19  
Old 08-29-2004, 11:08 AM
MKoslof's Avatar
MKoslofRetrieving data from a file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

What? Please attach your text file as a .zip attachment so I can look at it. Why are you doing this? Even if that is your text, line by line, this is returning a text string. This means nothing to your check box control.

So a text file line reads like this?

"System.Windows.Forms.CheckBox, CheckState: 0"

If you are trying to physically store settings like this you should be using a resource file..or embedding this type of information within your assembly.

If you want to keep this format you would have to read in each line and split at the first comma. Then you would split or get to the index of your ":" character. Now, get the value that comes after 0,1. Then evaluate this value..and act accordingly (if 0 or 1 choose either true or false for the check box value)
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown

Last edited by MKoslof; 08-29-2004 at 11:14 AM.
Reply With Quote
  #20  
Old 08-29-2004, 11:19 AM
riehmc riehmc is offline
Regular
 
Join Date: Jul 2004
Posts: 95
Default

Here it is. I'll use any type of file to make this work, but I've only used a textfile in the past.
Attached Files
File Type: zip Powertune.zip (205 Bytes, 9 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Retrieving data from a file
Retrieving data from a file
Retrieving data from a file Retrieving data from a file
Retrieving data from a file
Retrieving data from a file
Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file Retrieving data from a file
Retrieving data from a file
Retrieving data from a file
 
Retrieving data from a file
Retrieving data from a file
 
-->