Just need a little lesson

Don't understand what is not working

Page 1 of 1

13 Replies - 1087 Views - Last Post: 29 October 2009 - 11:40 PM Rate Topic: -----

#1 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Just need a little lesson

Post icon  Posted 26 October 2009 - 01:08 PM

Ok, for starters, I am new to the forums here so Hello.

Sorry if I am brief I have to get my son soon I just wanted to post so I could hopefully get some answers and understand what I am doing wrong. I know this is homework and I should not ask it but I am stuck, I do not want code hand outs also do not want really involved ways of doing this as we are not deep into VB yet.

Ok, so here is the code for my program. We are making a membership list and it needs to load with just the names in the txt file that is in the debug bin. The txt file is set up in one column and holds the member name and the next line is member number and so on. Information is read into an "array of structures" and the display is supposed to be ONLY the names in the listbox on load. This is my problem. I had it displaying all th txt file info and I just need the names. I tried what you will see in the code but it is not working and my book is less then helpful when it comes to examples and/or knowledge on the subject. Though it could be too many hours spent trying to get it to work.

Imports System.IO
Public Class frmMemberList
	Structure Members
		Dim member As String
		Dim number As Integer
	End Structure

	Const FOLDER As String = "MEMBERPHONES.TXT"
	Dim data() As Members

	Private Sub frmMemberList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		getData()
		displayData()
	End Sub
	Sub getData()
		Dim i As Integer
		Dim sr As StreamReader = File.OpenText(FOLDER)
		Do While (sr.Peek <> -1)
			i += 1
			data(i).member = sr.ReadLine
			data(i).number = sr.ReadLine
		Loop
		sr.Close()
	End Sub

	Sub displayData()
		lstDisplay.Items.Clear()
		For i As Integer = 0 to ()
			lstDisplay.Items.Add(String.Format(data(i).member))
		Next
	End Sub



I appreciate any help I can get. I think I have the other functions I need done, just need to get the display up and running to test.

Is This A Good Question/Topic? 0
  • +

Replies To: Just need a little lesson

#2 eworm  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 64
  • Joined: 17-March 09

Re: Just need a little lesson

Posted 26 October 2009 - 01:52 PM

I am unable to help you with using the Structure method. But that seems like you're doing way more work than is necessary. Unless you absolutely HAVE to use that structure method then I think I can help you.

It might be easier and less steps to read the file and add it to the list all in one swipe.

Dim sr as IO.StreamReader
Dim strPath as String = "memberphones.txt"
Dim x as Integer
Dim strNames() as String
Dim intNumbers() as Integer

sr = New IO.StreamReader(strPath)

ListBox.Items.Clear

Do While sr.Peek <> -1
	 strNames(x) = sr.readline
	 strNumbers(x) = sr.readline
	 
	 ListBox.Items.Add(strNames(x))
Loop

sr.close



I hope this helps..and sorry it wasn't in that Structure Format..I have never seen that used before.
Was This Post Helpful? 1
  • +
  • -

#3 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Just need a little lesson

Posted 26 October 2009 - 02:31 PM

View Posteworm, on 26 Oct, 2009 - 12:52 PM, said:

I am unable to help you with using the Structure method. But that seems like you're doing way more work than is necessary. Unless you absolutely HAVE to use that structure method then I think I can help you.

It might be easier and less steps to read the file and add it to the list all in one swipe.

Dim sr as IO.StreamReader
Dim strPath as String = "memberphones.txt"
Dim x as Integer
Dim strNames() as String
Dim intNumbers() as Integer

sr = New IO.StreamReader(strPath)

ListBox.Items.Clear

Do While sr.Peek <> -1
	 strNames(x) = sr.readline
	 strNumbers(x) = sr.readline
	 
	 ListBox.Items.Add(strNames(x))
Loop

sr.close



I hope this helps..and sorry it wasn't in that Structure Format..I have never seen that used before.

I appreciate the speedy response. I see what you are doing and totally agree that it would be much less work and to me make more sense as that was sort of my initial format, but we have to have the names and numbers read into an array of structures. I think I may have something or may be on the right track with a recent change I made so I am hopeful that things work out. Though any insight and education I can take out of this post and the posts of anyone else who wishes to toss in some information would be greatly appreciated. I am still pretty green at VB but am learning rather quickly and hope to learn as much as possible since it can only serve to help me later.
Was This Post Helpful? 0
  • +
  • -

#4 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Just need a little lesson

