|
-
Sep 10th, 2004, 03:26 AM
#1
Thread Starter
Junior Member
storing list into an array
Hi, i have a csv file with two columns. column on is the name of a set of tools, and column 2 has the list of tools in each set.
ex.
Name of Set Names in set
Good number1
Good number2
Good number3
Bad number4
Bad number5
etc.
So far I have code that will separate the two columns and put them into listboxes. The first listbox contains only one of the set names, not all the multiple names since that would be redundant. The second listbox contains all the names of the set. However, what I want to do is when set "Good" is selected, only the names in the set appear in the 2nd listbox. How could I do that?
The code i have now to separate the lists is this...
Dim strPath As String
Dim SR As StreamReader
strPath = "C:\Desktop\csv1.csv"
SR = New StreamReader(strPath)
Do While SR.Peek() <> -1
Dim strfilestring As String = SR.ReadLine
If strfilestring <> "" Then
Dim filetext() As String = Split(strfilestring, ",")
Dim found As Boolean = False
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items.Item(i).ToString = filetext(0) Then
found = True
Exit For
End If
Next
If found = False Then
ListBox1.Items.Add(filetext(0))
End If
ListBox2.Items.Add(filetext(1))
End If
Loop
-
Sep 10th, 2004, 04:40 AM
#2
PowerPoster
Hi,
Pardon my ignorance but what is a "csv" file?
Apart from that, by placing
Dim filetext() As String = Split(strfilestring, ",")
Dim found As Boolean = False
in the loop you are creating many separate instances of them. You should either put the declarations outside of the loop and empty them each loop or keep them inside of the loop, disposing them at the end of the loop - but that is wasteful.
From your code it seems that each line in the file contains the name of the tool set followed by a "," followed by the name of one tool. How many names of tool sets are there? If it is not too onerous, how about having an array for each different set? You could then fill ListBox2 in accordance with the selection made in ListBox1. You could fill the arrays using a Select Case loop testing the value of filetext(0)
Is your present code working at all? If so what is the actual error/problem?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Sep 10th, 2004, 10:00 AM
#3
Addicted Member
Pardon my ignorance but what is a "csv" file?
Comma Separated Values file. Simliar to a comma-delimited ASCII text file, except I believe that the standard format has only string values placed inside double quotes, numbers and dates are not surrounded by double quotes.
Last edited by Qualm; Sep 10th, 2004 at 10:09 AM.
-
Sep 10th, 2004, 12:39 PM
#4
Thread Starter
Junior Member
Thanks for the reply. Right now, the code does work. And I could do it the way you supposed by having an array for each specific tool set. However, There are quite a few and I am trying to make it dynamic so that this list can be added onto. any suggestions?
-
Sep 10th, 2004, 05:38 PM
#5
PowerPoster
Hi,
You did not comment on the question of multiple instances of Filetext() and Found.
Does the file contain the name of the the set and one tool, delimited by "," (my thanks to Qualm ) on each line, or is the name of the set followed by more than one tool?
If there is only one tool to each line you could split the file, one line at a time into an array in the knowledge that each odd number element contained the set name and each even number the tool name. You could then allocate the set names to listbox1 by accessing the even numbers of the array (regard 0 as an even number) skipping duplicate names. Loop until the array upperbound-1 is reached. You then compare the selected Set name to the contents of the even numbered elements of the array and when a match is found, store the contents of the following array index (the next higher odd number) to the second listbox.
If there is more than one tool to each line we will have to think a little more.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Sep 11th, 2004, 12:58 AM
#6
Thread Starter
Junior Member
thanks again taxes. About your earlier comment, I put the two lines of code inside the loop because i need those actions to occur everytime it loops around. How would I make it more efficient outside?
Also, from your second reply. Are you saying to put the whole list into one gigantic array, and then only populate listbox1. then from the selecteditem from listbox1, compare that to all the even numbered elements in the big array and when a match is found, populate the 2nd listbox with the odd numbered array, and increment up for every following tool in that set.
so then would I have to do another streamreader that would this time instead of going line by line, just ReadToEnd? I"m assuming this would be the easiest.
Woutd you be able to code out an efficient way to do this briefly? I think I understand what you are trying to say, but I'm not sure if I'm familiar with all the syntax. Thanks a lot!!!
-
Sep 11th, 2004, 02:02 PM
#7
PowerPoster
Hi,
Looks like you have sorted this out in your new thread.
If not post again under this one.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Sep 11th, 2004, 02:24 PM
#8
Thread Starter
Junior Member
Taxes,
Thanks once again. I set i = 0 but it still didnt work. There is still nothing that shows up in listbox2. here's the code again if you wanted to see... Thanks again.
Dim strpath2 As String
Dim SR2 As StreamReader
strpath2 = "C:\csv1.csv"
SR2 = New StreamReader(strpath2)
Dim strfilestring2 As String = SR.ReadToEnd
Dim filetext2() As String = Split(strfilestring2, ",")
Dim j As Integer = 1
For i As Integer = 0 To filetext2.GetUpperBound(i) - 1 Step 2
If ListBox1.SelectedItems.Item(i).ToString = filetext2(0) Then
ListBox2.Items.Add(filetext2(j))
j += 2
End If
Next
-
Sep 11th, 2004, 04:40 PM
#9
PowerPoster
HI,
Sorry, In my rush to look at new houses here I missed a couple of other errors. Alter your code to (look very carefully!)
VB Code:
Dim strfilestring2 As String = SR.ReadToEnd
Dim filetext2() As String = Split(strfilestring2, ",")
For i As Integer = 0 To filetext2.GetUpperBound(i) - 1 Step 2
If ListBox1.SelectedItems= filetext2(i) Then
ListBox2.Items.Add(filetext2(i+1))
End If
Next
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Sep 11th, 2004, 05:15 PM
#10
Thread Starter
Junior Member
Taxes,
Thanks again for your reply. I made the changes that you pointed out, and still the code is not working. I don't know what the problem could be. I did change the 'selecteditems' to 'selecteditem'. there was an error with it plural. I don't know what else that could be wrong? Thanks so much again.
-
Sep 12th, 2004, 09:12 AM
#11
PowerPoster
Hi,
Yes, sorry again, it should have been singular.
Put a breakpoint on the line
If ListBox1.SelectedItems= filetext2(i) Then
and then check to see the following:
1. What is contained in ListBox1.Selected Item
2. What is the value of i
3. What is the value of filetext2(i)
4. What is the value of filetext2(i+1)
Post the above values for the first, second and third times through the loop.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Sep 12th, 2004, 03:40 PM
#12
Thread Starter
Junior Member
I finally got it taxes. Thanks again so much for your help. The problem with the code was that with .ReadToEnd, it didn't handle the next line because in a csv file, there is a comma that separates each cell, but not a new line. This is the new code that i have, familiar to the original one that i had.
Dim strpath2 As String
Dim SR2 As StreamReader
strpath2 = "C:\csv1.csv"
SR2 = New StreamReader(strpath2)
ListBox2.Items.Clear()
Do While SR2.Peek() <> -1
Dim strfilestring2 As String = SR2.ReadLine
If strfilestring2 <> "" Then
Dim filetext2() As String = Split(strfilestring2, ",")
If ListBox1.SelectedItem = filetext2(0) Then
ListBox2.Items.Add(filetext2(1))
End If
End If
Loop
I did the .ReadLine again, and just looped through the whole array list. But I appreciate all the help you gave me!!!!
-
Sep 12th, 2004, 06:37 PM
#13
PowerPoster
Hi,
I think you will find that your latest code does NOT work. In:
If ListBox1.SelectedItem = filetext2(0) Then
ListBox2.Items.Add(filetext2(1))
It will work only if the selected Set name is the same as the contents of filetext2(0).
You are comparing the same element, filetext2(0) each time and not running through each of the sets.
Also it is not necessary to read the file every time you select a new set.
I think your entire code would be better as:
With formwide scope
VB Code:
Dim filetext1() As String
Dim Icount As Integer
In the event which fills the first ListBox
VB Code:
Dim strpath2 As String
Dim SR2 As StreamReader
strpath2 = "C:\csv1.csv"
SR2 = New StreamReader(strpath2)
ListBox2.Items.Clear()
Dim strfilestring2 As String = SR2.ReadLine
If strfilestring2 <> "" Then
filetext1() = Split(strfilestring2, ",")
For iCount=0 to filetext1.GetUpperBound-1.Step 2
ListBox1.Items.Add(filetext1(i))
Next
End If
Then
VB Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
For iCount=0 to filetext1.GetUpperBound(0)-1 Step 2
If ListBox1.SelectedItem = filetext1(iCount) Then
ListBox2.Items.Add(filetext1(iCount+1))
End If
Next
End Sub
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|