Split from a text file

each line in the text file looks like this 1,"a1234",15,#200

Page 1 of 1

4 Replies - 9982 Views - Last Post: 21 October 2008 - 04:38 PM Rate Topic: -----

#1 benbinyamin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 20-October 08

Split from a text file

Posted 20 October 2008 - 08:02 PM

Based on the code below the details in the text file look like this and it works ok.
1
a1234
15
#2008-10-20

I need to have this information in one line inside the text file like this example below:
1,"a1234",15,#2008-10-20

and then split the line using the (,) and show each section in a different text box on the form.
Option Strict On

Imports System.IO
Public Class Form1

	Private OrdersStreamReader As StreamReader
  
	Structure Parts
		Dim OrderNoString As String
		Dim ProductIDString As String
		Dim QuantityString As String
		Dim DateRequiredString As String
		Dim OrderString As String
	End Structure
	Private aPart(50) As Parts
	Private countAllInteger As Integer
	
   
	Private Sub CmbOrders_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbOrders.SelectedIndexChanged
		'Display the information for the selected Order.

		With CmbOrders
			If .SelectedIndex <> -1 Then
				txtOrders.Text = aPart(.SelectedIndex).OrderNoString
				txtProductID.Text = aPart(.SelectedIndex).ProductIDString
				txtQuantity.Text = aPart(.SelectedIndex).QuantityString
				txtDateRequired.Text = aPart(.SelectedIndex).DateRequiredString
			End If
		End With
	End Sub

	Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
		'Open the file
		Dim responseDialogResult As DialogResult
		Dim indexInteger As Integer

		'Is the file already open?
		If Not OrdersStreamReader Is Nothing Then
			OrdersStreamReader.Close()
		End If

		'Begin in the project folder
		OpenFileDialog1.InitialDirectory = Application.StartupPath

		'display the file open dialog box
		responseDialogResult = OpenFileDialog1.ShowDialog

		'Make sure that the user didn't click the Cancel button.
		If responseDialogResult <> DialogResult.Cancel Then
			'Open the output file
			OrdersStreamReader = New StreamReader(OpenFileDialog1.FileName)
			Do While OrdersStreamReader.Peek <> -1
				aPart(countAllInteger).OrderNoString = OrdersStreamReader.ReadLine
				aPart(countAllInteger).ProductIDString = OrdersStreamReader.ReadLine
				aPart(countAllInteger).QuantityString = OrdersStreamReader.ReadLine
				aPart(countAllInteger).DateRequiredString = OrdersStreamReader.ReadLine
				aPart(countAllInteger).OrderString = OrdersStreamReader.ReadLine
				countAllInteger += 1
			Loop
			OrdersStreamReader.Close()
			'Load all of the names into the combobox
			Do Until indexInteger > countAllInteger - 1
				CmbOrders.Items.Add(aPart(indexInteger).OrderString)
				indexInteger += 1
			Loop
			CmbOrders.Text = ""
		End If
	End Sub
End Class

Attached File(s)


This post has been edited by jayman9: 21 October 2008 - 11:03 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Split from a text file

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Split from a text file

Posted 20 October 2008 - 08:13 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Split from a text file

Posted 21 October 2008 - 11:05 AM

Post the code that writes to the text file. You need to write the information as comma separated before you can read it and Split the values apart on the commas.
Was This Post Helpful? 0
  • +
  • -

#4 benbinyamin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 20-October 08

Re: Split from a text file

Posted 21 October 2008 - 03:52 PM

View Postjayman9, on 21 Oct, 2008 - 11:05 AM, said:

Post the code that writes to the text file. You need to write the information as comma separated before you can read it and Split the values apart on the commas.


The information in the Text file will be entered manualy so i only need to do stream reader to read the information from the text file. i just need help with the code to split this line which is in the text file. 1,"a1234",15,#2008-10-20

the only thing is the code for spliting the above line and showing the information in 4 different text boxes.

hope this explains the information better.

apologies for being a pain iam very new to this.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Split from a text file

Posted 21 October 2008 - 04:38 PM

When you split a line like that, looks like you need to split it on the comma (",") you can read it into an array, then reference the indexes of the array when populating the TextBox.

Take a look at this example, I pass it the file I wish to read (in this case it has a single line so I really don't need a loop), I then split the file on each comma and throw it into a String array. From there I populate 4 TextBoxes based on the index of my String array. Remember, arrays start with an index of zero (0), not one

Private Sub SplitLine(ByRef file As String)
        'Open my file (which contains a single line of text)
	Dim reader As New StreamReader(file)

        'Create a String Array
	Dim data As String()

        'Read the line of text and split on the comma
	data = reader.ReadLine().Split(",")
	
	'Now we have our String array we can populate TextBoxes
	TextBox1.Text = data(0)  'Index 0 goes into the first TextBox
	TextBox2.Text = data(1)  'Index 1 goes into the second TextBox
	TextBox3.Text = data(2)  'Index 2 goes into the third TextBox
	TextBox4.Text = data(3)  'Index 3 goes into the fourth TextBox
End Sub



Hope that helps :)
Was This Post Helpful? 2

Page 1 of 1