Posted 26 October 2009 - 05:15 PM

Try
		Do While (sr.Peek <> -1)
			i += 1
			data(i).member = sr.ReadLine
			i += 1
			data(i).number = sr.ReadLine
		Loop


Hope it works, was not able to try it out
Was This Post Helpful? 0
  • +
  • -

#5 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Just need a little lesson

Posted 26 October 2009 - 06:15 PM

Don't double increment the index i, but do add 2 things. 1) Initialize the value of i to 0 at declaration time (you are just assuming it defaults to 0 now. 2) increment i only once, and do it AFTER the reads so that you actually use slot 0 in the array of structures.
Was This Post Helpful? 0
  • +
  • -

#6 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Just need a little lesson

Posted 26 October 2009 - 07:02 PM

View Postmark.bottomley, on 26 Oct, 2009 - 05:15 PM, said:

Don't double increment the index i, but do add 2 things. 1) Initialize the value of i to 0 at declaration time (you are just assuming it defaults to 0 now. 2) increment i only once, and do it AFTER the reads so that you actually use slot 0 in the array of structures.

I see what you are saying here. That makes sense. Sorry though I have to backtrack, I made the mistake of thinking I had it figured out, reworked the code and ended up confusing myself more I think so I am trying to get back to the beginning and take a fresh look at things.....I feel bad because I know this should not be this difficult, yet I seem to have made it a disaster.
Was This Post Helpful? 0
  • +
  • -

#7 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Just need a little lesson

Posted 26 October 2009 - 08:03 PM

View PostMKunstman, on 26 Oct, 2009 - 06:02 PM, said:

View Postmark.bottomley, on 26 Oct, 2009 - 05:15 PM, said:

Don't double increment the index i, but do add 2 things. 1) Initialize the value of i to 0 at declaration time (you are just assuming it defaults to 0 now. 2) increment i only once, and do it AFTER the reads so that you actually use slot 0 in the array of structures.

I see what you are saying here. That makes sense. Sorry though I have to backtrack, I made the mistake of thinking I had it figured out, reworked the code and ended up confusing myself more I think so I am trying to get back to the beginning and take a fresh look at things.....I feel bad because I know this should not be this difficult, yet I seem to have made it a disaster.

Ok so I have reworked the code and this is where I stand. No display when ran to debug, in fact nothing...
Imports System.IO
Public Class frmMemberList

	Structure Members
		Dim member As String
		Dim number As Integer
	End Structure

	Const FOLDER As String = "MEMBERPHONES.TXT"
   

	Private Sub frmMemberList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim recCounter As Integer
		recCounter = NumberOfRecords(FOLDER)
		Dim person(recCounter - 1) As Members
		getData(person, recCounter)
		sortData(person, recCounter)
		showData(person, recCounter)
	End Sub

	Function NumberOfRecords(ByVal filespec As String) As Integer
		Dim member As String, number As Integer
		Dim n As Integer
		n = 0
		Dim sr As StreamReader = File.OpenText(filespec)
		Do While (sr.Peek <> -1)
			member = sr.ReadLine
			number = CInt(sr.ReadLine)
			n += 1
		Loop
		sr.Close()
		Return n
	End Function

	Sub getData(ByRef person() As Members, ByVal recCounter As Integer)
		Dim sr As StreamReader = File.OpenText(FOLDER)
		For i As Integer = 0 To recCounter - 1
			person(i).member = sr.ReadLine
			person(i).number = CInt(sr.ReadLine)
		Next
		sr.Close()
	End Sub

	Sub sortData(ByRef person() As Members, ByVal recCounter As Integer)
		For passNum As Integer = 1 To recCounter - 1
			For i As Integer = 1 To recCounter - passNum
				If person(i - 1).number > person(i).number Then
					swapData(person, i - 1)
				End If
			Next
		Next
	End Sub

	Sub showData(ByVal person() As Members, ByVal recCounter As Integer)
		lstDisplay.Items.Clear()
		For i As Integer = 0 To recCounter - 1
			lstDisplay.Items.Add(person(i).member)
		Next
	End Sub
	Sub swapData(ByRef person() As Members, ByVal i As Integer)
		Dim temp As Members
		temp = person(i)
		person(i) = person(i + 1)
		person(i + 1) = temp
	End Sub

	Private Sub lstDisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDisplay.SelectedIndexChanged

	End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#8 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Just need a little lesson

Posted 26 October 2009 - 11:39 PM

Hi,

