5 Replies - 1578 Views - Last Post: 22 May 2009 - 02:59 AM Rate Topic: -----

#1 brynnflynn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-May 09

Importing Text Box Contents from a Text File

Post icon  Posted 20 May 2009 - 12:42 PM

So I'm in the final stages of finishing a program I've been working on for nearly a year now, and this is basically my final hurdle. The Save dialogue is working beautifully, with 'flags' in order to switch it over from the regular input into text boxes to the Listbox input protocol.

However, I'm having a *** of a time getting it to take lines from the text file and put them in the proper text box. Here is an example file:

Quote

SHOWNAME
CALLNAME
BREED
SHOWCODE
[]
1 in SHOWNm on SITENAME
[]
1
3
2
4
OTHERINFO

where the '[]' switches the program over to the If/while statement in the code below.

	Private Sub OpenProfileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProfileToolStripMenuItem.Click
		Dim AllText As String = ""
		Dim LineOfText As String = ""

		'array containing the names of the text boxes I want 
		'the lines to go into
		strboxNames(0) = "callNameText"
		strboxNames(1) = "showNameText"
		strboxNames(2) = "breedText"
		strboxNames(3) = "genderText"
		strboxNames(4) = "showCodeText"
		strboxNames(5) = "rkcText"
		strboxNames(6) = "vpaText"
		strboxNames(7) = "pkcText"
		strboxNames(8) = "otherText"
		strboxNames(9) = "totalText"
		strboxNames(10) = "notesText"

		OpenFileDialog1.FileName = "Pet.txt"
		OpenFileDialog1.Filter = "txt (*.txt)|*.txt"
		OpenFileDialog1.ShowDialog()
		If OpenFileDialog1.FileName <> "" Then
			Try
				FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
				
				'ati is the pointer going through the text file
				Dim ati As Integer = 0

				'this reads in the text file into one array
				Do Until EOF(1)
					LineOfText = LineInput(1)
					AllText = AllText + LineOfText + vbCrLf
				Loop

				'splits the array into parts
				Dim strAllText() As String
				strAllText = Split(AllText, vbCrLf)

				'begin import to the program! i is the pointer for the text boxes
				For i = LBound(strboxNames) To UBound(strboxNames)

					'checks to make sure that we're not in the special Listbox 
					'section of the text file
					If strAllText(ati) = "[]" Then

						'moves the pointer up one so it doesn't add the []
						ati = ati + 1
					   
						'as long as it hasn't reached the end of the special case,
						'add it to the list box in the GUI
						While strAllText(ati) <> "[]"
							showHistoryList.Items.Add(strAllText(ati))
							'moves to next line
							ati = ati + 1
						End While

						'once you're done, move the pointer forward so the program
						'doesn't count the second []
						ati = ati + 1
					End If
					
					'THEORETICALLY, this would take the i instance of the 
					'textbox names defined at the beginning, make it 
					'into a reference for that textbox, and then place the ati
					'instance of strAllText into that text box.
					Me.Controls(strboxNames(i)).Text() = strAllText(ati)
					
					'moves to next line in the text file
					ati = ati + 1
				Next
			Finally
				FileClose(1)
			End Try
		End If
	End Sub



So with the sample file I provided above, in the textbox named callNameText would appear "SHOWNAME", and so on and so forth. If someone has any ideas on how to make this implementation work I'd be eternally grateful! With this build, I get a NullReferenceException on the "Me.Controls(strboxNames(i)).Text() = strAllText(ati)" line.

Is This A Good Question/Topic? 0
  • +

Replies To: Importing Text Box Contents from a Text File

#2 kasbaba  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 133
  • Joined: 03-November 08

Re: Importing Text Box Contents from a Text File

Posted 20 May 2009 - 12:55 PM

View Postbrynnflynn, on 20 May, 2009 - 11:42 AM, said:



You can use the code below to simplify your design..the FileLines() array, will contain all lines in the Text file...you can then use the For Loop to look for the value you want..

