|
-
Jan 24th, 2011, 03:41 PM
#1
Thread Starter
Junior Member
redacted
Last edited by Reboh79; Jun 13th, 2015 at 06:11 AM.
Reason: Redacting content
-
Jan 24th, 2011, 04:06 PM
#2
Re: How can I retrieve data from a specific place in text?
try this:
vb Code:
Public Class Form1
Dim firstParts() As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
IO.File.WriteAllLines("filename", New String() {String.Join("", New String() {firstParts(0), "=", TextBox1.Text}), String.Join("", New String() {firstParts(1), "=", TextBox2.Text})})
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines() As String = IO.File.ReadAllLines("filename")
ReDim firstParts(lines.GetUpperBound(0))
Dim parts() As String = lines(0).Split("="c)
firstParts(0) = parts(0)
TextBox1.Text = parts(1)
parts = lines(1).Split("="c)
firstParts(1) = parts(0)
TextBox2.Text = parts(1)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 24th, 2011, 04:11 PM
#3
Re: How can I retrieve data from a specific place in text?
If you have any control over the format of this file, I would suggest that you consider something like XML. After all, XML structures a text file with nodes and data associated with those nodes. Your description suggests that you don't want to be making random changes to the file, but to actually only change the values associated with certain tags. That's much more aligned with XML than pure text files.
My usual boring signature: Nothing
 
-
Jan 24th, 2011, 04:12 PM
#4
Thread Starter
Junior Member
Last edited by Reboh79; Jun 13th, 2015 at 06:10 AM.
Reason: Redacting content
-
Jan 24th, 2011, 04:22 PM
#5
Re: How can I retrieve data from a specific place in text?
vb Code:
Public Class Form1
'array to store first parts of lines
Dim firstParts() As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'save changes. WriteAllLines saves a string array, so i created a string array, + rejoined the lines
IO.File.WriteAllLines("filename", New String() {String.Join("", New String() {firstParts(0), "=", TextBox1.Text}), String.Join("", New String() {firstParts(1), "=", TextBox2.Text})})
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'read the file into an array of strings, each element being 1 line of the file
Dim lines() As String = IO.File.ReadAllLines("filename")
'dimension the firstParts array
ReDim firstParts(lines.GetUpperBound(0))
'split the first line into 2 parts
Dim parts() As String = lines(0).Split("="c)
'save the first part to the array
firstParts(0) = parts(0)
'put the second part in TextBox1
TextBox1.Text = parts(1)
'split the second line into 2 parts
parts = lines(1).Split("="c)
'save the first part to the array
firstParts(1) = parts(0)
'put the second part in TextBox2
TextBox2.Text = parts(1)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 24th, 2011, 04:43 PM
#6
Re: How can I retrieve data from a specific place in text?
here's an xml example:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xml As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<root>
</root>
xml...<root>(0).Add( _
<people>
<name><%= TextBox1.Text %></name>
<age><%= TextBox2.Text %></age>
</people>)
xml.Save("test.xml")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xml As XDocument = Nothing
If IO.File.Exists("test.xml") Then
xml = XDocument.Load("test.xml")
Dim info = (From node In xml...<people> _
Select New With { _
.name = node.<name>.Value, _
.age = node.<age>.Value}).ToArray
TextBox1.Text = info(0).name
TextBox2.Text = info(0).age
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 24th, 2011, 04:45 PM
#7
Thread Starter
Junior Member
Last edited by Reboh79; Jun 13th, 2015 at 06:10 AM.
Reason: Redacting content
-
Jan 24th, 2011, 04:57 PM
#8
Re: How can I retrieve data from a specific place in text?
it creates an xml file like this when you click the button.
vb Code:
<?xml version="1.0" encoding="utf-8"?>
<root>
<people>
<name>Jim</name>
<age>51</age>
</people>
</root>
in the form_load event, it checks if the xml file exists, + reads it.
LINQ tutorial:
http://searchwindevelopment.techtarg...de-LINQ-to-XML
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 24th, 2011, 05:38 PM
#9
Re: How can I retrieve data from a specific place in text?
One thing I would add is that XML is particularly easy to work with if you are dealing with a datatable, since you can serialize the table to XML with a single line, and deserialize back from XML just as easily. Considering your sample data, I would expect that you probably have more than one person (or else there are even better alternatives than XML), so you are probably storing the information somewhere. If you are using a datatable, then XML is definitely going to be better.
My usual boring signature: Nothing
 
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
|