Lets start to debug...
Debugging is a programmers best tool.

I see you have this statement

   Const FOLDER As String = "MEMBERPHONES.TXT"



and you start using it here

	recCounter = NumberOfRecords(FOLDER)



What is the value of recCounter?
(i have a hunch that its 0)

So lets make sure we find the file "MEMBERPHONES.TXT"
to start off with lets move the file to "C:\" and not in the debug bin.

Change the "Const FOLDER As String = "MEMBERPHONES.TXT" to this

	Const FOLDER As String = "C:\MEMBERPHONES.TXT"



Now go to the NumberOfRecords() sub and change the code to this

Function NumberOfRecords(ByVal filespec As String) As Integer
		If My.Computer.FileSystem.FileExists(filespec) = false then
		   MessageBox.Show("Could not find the file MEMBERPHONES.TXT","Could not find file",MessageBoxButtons.OK,MessageBoxIcon.Error)
		   Return 0
		else
		   Dim member As String, number As Integer
		   Dim n As Integer
		   n = 0
		   Dim sr As StreamReader = File.OpenText(filespec)
		   Do While (sr.Peek <> -1)
			   member = sr.ReadLine
			   number = CInt(sr.ReadLine)
			   n += 1
		   Loop
		   sr.Close()
		   Return n
		end if 
	End Function



now that we can see if a file exists, we make sure that we continue if the file actually contains some data.
So lets modify the form load event to this

   Private Sub frmMemberList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim recCounter As Integer
		recCounter = NumberOfRecords(FOLDER)
		if recCounter > 0 then
		   Dim person(recCounter - 1) As Members
		   getData(person, recCounter)
		   sortData(person, recCounter)
		   showData(person, recCounter)
		else
		   MessageBox.Show("There are no records in MEMBERPHONES.TXT","No Records",MessageBoxButtons.OK,MessageBoxIcon.Error)
			'you might want to add some code later on do do the form load event again if you do find a 
			'file that has records.
		end if
	End Sub



So try this code first. If if is able to run without the message boxes, we are on the right path and can inspect the rest of the code.

:^:
Was This Post Helpful? 0
  • +
  • -

#9 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Just need a little lesson

Posted 27 October 2009 - 05:38 AM

View Postmotcom, on 26 Oct, 2009 - 10:39 PM, said:

Hi,

Lets start to debug...
Debugging is a programmers best tool.

I see you have this statement

   Const FOLDER As String = "MEMBERPHONES.TXT"



and you start using it here

	recCounter = NumberOfRecords(FOLDER)



What is the value of recCounter?
(i have a hunch that its 0)

So lets make sure we find the file "MEMBERPHONES.TXT"
to start off with lets move the file to "C:\" and not in the debug bin.

Change the "Const FOLDER As String = "MEMBERPHONES.TXT" to this

	Const FOLDER As String = "C:\MEMBERPHONES.TXT"



Now go to the NumberOfRecords() sub and change the code to this

Function NumberOfRecords(ByVal filespec As String) As Integer
		If My.Computer.FileSystem.FileExists(filespec) = false then
		   MessageBox.Show("Could not find the file MEMBERPHONES.TXT","Could not find file",MessageBoxButtons.OK,MessageBoxIcon.Error)
		   Return 0
		else
		   Dim member As String, number As Integer
		   Dim n As Integer
		   n = 0
		   Dim sr As StreamReader = File.OpenText(filespec)
		   Do While (sr.Peek <> -1)
			   member = sr.ReadLine
			   number = CInt(sr.ReadLine)
			   n += 1
		   Loop
		   sr.Close()
		   Return n
		end if 
	End Function



now that we can see if a file exists, we make sure that we continue if the file actually contains some data.
So lets modify the form load event to this

   Private Sub frmMemberList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim recCounter As Integer
		recCounter = NumberOfRecords(FOLDER)
		if recCounter > 0 then
		   Dim person(recCounter - 1) As Members
		   getData(person, recCounter)
		   sortData(person, recCounter)
		   showData(person, recCounter)
		else
		   MessageBox.Show("There are no records in MEMBERPHONES.TXT","No Records",MessageBoxButtons.OK,MessageBoxIcon.Error)
			'you might want to add some code later on do do the form load event again if you do find a 
			'file that has records.
		end if
	End Sub



So try this code first. If if is able to run without the message boxes, we are on the right path and can inspect the rest of the code.

:^:

