Simple question...loop through lines of textbox

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I need to loop through each line of a textbox so I can write the line to an XML element. This isn't an XML question though. I know how to do this with a listbox, but I tried the following with a textbox and it gives me an error:
For y = 0 To Directions.Lines - 1

How can I loop through each line and do something with the selected line?
 
The Lines property of a TextBox returns a string array where each line of text in the control is an element. One thing to note is that a new array is generated every time you access the property, so you need to make sure that you access it only once. This is bad:
VB.NET:
For i As Integer = 0 To myTextBox.Lines.GetUpperBound(0) '<- array created here
    MessageBox.Show(myTextBox.Lines(i)) '<- array created here
Next i
If your TextBox had 100 lines then you'd create 101 arrays. This would be better:
VB.NET:
Dim lines As String() = myTextBox.Lines '<- array create here.

For i As Integer = 0 To lines.GetUpperBound(0)
    MessageBox.Show(lines(i))
Next i
That way you create only one array. That would be the most efficient way to do it from an execution point of view. If you want the neatest code then you'd do this:
VB.NET:
For Each line As String In myTextBox.Lines '<- array created here
    MessageBox.Show(line)
Next line
You create just one array and the code is very clean and clear.
 
Thanks, that does exactly what I want. I'm very happy with this since it's my first time writing an XML document programmatically. I even got this set up so that the directions and ingredients elements have child nodes, which will make it easier for the program to find what it needs. I figured I'd have to just go through each line of the XML file to see if it was an ingredient line. I thought it was probably easy to figure out how to make child nodes, but didn't know how. I stumbled across the solution. Here's an example of how the XML file looks.

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE Recipe (View Source for full doctype...)>
- <Recipe Name="Blake">
- <Ingredients>
<Item>1 cup water</Item>

<Item>1 tsp salt</Item>

<Item>1 tsp sugar</Item>

</Ingredients>


- <Directions>
<Step>line1</Step>

<Step>line2</Step>

<Step>line3</Step>

</Directions>


</Recipe>
 
You could event do things like give the Item nodes attributes like Name Quantity and Unit, then your nodes would look like:

<
Item name="water" quantity="1" unit="cup" />
 
Thanks, I just might do it that way. I want to try to have my application have a shopping list for the recipes you select. It might be tricky, but I'd like it to calculate the total amount of ingredients used if you've selected multiple recipes for the shopping list. I'm also trying to figure out how to open an existing XML file and add a new recipe to it. What I have so far assumes that you don't already have an XML file. I'm going to be keeping all of the recipes in one XML file, so I need to learn how to open an existing one and add to it. Thanks for your help.
 
Is this specifically an exercise in learning how to use XML? If not then you'd probably be better off using a proper database. Alternatively you could just create your XML file by calling the WriteXml method of a DataSet. That way you can load all your existing recipes by calling ReadXml and use the in-built functionality of the DataSet to display and edit your data, then save the changes by calling WriteXml again.
 
This is just something for us to use at home, but it's also for me to learn. I am planning on giving this to a few friends if I get it all completed and it does what I want. I hadn't thought of the database idea before, but that sounds good too. I just planned on using XML because I'm familiar with how it is structured and thought that it would be a good way to handle doing what I want. I'm also thinking about setting up an XML file on my website so that other people can add and view the recipes on it. I had originally planned on the application accessing and changing the file on my website, but I'm now thinking that if I add that feature, I might just use an ASP page to do that part. I'll look into WriteXml and ReadXml tomorrow or whenever I can so that I can see how that works. Thanks for your help.
 
Using XML as a data source is fine when there is a small amount of data with a single user. For what you're talking about I think you really should start using a database now or it's going to get completely unmanageable down the track and you'll just have to change it anyway. It doesn't matter too much which database you choose as once you're using ADO.NET it's a fairly easy thing to switch from one data source to another.
 
Just a quick question. Can you show me a quick example (even just one line doing a ReadXml or WriteXml) for using a DataSet. I'm not sure exactly what you mean by that or if it's different from an XMLTextWriter. Thanks.

I found a code example, if this is what you mean. However, this example only creates a new XML file. I still need to learn how to open an existing file and add XML to it.
 
You create a new DataSet and call its ReadXml method to load an XML file into it. You then call the WriteXml method to save the contents of the DataSet to an XML file on disk. I believe that you can load any well-formed XML file this way although I've never tried it with one I've written myself. An XML file that is saved this way has a specific structure and I've always created my XML files that are for this purpose by calling WriteXml in the first place.

Here's my suggestion. You install your app with no XML file at all. Then when you run it you check for the existence of an XML file. If one is their then you load it and if one isn't then you create an empty DataTable with the appropriate schema:
VB.NET:
Dim recipesData As New DataSet
Dim xmlPath As String = IO.Path.Combine(Application.StartupPath, "Recipes.XML"

If IO.File.Exists(xmlPath)
    'Load the existing file.
    recipesData.ReadXml(xmlPath)
Else
    'Create an empty DataTable.
    Dim recipesTable As New DataTable

    'Add columns here.

    recipesData.Tables.Add(recipesTable)
End If
Then when you shutdown your application you would save the data to disk:
VB.NET:
'Save the data to disk.
recipesData.WriteXml(IO.Path.Combine(Application.StartupPath, "Recipes.XML")
As you're using 2005 the place to do all this would be in the Startup and Shutdown events of the application. You can then also make the DataSet a class-level variable in the ApplicationEvents.vb file and access it as a member of My.Application throughout your project.
 
Back
Top