11 Replies - 15740 Views - Last Post: 30 March 2011 - 11:52 AM Rate Topic: -----

#1 dranfu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Using a .txt file to populate a list box

Posted 14 December 2008 - 04:09 PM

Hello,

I'm new to the forum, but it looks like a great place to learn, so I'm glad I discovered it.

Anyhow, I'm very new to VB.net/Vb2005. I just finished my first semester learning VB and I'm working on a few personal projects to solidify/increase what I've learned this year. Right now I'm building a simple inventory application that will keep track of individual users at my job.

The problem I'm having is in trying to populate a comboBox with items from a text file. My goal is that when the application loads, a comboBox will be populated with the names of users(located in a text file.) Upon selecting a user, the application will read some other(like users name, windows version, computer name, etc) data from another text file and populate the applications text boxes with that information.

I know how to go about building most of the application, but the problem I'm running into is how to use a text file to populate the comboBox. If I use an array or a collection for the comboBox data, then the user list will be lost each time the application closes. Also, whenver a new user is added, that data will be lost, because the collection or orray will be lost each time the application ends. Also, I don't want to use a database for the program, just a simple file.

Here is the code I have worked out so far.

Imports System.IO

Public Class MyInventoryForm

   Private output As StreamWriter





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

	  Dim userNames As String = "user_names.txt"
	  userComboBox.DataSource = userNames



   End Sub ' Form Load

   

   Private Sub NewUserAddUserButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewUserAddUserButton.Click, Button1.Click

   End Sub

   
   Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
	  Dim filename As String = ("user_data.txt")

	  'Create new stream to append data to the file


	  output = New StreamWriter(filename, True)
	  output.WriteLine(lastNameTextBox.Text)
	  output.WriteLine(firstNameTextBox.Text)
	  output.WriteLine(departmentTextBox.Text)
	  output.WriteLine(extensionTextBox.Text)
	  output.WriteLine(cellPhoneTextBox.Text)
	  output.WriteLine(supervisorExtensionTextBox.Text)
	  output.WriteLine(adUserNameTextBox.Text)
	  output.WriteLine(adPasswordTextBox.Text)
	  output.WriteLine(adEmailTextBox.Text)
	  output.WriteLine(adWebAccessComboBox.SelectedItem)
	  output.WriteLine(compNameTextBox.Text)
	  output.WriteLine(IpAddressTextBox.Text)
	  output.WriteLine(winVerTextBox.Text)
	  output.WriteLine(memoryTextBox.Text)
	  output.WriteLine(hardDiskTextBox.Text)
	  output.WriteLine(cpuSpeedTextBox.Text)
	  output.WriteLine("")
	  output.Close()
	  saveButton.Enabled = False
	  DisableInput()

   End Sub
   Private Sub DisableInput()
	  lastNameTextBox.Enabled = False
	  firstNameTextBox.Enabled = False
	  departmentTextBox.Enabled = False
	  extensionTextBox.Enabled = False
	  cellPhoneTextBox.Enabled = False
	  supervisorExtensionTextBox.Enabled = False
	  adUserNameTextBox.Enabled = False
	  adPasswordTextBox.Enabled = False
	  adEmailTextBox.Enabled = False
	  adWebAccessComboBox.Enabled = False
	  compNameTextBox.Enabled = False
	  IpAddressTextBox.Enabled = False
	  winVerTextBox.Enabled = False
	  memoryTextBox.Enabled = False
	  hardDiskTextBox.Enabled = False
	  cpuSpeedTextBox.Enabled = False

   End Sub
End Class



When I try to use a text file as the data source of the comboBox, I receive this error: "ystem.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource." I'm assuming this is because the comboBox is expecting a collection or an array as its data. But there must be a way to use a text file to populate a comboBox, right? Otherwise, the data would always be lost?

I'm sure I'm missing something here. Any ideas would be very appreciated:)

Is This A Good Question/Topic? 0
  • +

Replies To: Using a .txt file to populate a list box

#2 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Re: Using a .txt file to populate a list box

Posted 14 December 2008 - 06:15 PM

Here is what I use to open a txt file but I'm not using .net System.IO. I'm using VB Library file instead. Maybe it will work for you to or another post will demonstrate the StreamReader for reading in the file.

See FileIO at the top there?
Also I defined my defualt file at module-level.

Lots of notes below and I used naming convention that might be easy to follow. If populating the list box when the program loads you might consider using a try-catch block. In place of putting this code in the Form_Load procedure I called it from there.