I appreciate this...and now that I have slept on it, kind of, it makes more sense. I do have to say remaining the biggest pain on these forums, I am sure....is that I woke up after about an hour of sleep and thought I had the answer....and well...I got most of it. I only have to get the numbers to display in txtPhone.Text when the name is selected from the list, and then text my other procedures. It looks like this
Imports System.IO
Public Class frmMemberList
	Structure Members
		Dim member As String
		Dim number As Integer
	End Structure

	Const FOLDER As String = "MEMBERPHONES.TXT"
	Dim data(15) As Members
	Dim names() As String
	Dim numbers() As String

	Private Sub frmMemberList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		names = FillArray()

		displayData()

	End Sub

	Function FillArray()
		Dim names(14) As String
		Dim numbers(14) As String
		Dim sr As StreamReader = File.OpenText(FOLDER)
		For i As Integer = 0 To 14
			names(i) = sr.ReadLine
			numbers(i) = sr.ReadLine
		Next
		sr.Close()
		Return names
	End Function

	Sub displayData()
		lstDisplay.Items.Clear()
		lstDisplay.DataSource = names
	End Sub

	Private Sub lstDisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDisplay.SelectedIndexChanged
		txtName.Text = CStr(lstDisplay.Text)
	End Sub

	Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
		Dim message As String
		If (txtName.Text <> "") And (txtPhone.Text <> "") Then
			Dim sw As StreamWriter = File.AppendText(FOLDER)
			sw.WriteLine(txtName.Text)
			sw.WriteLine(txtPhone.Text)
			sw.Close()
			txtName.Clear()
			txtPhone.Clear()
			txtName.Focus()
			MessageBox.Show("File has been added.")
		Else
			message = "You must enter a name and seven digit phone number."
			MessageBox.Show(message, "Incomplete information.")
		End If
	End Sub

	Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDelete.Click
		Dim message As String
		If txtName.Text <> "" Then
			If File.Exists(FOLDER) Then
				deletePerson()
			Else
				message = "Either no file has yet been created or "
				message = message & "the file is not where expected."
				MessageBox.Show(message, "File Not Found.")
			End If
		Else
			MessageBox.Show("You must enter a name.", "Information Incomplete.")
		End If
		txtName.Focus()
	End Sub
	Sub deletePerson()
		Dim name As String, number As String
		Dim foundFlag As Boolean = False
		Dim sr As StreamReader = File.OpenText(FOLDER)
		Dim sw As StreamWriter = File.CreateText("TEMP.TXT")
		Do While (sr.Peek <> -1)
			name = sr.ReadLine
			number = sr.ReadLine
			If (name <> txtName.Text) Then
				sw.WriteLine(name)
				sw.WriteLine(number)
			Else
				foundFlag = True
			End If
		Loop
		sr.Close()
		sw.Close()
		File.Delete(FOLDER)
		File.Move("TEMP.TXT", FOLDER)
		MessageBox.Show("Record deleted from file.")
		If Not foundFlag Then
			MessageBox.Show("The record was not found.")
		Else
			txtName.Clear()
			txtPhone.Clear()
		End If
	End Sub


	Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
		End
	End Sub

	Private Sub mnuModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuModify.Click
	End Sub
End Class


So I just need as I said the numbers into an array(somehow...the way my professor told me did not work) and then they have to display when a member name is selected. And then I have to make the menu button procedure for modify which will just basically change anything about existing data when a change is made in the txtboxes.
Was This Post Helpful? 0
  • +
  • -

#10 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Just need a little lesson

Posted 27 October 2009 - 07:29 AM

Hi,

I modified your code quite a bit...

I added an id to hold the record numbers so you can tell the lstDetails to be sorted automatically and it should work

also i added properties to your structure so you can bind it to the lstDetails.

i also updated the Add button to do a save as well but you can modify it later on.

the delete button is working but everytime you delete you need to load the file again.....