Private Sub OpenProfileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProfileToolStripMenuItem.Click
		Dim AllText As String = ""
		Dim LineOfText As String = ""

		'array containing the names of the text boxes I want 
		'the lines to go into
		strboxNames(0) = "callNameText"
		strboxNames(1) = "showNameText"
		strboxNames(2) = "breedText"
		strboxNames(3) = "genderText"
		strboxNames(4) = "showCodeText"
		strboxNames(5) = "rkcText"
		strboxNames(6) = "vpaText"
		strboxNames(7) = "pkcText"
		strboxNames(8) = "otherText"
		strboxNames(9) = "totalText"
		strboxNames(10) = "notesText"

		OpenFileDialog1.FileName = "Pet.txt"
		OpenFileDialog1.Filter = "txt (*.txt)|*.txt"

	If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel then
		Dim FileLines() as String
		Try
			FileLines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
			For i = 0 to FileLines.Length - 1
				if FileLines(i).toString = "[]" then
					'do something here
				end if
			Next i
		Catch EX as exception
		End Try
	End If
End Sub



hope this helps
Was This Post Helpful? 1

#3 bigtonyk  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 61
  • Joined: 17-May 09

Re: Importing Text Box Contents from a Text File

Posted 20 May 2009 - 01:25 PM

View Postbrynnflynn, on 20 May, 2009 - 11:42 AM, said:

So I'm in the final stages of finishing a program I've been working on for nearly a year now, and this is basically my final hurdle. The Save dialogue is working beautifully, with 'flags' in order to switch it over from the regular input into text boxes to the Listbox input protocol.

However, I'm having a *** of a time getting it to take lines from the text file and put them in the proper text box. Here is an example file:

Quote

SHOWNAME
CALLNAME
BREED
SHOWCODE
[]
1 in SHOWNm on SITENAME
[]
1
3
2
4
OTHERINFO

where the '[]' switches the program over to the If/while statement in the code below.

	Private Sub OpenProfileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProfileToolStripMenuItem.Click
		Dim AllText As String = ""
		Dim LineOfText As String = ""

		'array containing the names of the text boxes I want 
		'the lines to go into
		strboxNames(0) = "callNameText"
		strboxNames(1) = "showNameText"
		strboxNames(2) = "breedText"
		strboxNames(3) = "genderText"
		strboxNames(4) = "showCodeText"
		strboxNames(5) = "rkcText"
		strboxNames(6) = "vpaText"
		strboxNames(7) = "pkcText"
		strboxNames(8) = "otherText"
		strboxNames(9) = "totalText"
		strboxNames(10) = "notesText"

		OpenFileDialog1.FileName = "Pet.txt"
		OpenFileDialog1.Filter = "txt (*.txt)|*.txt"
		OpenFileDialog1.ShowDialog()
		If OpenFileDialog1.FileName <> "" Then
			Try
				FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
				
				'ati is the pointer going through the text file
				Dim ati As Integer = 0

				'this reads in the text file into one array
				Do Until EOF(1)
					LineOfText = LineInput(1)
					AllText = AllText + LineOfText + vbCrLf
				Loop

				'splits the array into parts
				Dim strAllText() As String
				strAllText = Split(AllText, vbCrLf)

				'begin import to the program! i is the pointer for the text boxes
				For i = LBound(strboxNames) To UBound(strboxNames)

					'checks to make sure that we're not in the special Listbox 
					'section of the text file
					If strAllText(ati) = "[]" Then

						'moves the pointer up one so it doesn't add the []
						ati = ati + 1
					   
						'as long as it hasn't reached the end of the special case,
						'add it to the list box in the GUI
						While strAllText(ati) <> "[]"
							showHistoryList.Items.Add(strAllText(ati))
							'moves to next line
							ati = ati + 1
						End While

						'once you're done, move the pointer forward so the program
						'doesn't count the second []
						ati = ati + 1
					End If
					
					'THEORETICALLY, this would take the i instance of the 
					'textbox names defined at the beginning, make it 
					'into a reference for that textbox, and then place the ati
					'instance of strAllText into that text box.
					Me.Controls(strboxNames(i)).Text() = strAllText(ati)
					
					'moves to next line in the text file
					ati = ati + 1
				Next
			Finally
				FileClose(1)
			End Try
		End If
	End Sub



So with the sample file I provided above, in the textbox named callNameText would appear "SHOWNAME", and so on and so forth. If someone has any ideas on how to make this implementation work I'd be eternally grateful! With this build, I get a NullReferenceException on the "Me.Controls(strboxNames(i)).Text() = strAllText(ati)" line.