Open the file and read a line, add it to list box. loop. read next line and add it to list box. Stops at EOF. The exception calls another procedure if the file failed to open.

I was reading in catalogs names into a combo box. You are at least doing something very similar.

See if this helps. ddollar

Option Strict On ' strongly typed - you should do this too.
Imports Microsoft.VisualBasic.FileIO ' using VS library file knowing its not standard dot-net.
Imports System.IO ' also using standard dot-net library streams with small amounts of data.

Public Class MyMainForm

' one to handle tracking changes to a file that user wants to save.
Private FileIsDirtyBoolean As Boolean = False

' one to track the current active data file.
Private ActiveUserTextFile As String = "ListOfCatalogs.txt"

Private Sub OpenReadTextFile()
' this procedure attempts to open/read a text file needed for the catalog names
' combo box. works with success and failure. used vb library. its only
' used when the program is loaded. other places in the program use the system.io stream library.

' identify specific file (in bin/Debug folder).
' correct file name is ListOfCatalogs.txt.
Dim OpenFileString As String = ActiveUserTextFile

Try
' attempt to open/read file using simplified VB.FileIO library.
' no need to specify type or delimiter, just open the file.
CatalogListTextFieldParser = New TextFieldParser(OpenFileString)

' if the open/read was successful, populate combo box with file contents.
Dim CatalogListItemString As String

' loop and read-in each line until end-of-file.
Do Until CatalogListTextFieldParser.EndOfData

CatalogListItemString = CatalogListTextFieldParser.ReadLine()
CatalogNamesComboBox.Items.Add(CatalogListItemString)

Loop

CatalogListTextFieldParser.Close() ' always close resourse when prudent.
OpenWriteTextFile(OpenFileString) ' call stream writer to handle any data changes.

Catch ex As Exception

'handle exception by calling OpenReadTextFileFailed procedure.
OpenReadTextFileFailed(OpenFileString)
End Try

End Sub


This post has been edited by ddollar: 14 December 2008 - 06:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 dranfu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Re: Using a .txt file to populate a list box

Posted 14 December 2008 - 08:28 PM

Hi ddolar,

Thanks for the indepth reply. As I was reading more about files/streams, and collections, I realized that what I needed to do was to assign the names( the first name, last name) of the user list text file to a collection. And then load that collection as the data source for the ComboBox. And as you mentioned, In the forms load event handler, I can use streamReader to read each line of the user list, assign it to a collection, and then assign that collection as the source of the comboBox.

Now, actually, you suggested using TextFieldParser, which is class I'm not familiar with yet, but it seems to be fairly similar to StreamWriter/StreamReader. I'm going to read more about it later, as well as the Try Catch statement(which we did not get into this semester). I have about 2 -3 hours more tonight that I can work on the App, and then I have to go to bed, but after work tommorow, I will spend at least 7 good hours on the application, and I will spend some time learning about the Try/Catch statement and let you know how i,t works.

I'm still going over your code, but thank you so much for commenting it very nicely :) It was much easier to follow.

I'll get you an update tommorow.
Was This Post Helpful? 0
  • +
  • -

#4 dranfu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Re: Using a .txt file to populate a list box

Posted 15 December 2008 - 09:12 AM

Ok, everything worked pretty well yesterday after using streamReader to read the lines of the text file and then assign each name to a collection. However, now I am trying to work on the edit function of the program: When the user clicks the edit button, they should be able to edit the values in the text boxes and then replace those values in the text file.

I read about the members in the TextField Parser and StreamReader/Writer classes, but I did not find any properties or methods that will erase a line of text. Am I missing something?


View Postdranfu, on 14 Dec, 2008 - 07:28 PM, said:

Hi ddolar,

Thanks for the indepth reply. As I was reading more about files/streams, and collections, I realized that what I needed to do was to assign the names( the first name, last name) of the user list text file to a collection. And then load that collection as the data source for the ComboBox. And as you mentioned, In the forms load event handler, I can use streamReader to read each line of the user list, assign it to a collection, and then assign that collection as the source of the comboBox.

Now, actually, you suggested using TextFieldParser, which is class I'm not familiar with yet, but it seems to be fairly similar to StreamWriter/StreamReader. I'm going to read more about it later, as well as the Try Catch statement(which we did not get into this semester). I have about 2 -3 hours more tonight that I can work on the App, and then I have to go to bed, but after work tommorow, I will spend at least 7 good hours on the application, and I will spend some time learning about the Try/Catch statement and let you know how i,t works.