Imports System.IO
Public Class frmMemberList
	Structure Members
		'added id so we know which record this is
		'because there can be more than one person with the same name
		'you might want to add this to your "MEMBERPHONES.TXT"
		Dim _ID As Integer
		'adding properies so we can bind them to controls
		Public Property ID() As Integer
			Get
				Return _ID
			End Get
			Set(ByVal value As Integer)
				_ID = value
			End Set
		End Property

		Dim _member As String
		'adding properies so we can bind them to controls
		Public Property Member() As String
			Get
				Return _member
			End Get
			Set(ByVal value As String)
				_member = value
			End Set
		End Property
		'adding properies so we can bind them to controls
		Dim _number As Integer
		Public Property Number() As Integer
			Get
				Return _number
			End Get
			Set(ByVal value As Integer)
				_number = value
			End Set
		End Property

	End Structure

	Const FOLDER As String = "MEMBERPHONES.TXT"
	'Setting first record already.....
	Dim data() As Members
	'this keeps track of how many records there are
	Private Records As Integer = 0

	Private Sub frmMemberList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		LoadData()
	End Sub

	Private Sub LoadData()

		Dim sr As StreamReader = File.OpenText(FOLDER)

		ReDim data(0)
		Records = 0
		Do While sr.EndOfStream = False
			data(Records).ID = Records
			data(Records).member = sr.ReadLine
			data(Records).number = sr.ReadLine
			Records += 1
			'increasing the data variable and keeping existing data
			ReDim Preserve data(Records)
		Loop

		sr.Close()




		'as you can see the property "ID" can now be bound to value member
		lstDisplay.ValueMember = "ID"
		'and the property "Member" can now be bound to Display Member
		lstDisplay.DisplayMember = "Member"

		lstDisplay.DataSource = data
	End Sub

	Private Sub lstDisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDisplay.SelectedIndexChanged
		txtName.Text = data(lstDisplay.SelectedValue).Member
		txtPhone.Text = data(lstDisplay.SelectedValue).Number
	End Sub

	Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
		If mnuAdd.Text = "Save" Then
			Dim message As String
			If (txtName.Text <> "") And (txtPhone.Text <> "") Then
				Dim sw As StreamWriter = File.AppendText(FOLDER)
				sw.WriteLine(txtName.Text)
				sw.WriteLine(txtPhone.Text)
				sw.Close()

				MessageBox.Show("File has been added.")
				'Added as record so lets increase the record size

				ReDim Preserve data(Records)
				data(Records).ID = Records
				data(Records).Member = txtName.Text
				data(Records).Number = txtPhone.Text
				lstDisplay.DataSource = data
				Records += 1
			Else
				message = "You must enter a name and seven digit phone number."
				MessageBox.Show(message, "Incomplete information.")
			End If
			mnuAdd.Text = "Add"
		Else
			mnuAdd.Text = "Save"
			txtName.Text = ""
			txtPhone.Text = ""
		End If
	End Sub

	Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDelete.Click
		Dim message As String
		If txtName.Text <> "" Then
			If File.Exists(FOLDER) Then
				deletePerson()
			Else
				message = "Either no file has yet been created or "
				message = message & "the file is not where expected."
				MessageBox.Show(message, "File Not Found.")
			End If
		Else
			MessageBox.Show("You must enter a name.", "Information Incomplete.")
		End If
		txtName.Focus()
	End Sub
	Sub deletePerson()
		Dim name As String, number As String
		Dim foundFlag As Boolean = False
		Dim sr As StreamReader = File.OpenText(FOLDER)
		Dim sw As StreamWriter = File.CreateText("TEMP.TXT")
		Do While (sr.Peek <> -1)
			name = sr.ReadLine
			number = sr.ReadLine
			If (name <> txtName.Text) Then
				sw.WriteLine(name)
				sw.WriteLine(number)
			Else
				foundFlag = True
			End If
		Loop
		sr.Close()
		sw.Close()
		File.Delete(FOLDER)
		File.Move("TEMP.TXT", FOLDER)
		MessageBox.Show("Record deleted from file.")
		If Not foundFlag Then
			MessageBox.Show("The record was not found.")
		Else
			txtName.Clear()
			txtPhone.Clear()
		End If
		'need to load the data again since we can not delete from an array
		'sorry......
		LoadData()
	End Sub


	Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
		Me.Close()
	End Sub

	'Private Sub mnuModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuModify.Click
	'End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#11 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Just need a little lesson

Posted 27 October 2009 - 07:52 AM

Hey,
I just wanted to say thank you. I see what you did with the ID thing, though I can honeslty say I have never used those commands or even learned about them thus far.

That being said, we (as this is an assignment) are not allowed to modify the contents of the txt file in any way other than through our program ie:we can only add, modify, or delete files adding more to the file structure is apprently off limits. So as I appreciate your help and I see what you are doing, I have to try to read it in and access it without adding id numbers to the lines. Though I was curious, I thought I remembered a way to count the lines like with passnum or something, is there a way I can assign the names and numbers to separate arrays and then access them? I do have to also figure out...and I put in an email to my professor on this one, how to make sure the array is not just limited to the txt files original contents, it has to be expandable and I know how to do that just kinda in writers block at the moment.
Was This Post Helpful? 0
  • +
  • -

