|
-
Nov 22nd, 2008, 02:15 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] Reverse the process.
Hello below is a sub that writes a bunch of information to a file.
Basicly it is in a way a 'save' method for my application. I now want to create a 'load' so to do this i will have to reverse this process, how may i do this? Note that since i am recording info through a for each loop the loops and the means of how information is retrieved must be in the same order.
Here is my code:
vb Code:
'Here:
Private Sub AddCustomer()
Dim Customer_Info As New List(Of String)
For Each ctrl As Control In Me.CustomerEditArea.Controls
If Not (TypeOf (ctrl) Is Label) And Not (TypeOf (ctrl) Is CheckBox) And Not (TypeOf (ctrl) Is DateTimePicker) Then
Customer_Info.Add(ctrl.Text.Trim)
End If
Next
Customer_Info.Add(DateSelecter.Value.ToString)
For Each chk As CheckBox In Me.CustomerEditArea.Controls
If TypeOf (chk) Is CheckBox Then
Customer_Info.Add(chk.CheckState.ToString)
End If
Next
Customer_Info.Add(lblsubtotal.Text)
Customer_Info.Add(lbltotal.Text)
IO.File.WriteAllLines(ProfileDirectory & txtfirstname.Text.Trim & txtlastname.Text.Trim & ".ERcr", Customer_Info.ToArray)
End Sub
-
Nov 22nd, 2008, 05:28 PM
#2
Frenzied Member
Re: [2005] Reverse the process.
First of all, I'd suggest that you try XML serialization, it should solve many of your worries with one stroke.
If you decide to stick with this method, try this:
Code:
Dim filepath As String '= file path here
Dim lines() As String = IO.File.ReadAllLines(filepath)
Dim i As Integer
For Each ctrl As Control In Me.CustomerEditArea.Controls
If Not (TypeOf (ctrl) Is Label) And Not (TypeOf (ctrl) Is CheckBox) And Not (TypeOf (ctrl) Is DateTimePicker) Then
ctrl.Text = lines(i)
i += 1
End If
Next
For Each chk As CheckBox In Me.CustomerEditArea.Controls
If TypeOf (chk) Is CheckBox Then
chk.CheckState = CType(lines(i), CheckState) 'This line will work for sure if you change the chk.CheckState.ToString to a CStr(chk.CheckState), otherwise I'm not sure
'If it doesn't and you don't want to change your code, you'll need to insert a Select Case block
i += 1
End If
Next
lblsubtotal.Text = lines(i)
lbltotal.Text = lines(i+1)
If the code above doesn't work, post the exception and I'll do my best to fix it.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Nov 22nd, 2008, 05:51 PM
#3
Thread Starter
Frenzied Member
Re: [2005] Reverse the process.
In the code you supplied you are missing line 9 of my code:
Customer_Info.Add(DateSelecter.Value.ToString)
How would i place that in? (between the first and second for each loop)
-
Nov 22nd, 2008, 06:35 PM
#4
Frenzied Member
Re: [2005] Reverse the process.
Hmmm... Is DateSelecter a DateTimePicker? I'm not exactly sure which date format will the ToString method return, that should be checked, but you'd use Date.Parse and set it to the Value property I think... Unfortunately, I haven't used a DateTimePicker and I have no idea which format will be used in ToString, as for some reason I cannot open the MSDN webpage... :S I'll try again in the morning, I need to get some sleep now...
Btw, this is exactly why I suggested using XML serialization... When serializing a class or structure, you'd keep a String as a String, a Date as a Date, an Enum as that same Enum and a List(Of T) as a List(Of T). You'd only have to worry about saving the data in the class and retrieving and using it afterwards...
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Nov 22nd, 2008, 07:17 PM
#5
Thread Starter
Frenzied Member
Re: [2005] Reverse the process.
Yes it is a DateTimePicker...
Then just wondering, how would XML serialization work?
-
Nov 23rd, 2008, 05:23 AM
#6
Frenzied Member
Re: [2005] Reverse the process.
It would be something like this:
Code:
Public Structure Data
Public texts As List(Of String)
Public checkboxes As List(Of CheckState)
Public dt As Date
Public total As String
Public subtotal As String
End Structure
'You'd populate an instance of this structure and then:
Public Sub Serialize(ByVal data As Data)
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(data)))
Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
serializer.Serialize(fs, items)
fs.Close()
End Sub
'When you want to recover the data:
Public Function Deserialize() As Data
Dim dat As Data
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(data)))
Dim sReader As New IO.StreamReader("test.xml")
dat = DirectCast(serializer.Deserialize(sReader), Data)
sReader.Close()
Return dat
End Function
You can also serialize a class instance, not only a structure. The only prerequisite is that the types you use must have parameterless constructors.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Nov 24th, 2008, 11:39 AM
#7
Frenzied Member
Re: [2005] Reverse the process.
So how do you like it? Have you decided on what to use?
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Nov 25th, 2008, 07:49 AM
#8
Thread Starter
Frenzied Member
Re: [2005] Reverse the process.
I think i might stick to post #2. Since it seems easier and works better with my code.
-
Nov 25th, 2008, 09:35 AM
#9
Frenzied Member
Re: [RESOLVED] [2005] Reverse the process.
As you wish. Then use the following to store and retrieve the date:
Code:
'This will give you the date as a String
Dim dat As New Date = Date.Now
dat.ToString(CultureInfo.InvariantCulture)
'And this will convert it back to a DateTime
Dim dateString As String = "5/1/2008 8:30:52 AM"
Dim dat As Date = Date.Parse(dateString, CultureInfo.InvariantCulture)
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Nov 25th, 2008, 05:17 PM
#10
Thread Starter
Frenzied Member
Re: [RESOLVED] [2005] Reverse the process.
-
Nov 26th, 2008, 08:45 AM
#11
-
Nov 28th, 2008, 04:49 PM
#12
Thread Starter
Frenzied Member
Re: [RESOLVED] [2005] Reverse the process.
if i were to write the dateselector.value on the first line then for loading couldnt i do this?
Code:
DateSelecter.Value = (Date.Parse(lines(0)))
Since for this one program i am using VS 2005 and Culture.. doesnt exist unless i have to import something..
-
Nov 28th, 2008, 04:57 PM
#13
Frenzied Member
Re: [RESOLVED] [2005] Reverse the process.
It's System.Globalization.Culture and it's an assembly from .Net 2.0 so it must exist in VS 2005.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Nov 28th, 2008, 04:58 PM
#14
Thread Starter
Frenzied Member
Re: [RESOLVED] [2005] Reverse the process.
ok ill check it, but just wondering then what i wrote will that work to or it will result in error?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|