I'm still going over your code, but thank you so much for commenting it very nicely :) It was much easier to follow.

I'll get you an update tommorow.

Was This Post Helpful? 0
  • +
  • -

#5 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Re: Using a .txt file to populate a list box

Posted 15 December 2008 - 01:11 PM

Hi,

Got some code for that? I remember seeing something in your code that looked like you were disabling a number of text boxes. Does your edit_button enable those boxes so the user can edit the text? Then you want to 'save' the 'changes' to the text file you already opened and read, updating the list?

Some code would be handy.

ddollar
Was This Post Helpful? 0
  • +
  • -

#6 dranfu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Re: Using a .txt file to populate a list box

Posted 15 December 2008 - 03:59 PM

Yes, I have a couple of procedures that that disabled the text boxes and enable them when called. But I haven't written anything for the edit function, as I am still working out the code for the comboBox SelectedIndexChanged event handler.

I have some code for a nested control statement that I'm trying to work out. Basically, when the user selects the user name from the combo box, I want to the StreamReader to compare that line to the name that is selected in the comboBox. If they are the same line, then I assign each line in the text file to the text boxes(which is the specific data for that use).

That works fine when the first line in the user_data text file is the first option in the comboBox; so that when the program loads, it loads all that users data just fine. However, once I try to use a nested control statement to read the other lines in the text something has gone wrong. I get nothing but blank, empty boxes?

I am still reading on about it, though. Perhaps I am not using the StreamReader readline class properly?

Here is my updated code: I've highlighted the ComboBox's event handler, which handles the nested control statement.

Imports System.IO