#15 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Just need a little lesson

Posted 27 October 2009 - 11:00 PM

Hi,

The content of the text file is not changed. The ID is just there to identify the record, thats all. (it helps if you bind the control to the structure and getting the proper record)

You will however get a problem if you have 2 people with the same name, even worse if they have the same number.

You can remove the id, and the properties.

Imports System.IO
Public Class frmMemberList
	Structure Members
		Dim member As String
		Dim number As Integer
	End Structure

	Const FOLDER As String = "MEMBERPHONES.TXT"
	'Setting first record already.....
	Dim data() As Members
	'this keeps track of how many records there are
	Private Records As Integer = 0

	Private Sub frmMemberList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		LoadData()
	End Sub

	Private Sub LoadData()

		Dim sr As StreamReader = File.OpenText(FOLDER)

		ReDim data(0)
		Records = 0
		Do While sr.EndOfStream = False
			data(Records).member = sr.ReadLine
			data(Records).number = sr.ReadLine
			lstDisplay.Items.Add(data(Records).member)
			Records += 1
			'increasing the data variable and keeping existing data
			ReDim Preserve data(Records)
		Loop
		sr.Close()
	End Sub

	Private Sub lstDisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDisplay.SelectedIndexChanged
		txtName.Text = data(lstDisplay.SelectedIndex).member
		txtPhone.Text = data(lstDisplay.SelectedIndex).number
	End Sub

	Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
		If mnuAdd.Text = "Save" Then
			Dim message As String
			If (txtName.Text <> "") And (txtPhone.Text <> "") Then
				Dim sw As StreamWriter = File.AppendText(FOLDER)
				sw.WriteLine(txtName.Text)
				sw.WriteLine(txtPhone.Text)
				sw.Close()

				MessageBox.Show("File has been added.")
				'Added as record so lets increase the record size

				ReDim Preserve data(Records)
				data(Records).member = txtName.Text
				data(Records).Number = txtPhone.Text
				lstDisplay.DataSource = data
				Records += 1
			Else
				message = "You must enter a name and seven digit phone number."
				MessageBox.Show(message, "Incomplete information.")
			End If
			mnuAdd.Text = "Add"
		Else
			mnuAdd.Text = "Save"
			txtName.Text = ""
			txtPhone.Text = ""
		End If
	End Sub

	Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDelete.Click
		Dim message As String
		If txtName.Text <> "" Then
			If File.Exists(FOLDER) Then
				deletePerson()
			Else
				message = "Either no file has yet been created or "
				message = message & "the file is not where expected."
				MessageBox.Show(message, "File Not Found.")
			End If
		Else
			MessageBox.Show("You must enter a name.", "Information Incomplete.")
		End If
		txtName.Focus()
	End Sub
	Sub deletePerson()
		Dim name As String, number As String
		Dim foundFlag As Boolean = False
		Dim sr As StreamReader = File.OpenText(FOLDER)
		Dim sw As StreamWriter = File.CreateText("TEMP.TXT")
		Do While (sr.Peek <> -1)
			name = sr.ReadLine
			number = sr.ReadLine
			If (name <> txtName.Text) Then
				sw.WriteLine(name)
				sw.WriteLine(number)
			Else
				foundFlag = True
			End If
		Loop
		sr.Close()
		sw.Close()
		File.Delete(FOLDER)
		File.Move("TEMP.TXT", FOLDER)
		MessageBox.Show("Record deleted from file.")
		If Not foundFlag Then
			MessageBox.Show("The record was not found.")
		Else
			txtName.Clear()
			txtPhone.Clear()
		End If
		'need to load the data again since we can not delete from an array
		'sorry......
		LoadData()
	End Sub


	Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
		Me.Close()
	End Sub

	'Private Sub mnuModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuModify.Click
	'End Sub
End Class


Was This Post Helpful? 1

#16 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Just need a little lesson

Posted 29 October 2009 - 11:18 AM

Just wanted to say thanks to all those who helped. I got it figured out though a bit crude it worked. The ideas and help were outstanding and speedy though so I wanted to thank you all, I definately picked up some education here.
Was This Post Helpful? 0
  • +
  • -

#17 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Just need a little lesson

Posted 29 October 2009 - 11:40 PM

Cool

Use the "this post was helpfull" button
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1