You can't index the Controls() collection with the control name. You have to loop through all the controls til you find the one with the matching name.

If the order of the data in the text file is always the same, why not hard code the control names to the array value?

callNameText.Text = strAllText(0)

etc.

I would suggest using XML for the data file and using named attributes for the data, then you can pull the specific data from the file and put it in whichever control you want.
Was This Post Helpful? 0
  • +
  • -

#4 Apache  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 46
  • Joined: 03-November 08

Re: Importing Text Box Contents from a Text File

Posted 20 May 2009 - 07:23 PM

Here's an object orientated approach to saving your data in XML:

Public Class FileClass

#Region "Structures and Objects"
	Structure PetStyle
		Dim CallName As String
		Dim BreedName As String
		Dim ShowName As String
		Dim Gender As String
		Dim ShowCode As String
		Dim RKC As String
		Dim VPA As String
		Dim PKC As String
		Dim Total As String
		Dim Notes As String
		Dim Other As String
	End Structure

	Structure XMLDataStructure
		Dim callNameTextList As Xml.XmlNodeList
		Dim showNameTextList As Xml.XmlNodeList
		Dim breedTextList As Xml.XmlNodeList
		Dim genderTextList As Xml.XmlNodeList
		Dim showCodeTextList As Xml.XmlNodeList
		Dim rkcTextList As Xml.XmlNodeList
		Dim vpaTextList As Xml.XmlNodeList
		Dim pkcTextList As Xml.XmlNodeList
		Dim otherTextList As Xml.XmlNodeList
		Dim totalTextList As Xml.XmlNodeList
		Dim notesTextList As Xml.XmlNodeList
	End Structure

	Dim Pet() As PetStyle
	Dim XMLData As New XMLDataStructure
#End Region

	Public Sub Save()
		Dim myXmlSettings As New Xml.XmlWriterSettings
		myXmlSettings.Indent = True
		myXmlSettings.NewLineOnAttributes = True

		Using x As Xml.XmlWriter = Xml.XmlWriter.Create("Pet.xml", myXmlSettings)
			x.WriteStartDocument()
			x.WriteStartElement("Pets")
			For i As Integer = 0 To UBound(Pet)
				x.WriteStartElement("Pet")

				x.WriteStartElement("callNameText")
				x.WriteValue(Pet(i).CallName)
				x.WriteEndElement()

				x.WriteStartElement("showNameText")
				x.WriteValue(Pet(i).ShowName)
				x.WriteEndElement()

				x.WriteStartElement("breedText")
				x.WriteValue(Pet(i).BreedName)
				x.WriteEndElement()

				x.WriteStartElement("genderText")
				x.WriteValue(Pet(i).Gender)
				x.WriteEndElement()

				x.WriteStartElement("showCodeText")
				x.WriteValue(Pet(i).ShowCode)
				x.WriteEndElement()

				x.WriteStartElement("rkcText")
				x.WriteValue(Pet(i).RKC)
				x.WriteEndElement()

				x.WriteStartElement("vpaText")
				x.WriteValue(Pet(i).VPA)
				x.WriteEndElement()

				x.WriteStartElement("pkcText")
				x.WriteValue(Pet(i).PKC)
				x.WriteEndElement()

				x.WriteStartElement("otherText")
				x.WriteValue(Pet(i).Other)
				x.WriteEndElement()

				x.WriteStartElement("totalText")
				x.WriteValue(Pet(i).Total)
				x.WriteEndElement()

				x.WriteStartElement("notesText")
				x.WriteValue(Pet(i).Notes)
				x.WriteEndElement()

				x.WriteEndElement()
			Next
			x.WriteEndElement()
			x.Close()
		End Using
	End Sub

	Public Sub Load()
		Dim x As New Xml.XmlDocument
		x.Load("Pet.xml")

		With XMLData
			.callNameTextList = x.GetElementsByTagName("callNameText")
			.showNameTextList = x.GetElementsByTagName("showNameText")
			.breedTextList = x.GetElementsByTagName("breedText")
			.genderTextList = x.GetElementsByTagName("genderText")
			.showCodeTextList = x.GetElementsByTagName("showCodeText")
			.rkcTextList = x.GetElementsByTagName("rkcText")
			.vpaTextList = x.GetElementsByTagName("vpaText")
			.pkcTextList = x.GetElementsByTagName("pkcText")
			.otherTextList = x.GetElementsByTagName("otherText")
			.totalTextList = x.GetElementsByTagName("totalText")
			.notesTextList = x.GetElementsByTagName("notesText")
		End With

		For i As Integer = 0 To XMLData.callNameTextList.Count - 1
			With Pet(i)
				.CallName = XMLData.callNameTextList.Item(i).InnerText
				.BreedName = XMLData.breedTextList.Item(i).InnerText
				.Gender = XMLData.genderTextList.Item(i).InnerText
				.PKC = XMLData.pkcTextList.Item(i).InnerText
				.RKC = XMLData.rkcTextList.Item(i).InnerText
				.VPA = XMLData.vpaTextList.Item(i).InnerText
				.ShowName = XMLData.showNameTextList.Item(i).InnerText
				.ShowCode = XMLData.showCodeTextList.Item(i).InnerText
				.Total = XMLData.totalTextList.Item(i).InnerText
				.Notes = XMLData.notesTextList.Item(i).InnerText
				.Other = XMLData.otherTextList.Item(i).InnerText
			End With
		Next
	End Sub