Public Class MyInventoryForm

   Private output_2 As StreamWriter 'writes to the user_names text file
   Private output As StreamWriter ' writes to the user data text file
   Private input As StreamReader ' reads data from user_names text file
   'holds the list of users, displayed as last name, first name
   'in the comboBox
   Private input_2 As StreamReader 'used to compare the values in combox to display
   Private input_3 As StreamReader 'reads values from user_data text file

   Private user_List As Collections.ArrayList







   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	  user_List = New Collections.ArrayList()
	  Dim userNames As String = "user_names.txt"
	  input = New StreamReader(userNames)

	  Do While input.Peek > -1
		 user_List.Add(input.ReadLine)

	  Loop
	  input.close

	  userComboBox.DataSource = user_List



   End Sub ' Form Load



   


   Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
	  Dim user_data As String = ("user_data.txt")
	  Dim user_list As String = ("user_names.txt")



	  'Create new stream to append data to the file


	  output = New StreamWriter(user_data, True)
	  output.WriteLine(fullNameTextBox.Text)
	  output.WriteLine(firstNameTextBox.Text)
	  output.WriteLine(departmentTextBox.Text)
	  output.WriteLine(extensionTextBox.Text)
	  output.WriteLine(cellPhoneTextBox.Text)
	  output.WriteLine(supervisorExtensionTextBox.Text)
	  output.WriteLine(adUserNameTextBox.Text)
	  output.WriteLine(adPasswordTextBox.Text)
	  output.WriteLine(adEmailTextBox.Text)
	  output.WriteLine(adWebAccessComboBox.SelectedItem)
	  output.WriteLine(compNameTextBox.Text)
	  output.WriteLine(IpAddressTextBox.Text)
	  output.WriteLine(winVerTextBox.Text)
	  output.WriteLine(memoryTextBox.Text)
	  output.WriteLine(hardDiskTextBox.Text)
	  output.WriteLine(cpuSpeedTextBox.Text)
	  output.WriteLine("")
	  output.Close()

	  output_2 = New StreamWriter(user_list, True)
	  output_2.WriteLine(fullNameTextBox.Text)
	  output_2.Close()

	  saveButton.Enabled = False
	  DisableInput()

   End Sub
   Private Sub DisableInput()
	  fullNameTextBox.Enabled = False
	  firstNameTextBox.Enabled = False
	  departmentTextBox.Enabled = False
	  extensionTextBox.Enabled = False
	  cellPhoneTextBox.Enabled = False
	  supervisorExtensionTextBox.Enabled = False
	  adUserNameTextBox.Enabled = False
	  adPasswordTextBox.Enabled = False
	  adEmailTextBox.Enabled = False
	  adWebAccessComboBox.Enabled = False
	  compNameTextBox.Enabled = False
	  IpAddressTextBox.Enabled = False
	  winVerTextBox.Enabled = False
	  memoryTextBox.Enabled = False
	  hardDiskTextBox.Enabled = False
	  cpuSpeedTextBox.Enabled = False

   End Sub

   Private Sub EnableInput()
	  fullNameTextBox.Enabled = True
	  firstNameTextBox.Enabled = True
	  departmentTextBox.Enabled = True
	  extensionTextBox.Enabled = True
	  cellPhoneTextBox.Enabled = True
	  supervisorExtensionTextBox.Enabled = True

	  adUserNameTextBox.Enabled = True

	  adPasswordTextBox.Enabled = True

	  adEmailTextBox.Enabled = True

	  adWebAccessComboBox.Enabled = True

	  compNameTextBox.Enabled = True

	  IpAddressTextBox.Enabled = True
	  winVerTextBox.Enabled = True
	  memoryTextBox.Enabled = True
	  hardDiskTextBox.Enabled = True
	  cpuSpeedTextBox.Enabled = True
   End Sub

   Private Sub NewUserAddUserButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewUserAddUserButton.Click
	  EnableInput()
	  ClearControls()
	  saveButton.Enabled = True



   End Sub

   Private Sub ClearControls()
	  fullNameTextBox.Text = ""
	  firstNameTextBox.Text = ""
	  departmentTextBox.Text = ""
	  extensionTextBox.Text = ""
	  cellPhoneTextBox.Text = ""
	  supervisorExtensionTextBox.Text = ""
	  adUserNameTextBox.Text = ""
	  adPasswordTextBox.Text = ""
	  adEmailTextBox.Text = ""
	  compNameTextBox.Text = ""
	  IpAddressTextBox.Text = ""
	  winVerTextBox.Text = ""
	  memoryTextBox.Text = ""
	  hardDiskTextBox.Text = ""
	  cpuSpeedTextBox.Text = ""

   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	  End 'This terminates the application
   End Sub




   [color=#FFFF33]Private Sub userComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles userComboBox.SelectedIndexChanged
	  ClearControls()

	  Dim userNames_2 As String = "user_names.txt"
	  input_2 = New StreamReader(userNames_2)
	  Dim user_data2 As String = "user_data.txt"
	  input_3 = New StreamReader(user_data2)
	  Dim line As String = input_3.ReadLine
	  Dim lineReader As Integer 'Counts lines of selected user to display
	  Dim lineSkipper As Integer ' Counts lines to skip


	  '----------If the selected name in combo box matches the name in the
	  '----------user data text file, then add that data, starting at line one
	  '----------of the text file, to the text boxes.
	  If userComboBox.SelectedItem = line Then
		 fullNameTextBox.Text = input_3.ReadLine
		 firstNameTextBox.Text = input_3.ReadLine
		 departmentTextBox.Text = input_3.ReadLine
		 extensionTextBox.Text = input_3.ReadLine
		 cellPhoneTextBox.Text = input_3.ReadLine
		 supervisorExtensionTextBox.Text = input_3.ReadLine
		 adUserNameTextBox.Text = input_3.ReadLine
		 adPasswordTextBox.Text = input_3.ReadLine
		 adEmailTextBox.Text = input_3.ReadLine
		 compNameTextBox.Text = input_3.ReadLine
		 IpAddressTextBox.Text = input_3.ReadLine
		 winVerTextBox.Text = input_3.ReadLine
		 memoryTextBox.Text = input_3.ReadLine
		 hardDiskTextBox.Text = input_3.ReadLine
		 cpuSpeedTextBox.Text = input_3.ReadLine
		 input_3.Close()

	  Else

		 '-----------While there is still more text to be read, skip exactly 16 lines
		 '-----------down the text file. 
		 Do While input_3.Peek > -1

			For lineReader = 0 To 16
			   input_3.ReadLine()

			Next

			'-----------------Test again to see if now the string assigned to input_3.Readline
			'-----------------is the same as the string in the combo box

			Dim line_2 As String = input_3.ReadLine

			If userComboBox.SelectedItem = line_2 Then

			   fullNameTextBox.Text = input_3.ReadLine
			   firstNameTextBox.Text = input_3.ReadLine
			   departmentTextBox.Text = input_3.ReadLine
			   extensionTextBox.Text = input_3.ReadLine
			   cellPhoneTextBox.Text = input_3.ReadLine
			   supervisorExtensionTextBox.Text = input_3.ReadLine
			   adUserNameTextBox.Text = input_3.ReadLine
			   adPasswordTextBox.Text = input_3.ReadLine
			   adEmailTextBox.Text = input_3.ReadLine
			   compNameTextBox.Text = input_3.ReadLine
			   IpAddressTextBox.Text = input_3.ReadLine
			   winVerTextBox.Text = input_3.ReadLine
			   memoryTextBox.Text = input_3.ReadLine
			   hardDiskTextBox.Text = input_3.ReadLine
			   cpuSpeedTextBox.Text = input_3.ReadLine

			   input_3.Close()
			   Exit Do
			End If

		 Loop 'Loop until this condition is met

	  End If



   End Sub[/color]
End Class






View Postddollar, on 15 Dec, 2008 - 12:11 PM, said:

Hi,

Got some code for that? I remember seeing something in your code that looked like you were disabling a number of text boxes. Does your edit_button enable those boxes so the user can edit the text? Then you want to 'save' the 'changes' to the text file you already opened and read, updating the list?

Some code would be handy.

ddollar

Was This Post Helpful? 0
  • +
  • -

#7 dranfu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Re: Using a .txt file to populate a list box

Posted 15 December 2008 - 06:57 PM

Ok, I was able to figure out what was wrong with my loop. I was correctly advancing through the lines in the user_data text file with the For...Next loop, but I forgot to add another (line = input_3.ReadLine) to read the current line that the streamReader winds up at, after advancing through the text file. After adding the corrections, when the user selects the user in the comboBox, the correct information is loaded in the text boxes.

Note that, as you suggested, I have Option Strict turned on. Explicitly converting everything.

The resolution code:

Private Sub userComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles userComboBox.SelectedIndexChanged
	  ClearControls()

	  Dim userNames_2 As String = "user_names.txt"
	  input_2 = New StreamReader(userNames_2)
	  Dim user_data2 As String = "user_data.txt"
	  input_3 = New StreamReader(user_data2)
	  Dim line As String = input_3.ReadLine
	  Dim lineReader As Integer 'Counts lines of selected user to display
	  Dim lineSkipper As Integer ' Counts lines to skip


	  '----------If the selected name in combo box matches the name in the
	  '----------user data text file, then add that data, starting at line one
	  '----------of the text file, to the text boxes.
	  Do While userComboBox.Text IsNot line AndAlso input_3.Peek > -1

		 If Convert.ToString(userComboBox.SelectedItem) = line Then
			fullNameTextBox.Text = input_3.ReadLine
			departmentTextBox.Text = input_3.ReadLine
			extensionTextBox.Text = input_3.ReadLine
			cellPhoneTextBox.Text = input_3.ReadLine
			supervisorExtensionTextBox.Text = input_3.ReadLine
			adUserNameTextBox.Text = input_3.ReadLine
			adPasswordTextBox.Text = input_3.ReadLine
			adEmailTextBox.Text = input_3.ReadLine
			compNameTextBox.Text = input_3.ReadLine
			IpAddressTextBox.Text = input_3.ReadLine
			winVerTextBox.Text = input_3.ReadLine
			memoryTextBox.Text = input_3.ReadLine
			hardDiskTextBox.Text = input_3.ReadLine
			cpuSpeedTextBox.Text = input_3.ReadLine
			Exit Do

		 Else

			For lineReader = 0 To 15
			   line = input_3.ReadLine
			Next

		 End If

		 line = input_3.ReadLine

	  Loop




   End Sub 'userComboBox_SelectedIndexChanged
End Class 'MyInventoryForm



Now I just need to find a Class that has the ability to remove lines of text. Or perhaps is there a string method or property that will let me do the same thing?
Was This Post Helpful? 0
  • +
  • -

#8 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Re: Using a .txt file to populate a list box

Posted 16 December 2008 - 10:30 AM

I don't know that you can remove a 'line' from a text file. Is that what you are thinking? If the user changes the item, you would update the text file by replacing the particular line?

I see you are using an arraylist. At least populating one; used it to add items to the listbox, but then ignoring it.

So you want to 'save changes' to the text file, is that right? The user makes a change to one or more items so the file needs to be updated. You could update the arraylist using .RemoveAt; then replace the file with the arraylist contents.

How's that? Or am I off track?

ddollar
Was This Post Helpful? 0
  • +
  • -

#9 dranfu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Re: Using a .txt file to populate a list box

Posted 17 December 2008 - 07:20 PM

Actually, that is an interesting idea. I didn't think of using an array list to store all the items the user entered. After all, I could just convert the numeric values they enter to strings, and visa versa. However, I think I'll be able to accomplish the edit function with simply using a Do Loop and searching for my primary key(their last name and first name) and then when found, I can use a For...Next statement to advance through each line of the file replace each string with the replace method. Or I'll have to rebuild all the strings with the string builder Class, and then I can use the built in remove method of the string builder class.

The problem I have right now, is that when the form loads, it uses a StreamReader object to read a list of names from a text file, and then assigns each of those names to the user list collection. Then, the user list collection is used as the data source for the combo box. Example below:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	  user_List = New Collections.ArrayList()
	  Dim userNames As String = "user_names.txt"
	  input = New StreamReader(userNames)

	  Do While input.Peek > -1
		 user_List.Add(input.ReadLine)

	  Loop
	  input.Close()

	  userComboBox.DataSource = user_List

   End Sub ' Form Load



Unfortunately, now I need to make it so that when the user hits the save button, it will update the collections list with the newly added user. But when I try to re-assign a new collection, one that includes the new user name, to the comboBox, I get an error stating that comboBoxes only except Ilist objects as data sources. Example Below:

Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
	  Dim user_data As String = ("user_data.txt")
	  Dim user_list As String = ("user_names.txt")



	  'Create new stream to append data to the file


	  output = New StreamWriter(user_data, True)
	  output.WriteLine((lastNameTextBox.Text) & ", " & (firstNameTextBox.Text))
	  output.WriteLine(departmentTextBox.Text)
	  output.WriteLine(extensionTextBox.Text)
	  output.WriteLine(cellPhoneTextBox.Text)
	  output.WriteLine(supervisorExtensionTextBox.Text)
	  output.WriteLine(adUserNameTextBox.Text)
	  output.WriteLine(adPasswordTextBox.Text)
	  output.WriteLine(adEmailTextBox.Text)
	  output.WriteLine(Convert.ToString(adWebAccessComboBox.SelectedItem))
	  output.WriteLine(compNameTextBox.Text)
	  output.WriteLine(IpAddressTextBox.Text)
	  output.WriteLine(winVerTextBox.Text)
	  output.WriteLine(memoryTextBox.Text)
	  output.WriteLine(hardDiskTextBox.Text)
	  output.WriteLine(cpuSpeedTextBox.Text)
	  output.WriteLine("")
	  output.Close()

	  userComboBox.DataSource = user_list

	  output_2 = New StreamWriter(user_list, True)
	  output_2.WriteLine((lastNameTextBox.Text) & ", " & (firstNameTextBox.Text))
	  output_2.Close()

	  '-----------------------------------------
	  user_List2 = New Collections.ArrayList()
	  Dim userNames As String = "user_names.txt"
	  input_names = New StreamReader(userNames)

	  Do While input_names.Peek > -1
		 user_List2.Add(input_names.ReadLine)

	  Loop

	  Dim newUserList As Collections.ArrayList = New Collections.ArrayList()
	  newUserList = user_List2


	  input_names.Close()
	  userComboBox.DataSource = user_List2



	  '-----------------------------------------

	  saveButton.Enabled = False
	  DisableInput()

   End Sub


I only get this compiler error when I try to re-assign a data source to the comboBox when hitting save. If I take out the new array list, and leave off trying to re-assign the data source of the comboBox, everything works smoothly--everything except for the fact that if someone adds a new user, they have to restart the program to see the new user show up in the ComboBox :(

Here is the full code so far, for reference:

Imports System.IO

Public Class MyInventoryForm

   Private output_2 As StreamWriter 'writes to the user_names text file
   Private output As StreamWriter ' writes to the user data text file
   Private input As StreamReader ' reads data from user_names text file
   'holds the list of users, displayed as last name, first name
   'in the comboBox
   Private input_2 As StreamReader 'used to compare the values in combox to display
   Private input_3 As StreamReader 'reads values from user_data text file
   Private input_names As StreamReader ' second stream reader, adds to user_list2 collection

   Private user_List As Collections.ArrayList
   Private user_List2 As Collections.ArrayList








   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	  user_List = New Collections.ArrayList()
	  Dim userNames As String = "user_names.txt"
	  input = New StreamReader(userNames)

	  Do While input.Peek > -1
		 user_List.Add(input.ReadLine)

	  Loop
	  input.Close()

	  userComboBox.DataSource = user_List

   End Sub ' Form Load



   


   Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
	  Dim user_data As String = ("user_data.txt")
	  Dim user_list As String = ("user_names.txt")



	  'Create new stream to append data to the file


	  output = New StreamWriter(user_data, True)
	  output.WriteLine((lastNameTextBox.Text) & ", " & (firstNameTextBox.Text))
	  output.WriteLine(departmentTextBox.Text)
	  output.WriteLine(extensionTextBox.Text)
	  output.WriteLine(cellPhoneTextBox.Text)
	  output.WriteLine(supervisorExtensionTextBox.Text)
	  output.WriteLine(adUserNameTextBox.Text)
	  output.WriteLine(adPasswordTextBox.Text)
	  output.WriteLine(adEmailTextBox.Text)
	  output.WriteLine(Convert.ToString(adWebAccessComboBox.SelectedItem))
	  output.WriteLine(compNameTextBox.Text)
	  output.WriteLine(IpAddressTextBox.Text)
	  output.WriteLine(winVerTextBox.Text)
	  output.WriteLine(memoryTextBox.Text)
	  output.WriteLine(hardDiskTextBox.Text)
	  output.WriteLine(cpuSpeedTextBox.Text)
	  output.WriteLine("")
	  output.Close()

	  userComboBox.DataSource = user_list

	  output_2 = New StreamWriter(user_list, True)
	  output_2.WriteLine((lastNameTextBox.Text) & ", " & (firstNameTextBox.Text))
	  output_2.Close()

	  '-----------------------------------------
	  user_List2 = New Collections.ArrayList()
	  Dim userNames As String = "user_names.txt"
	  input_names = New StreamReader(userNames)

	  Do While input_names.Peek > -1
		 user_List2.Add(input_names.ReadLine)

	  Loop

	  Dim newUserList As Collections.ArrayList = New Collections.ArrayList()
	  newUserList = user_List2


	  input_names.Close()
	  userComboBox.DataSource = user_List2



	  '-----------------------------------------

	  saveButton.Enabled = False
	  DisableInput()

   End Sub
   Private Sub DisableInput()
	  lastNameTextBox.Enabled = False
	  firstNameTextBox.Enabled = False
	  departmentTextBox.Enabled = False
	  extensionTextBox.Enabled = False
	  cellPhoneTextBox.Enabled = False
	  supervisorExtensionTextBox.Enabled = False
	  adUserNameTextBox.Enabled = False
	  adPasswordTextBox.Enabled = False
	  adEmailTextBox.Enabled = False
	  adWebAccessComboBox.Enabled = False
	  compNameTextBox.Enabled = False
	  IpAddressTextBox.Enabled = False
	  winVerTextBox.Enabled = False
	  memoryTextBox.Enabled = False
	  hardDiskTextBox.Enabled = False
	  cpuSpeedTextBox.Enabled = False

   End Sub

   Private Sub EnableInput()
	  lastNameTextBox.Enabled = True
	  firstNameTextBox.Enabled = True
	  departmentTextBox.Enabled = True
	  extensionTextBox.Enabled = True
	  cellPhoneTextBox.Enabled = True
	  supervisorExtensionTextBox.Enabled = True

	  adUserNameTextBox.Enabled = True

	  adPasswordTextBox.Enabled = True

	  adEmailTextBox.Enabled = True

	  adWebAccessComboBox.Enabled = True

	  compNameTextBox.Enabled = True

	  IpAddressTextBox.Enabled = True
	  winVerTextBox.Enabled = True
	  memoryTextBox.Enabled = True
	  hardDiskTextBox.Enabled = True
	  cpuSpeedTextBox.Enabled = True
   End Sub

   Private Sub NewUserAddUserButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewUserAddUserButton.Click
	  EnableInput()
	  ClearControls()
	  saveButton.Enabled = True



   End Sub

   Private Sub ClearControls()
	  lastNameTextBox.Text = ""
	  firstNameTextBox.Text = ""
	  departmentTextBox.Text = ""
	  extensionTextBox.Text = ""
	  cellPhoneTextBox.Text = ""
	  supervisorExtensionTextBox.Text = ""
	  adUserNameTextBox.Text = ""
	  adPasswordTextBox.Text = ""
	  adEmailTextBox.Text = ""
	  compNameTextBox.Text = ""
	  IpAddressTextBox.Text = ""
	  winVerTextBox.Text = ""
	  memoryTextBox.Text = ""
	  hardDiskTextBox.Text = ""
	  cpuSpeedTextBox.Text = ""

   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
	  End 'This terminates the application
   End Sub




   Private Sub userComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles userComboBox.SelectedIndexChanged
	  ClearControls()


	  Dim user_data2 As String = "user_data.txt"
	  input_3 = New StreamReader(user_data2)
	  Dim line As String = input_3.ReadLine



	  '----------If the selected name in combo box matches the name in the
	  '----------user data text file, then add that data, starting at line one
	  '----------of the text file, to the text boxes.


	  If Convert.ToString(userComboBox.SelectedItem) = line Then
		 lastNameTextBox.Text = input_3.ReadLine
		 firstNameTextBox.Text = input_3.ReadLine
		 departmentTextBox.Text = input_3.ReadLine
		 extensionTextBox.Text = input_3.ReadLine
		 cellPhoneTextBox.Text = input_3.ReadLine
		 supervisorExtensionTextBox.Text = input_3.ReadLine
		 adUserNameTextBox.Text = input_3.ReadLine
		 adPasswordTextBox.Text = input_3.ReadLine
		 adEmailTextBox.Text = input_3.ReadLine
		 adWebAccessComboBox.SelectedItem = input_3.ReadLine
		 compNameTextBox.Text = input_3.ReadLine
		 IpAddressTextBox.Text = input_3.ReadLine
		 winVerTextBox.Text = input_3.ReadLine
		 memoryTextBox.Text = input_3.ReadLine
		 hardDiskTextBox.Text = input_3.ReadLine
		 cpuSpeedTextBox.Text = input_3.ReadLine
		 input_3.Close()

		 Exit Sub

	  Else

		 Do
			line = input_3.ReadLine

			If Convert.ToString(userComboBox.SelectedItem) = line Then
			   lastNameTextBox.Text = input_3.ReadLine
			   firstNameTextBox.Text = input_3.ReadLine
			   departmentTextBox.Text = input_3.ReadLine
			   extensionTextBox.Text = input_3.ReadLine
			   cellPhoneTextBox.Text = input_3.ReadLine
			   supervisorExtensionTextBox.Text = input_3.ReadLine
			   adUserNameTextBox.Text = input_3.ReadLine
			   adPasswordTextBox.Text = input_3.ReadLine
			   adEmailTextBox.Text = input_3.ReadLine
			   adWebAccessComboBox.SelectedItem = input_3.ReadLine
			   compNameTextBox.Text = input_3.ReadLine
			   IpAddressTextBox.Text = input_3.ReadLine
			   winVerTextBox.Text = input_3.ReadLine
			   memoryTextBox.Text = input_3.ReadLine
			   hardDiskTextBox.Text = input_3.ReadLine
			   cpuSpeedTextBox.Text = input_3.ReadLine
			   Exit Do

			End If

		 Loop While Convert.ToString(userComboBox.SelectedItem) <> line

	  End If




	  ' You might be able to get this to work with a For...Next Loop 
	  'For counter = 0 To input_3.Peek > -1

	  'Statements

	  'Next




   End Sub 'userComboBox_SelectedIndexChanged
End Class 'MyInventoryForm



Thanks for your help, too:) I'm reading over your code again right now, looking for clues and answers :)





View Postddollar, on 16 Dec, 2008 - 09:30 AM, said:

I don't know that you can remove a 'line' from a text file. Is that what you are thinking? If the user changes the item, you would update the text file by replacing the particular line?

I see you are using an arraylist. At least populating one; used it to add items to the listbox, but then ignoring it.

So you want to 'save changes' to the text file, is that right? The user makes a change to one or more items so the file needs to be updated. You could update the arraylist using .RemoveAt; then replace the file with the arraylist contents.

How's that? Or am I off track?

ddollar

Was This Post Helpful? 0
  • +
  • -

#10 jay_t55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-August 08

Re: Using a .txt file to populate a list box

Posted 30 April 2009 - 04:41 AM

Regarding your problem. I may have a solution and will post code when I'm fully awake (just woke up)...

You could try maybe a foreach statement. For example, Foreach line in textfile add line to combobox... Just iterate through all lines in the file. :-)
Was This Post Helpful? 0
  • +
  • -

#11 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Using a .txt file to populate a list box

Posted 30 April 2009 - 06:29 AM

this is an old thread. please check the date of the last post. thanks.
Was This Post Helpful? 0
  • +
  • -

#12 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Re: Using a .txt file to populate a list box

Posted 30 March 2011 - 11:52 AM

For newbies to VB (I'm using 2010), try this:

Imports System.IO

Public Class Form1

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

        Dim output As New StreamReader("user_names.txt")
        Dim userName As String = ""

        Do Until output.Peek = -1
            userName = output.ReadLine()
            userComboBox.Items.Add(userName)
        Loop

        output.Close()
        output.Dispose()

    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1