3 Replies - 1683 Views - Last Post: 09 May 2009 - 10:51 PM Rate Topic: -----

#1 Graham Dyer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-April 09

Extracting numbers from text files for a combobox?

Posted 06 May 2009 - 12:28 AM

Hi everyone

I'm very new to this so I hope you'll bear with me. I've searched quite a bit but can't find anything specific to this. It's hopefully fairly simple:

I want to:

1. allow the user to specify a folder in which to find a specific text files,
2. to read a few specific numbers within each of those few text files, (eg. in txt file A, #A = '1', in txt file B, #B = '2', etc)
3. place those numbers into multiple comboboxes, (eg '1' into CB1, '2' into CB2, etc)
4. allow the user to change the number from the combobox drop down list, (eg CB1 change to '2', CB2 change to '1', etc)
5. Click to overwrite the old numbers in the text file with the new numbers and save the text files

My main problem is no. 2, how do I read a specific string in the text file and display it in the combobox?

Any assistance will be greatly appreciated

Graham

(Win XP, MS Visual Basic 2008 Express Edition with sp1)

Is This A Good Question/Topic? 0
  • +

Replies To: Extracting numbers from text files for a combobox?

#2 Graham Dyer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-April 09

Re: Extracting numbers from text files for a combobox?

Posted 06 May 2009 - 06:53 AM

My text file consists of:

#define roll_P 0.8
#define roll_I 1.5

so I want the first combobox to read the first value of '0.8' and the second combobox to read '1.5'

Here's my code to read the first line (with a message box just to display it) but I want to actually look for '#define roll_P' and then read only the '0.8' and put it into the combobox as the starting value:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Dim sr As IO.StreamReader = New IO.StreamReader("C:\test.txt")
		Dim str As String = sr.ReadLine
		sr.Close()
		MsgBox(str)

	End Sub


Please help?

This post has been edited by Graham Dyer: 06 May 2009 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 Graham Dyer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-April 09

Re: Extracting numbers from text files for a combobox?

Posted 08 May 2009 - 12:32 AM

Some progress but I can't get the value to show in the ComboBox on runtime, it only shows when I select any value in the ComboBox? Is my ComboBox event wrong?

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Using SR As New System.IO.StreamReader("C:/test.txt")
			While Not SR.EndOfStream
				Dim Theline As String = SR.ReadLine
					If Theline.Contains("#define roll_P ") Then
					ComboBox1.Text = Mid(Theline, 16, 3)
					MsgBox(ComboBox1.Text) 'to show what's been read
					Exit While
				Else
					MsgBox("Error")
				End If
			End While
			SR.Close()
		End Using
	End Sub



My idea is to find the line with specific text, read the digits after that text and use those as default start values for the comboboxes. Perhaps I'm going about it the wrong way?
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: Extracting numbers from text files for a combobox?

Posted 09 May 2009 - 10:51 PM

Here's my two cents. I've used this technique in a recent project more or less. Searching for known text inside a specific file to find a variable and then use that variable within my program.

I've done a short piece of code to get you started. It can be expanded on from here:

Controls:
ofdOpen - OpenFileDialog
cbxDefineI - ComboBox
cbxDefineP - ComboBox

Variables:
iFile - Streamreader, to read specified file
tempdata - String, houses each line of the file in turn
data - ComboBox.ObjectCollection, houses the values from the specified file

Synopsis:
Once a file has been chosen by the user this code will loop through the specified file line by line. Each line it checks if the line starts with "#define roll_I" or "#define roll_P" if so it add the rest of that line (the whole line bar the first 15 characters) into an ObjectCollection. When the file is closed the code then adds the contents of the ObjectCollection into both of the ComboBoxes on the form and refreshes them.

To Do:
Obviously this is a very rudimentry version and should not be used as-is. Validation should be added to assure correct files are used, the code itself would be better in a backend Sub rather than in front end code and as of now both combo boxes will display the same values as default. However, hopefully this will give you a set over your hurdle.

Private Sub ofdOpen_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdOpen.FileOk
		Dim iFile As New System.IO.StreamReader(ofdOpen.FileName)
		Dim tempdata As String
		Dim data As ComboBox.ObjectCollection

		Do Until iFile.EndOfStream = True
			tempdata = iFile.ReadLine()
			If tempdata.StartsWith("#define roll_I") = True Then
				data.Add(tempdata.Remove(0, 15))
			End If

			If tempdata.StartsWith("#define roll_P") = True Then
				data.Add(tempdata.Remove(0, 15))
			End If
		Loop
		iFile.Close()

		cbxDefineI.Items.Add(data) : cbxDefineI.Refresh()
		cbxDefineP.Items.Add(data) : cbxDefineP.Refresh()
	End Sub

This post has been edited by Apache: 09 May 2009 - 10:58 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1