End Class


To add text to the textboxes:

Dim index As Integer = 0
txtCallName.Text = Pet(index).CallName


You can scroll through the different pets by changing the value of the index integer. Simple Next and Previous button code would be:

	Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
		If index + 1 > UBound(Pet) _
		Then : index = 0
		Else : index += 1
		End If
	End Sub

	Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
		If index - 1 < 0 _
		Then : index = UBound(Pet)
		Else : index -= 1
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

#5 brynnflynn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-May 09

Re: Importing Text Box Contents from a Text File

Posted 20 May 2009 - 07:54 PM

Thank you both so much for your speedy replies! Within an hour or two of tweaking the code Kasbaba suggested, I got the following:

	Private Sub OpenProfileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProfileToolStripMenuItem.Click
		Dim AllText As String = ""
		Dim LineOfText As String = ""
		Dim i As Integer = 7

		OpenFileDialog1.FileName = "Pet.txt"
		OpenFileDialog1.Filter = "txt (*.txt)|*.txt"

		If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
			Dim FileLines() As String
			Try
				FileLines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
				showNameText.Text = FileLines(0)
				callNameText.Text = FileLines(1)
				breedText.Text = FileLines(2)
				genderText.Text = FileLines(3)
				showCodeText.Text = FileLines(4) & vbCrLf & FileLines(5) & vbCrLf & FileLines(6)

				While FileLines(i) <> "[]"
					showHistoryList.Items.Add(FileLines(i))
					'moves to next line
					i = i + 1
				End While

				rkcText.Text = FileLines(i + 1)
				pkcText.Text = FileLines(i + 2)
				vpaText.Text = FileLines(i + 3)
				otherText.Text = FileLines(i + 4)
				totalText.Text = FileLines(i + 5)
				notesText.Text = FileLines(i + 6)

			Catch EX As Exception
			End Try
		End If
	End Sub


MUCH neater! The reason I'm using text files is because this program is meant to open the text file and make it nice and pretty as a portable record keeping system; my users are going to want to trade the text files with each other as they trade the virtual pets the files go with. I have some pictures to show you guys if you're interested. :)

In case anyone's about to yell at me to use a loop for the second half of the code, where the i+1, i+2 stuff shows up the reason I did it that way was because the i value, depending on how many entries there are in the listbox, is going to change from file to file. This was the best method I could come up with for adjusting for that. :)

Posted Image
Posted Image
Posted Image

Thank you for the code Apache! I'm going to have to take a look at it and see what I can puzzle out/integrate into my system! I'm just hoping to get the basic build out the door to my beta testers.
Was This Post Helpful? 0
  • +
  • -

#6 Apache  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 46
  • Joined: 03-November 08

Re: Importing Text Box Contents from a Text File

Posted 22 May 2009 - 02:59 AM

No worries. Once you start working with XML as a save format you see how easy it is. It looks a bit daunting at first but if you get a working example and just modify it, it's easy to learn and master. It's quite natural to program and the methods are all quite self exlanatory. PM if you would like a step by step guide or any pointers on it. I'm writing a tutorial atm about object orientation and structured arrays using XML. I'll publish it once it's done.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1