[RESOLVED] VB 2010: Array displayed in listbox to textboxes for editing then back to array&.txt?
I'm having a little trouble working out how to make array items display in textboxes upon clicking on an item name displayed in a listbox.
I've got my array here.
Code:
Private Sub readFile()
Dim reader As IO.StreamReader = New IO.StreamReader("..\..\Stock.txt")
Dim i = 0
Do While reader.EndOfStream = False
stockArray(i) = (reader.ReadLine).Split(
"|")
i += 1
Loop
End Sub
and with some difficulty a filter running which I've got to display all the items from my file one load and filter through whatever I want.
Code:
Private Sub applySearch(x As String)
ListBox1.Items.Clear()
If x = "" Then
While stockArray(i) IsNot Nothing
ListBox1.Items.Add(stockArray(i)(0).ToString)
txtsearch.AutoCompleteCustomSource.Add(stockArray(i)(0).ToString)
i += 1
End While
Else
While stockArray(i) IsNot Nothing
If stockArray(i)(0).ToString.ToLower.Contains(x.ToLower) Or stockArray(i)(0).ToString.ToLower = x.ToLower Or stockArray(i)(3).ToString.ToLower = x.ToLower Then
ListBox1.Items.Add(stockArray(i)(0).ToString)
txtsearch.AutoCompleteCustomSource.Add(stockArray(i)(0).ToString)
End If
i += 1
End While
End If
End Sub
However due to my novice experience and probably more to the point lack of useful examples that I can find. I am unable to now make it so that once I click on an item in my listbox it will load the remaining 8 pieces of data associated from my array into text boxes so that it may be edited in the array and then saved upon a button click. Could anyone please give me a nudge in the right direction? So far I'm trying to do things under a sub "ListBox1_SelectedIndexChanged" which "Handles ListBox1.SelectedIndexChanged" which I think it what I want. But I'm having trouble with the code there after. Current attempts are letting my textbox equals something to do with the list box but I'm not sure what and not how to have this directly linked and updated in the array once alterations or additions to data is complete.
Thank You a bunch for anyone that can help and I'm sorry if I have been unclear at all.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
so let me get this str8...
a listbox gets clicked.. and a value from an array is going to fill a textbox...
how is the data from the array affiliated with items in the listbox? do you have somekind of connection between them?
if i understand this correctly would it work if you used the text from the listbox to call an associative array in which that array item holds all the related values?
If so, what you probably want to do is first look into inserting a text/value into the listbox. So it will show the description or something in the listbox, but when the user selects it you can use a value associated with that selected item to call a dictionary by key. This entry in the dictionary will be an array with all the values related to the selected item.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Yes I have an array which has multiple lines of data such as:
Code:
Name|Type|Stocklevel|Price|Location|StocklevelNeeded|ReorderNeeded|Amountneeded to Reorder|Tags|Description
There are several lines of data and all of the above on each line it split at "|" when placed into an array.
I have now partially fixed my problem which was getting the first item of each row of my array "Name" into a listbox and now have using the 'Selectedindex' the name of the item and the rest of the row necessary to edit displayed into text boxes.
However with this comes a new problem. I now am having the wrong stock items appearing once I click on items after I have applied a filter using the code in my original post.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Here it is in all it's unfinished glory. I currently have quite a bit to do but such as getting a login with MD5'd passwords going and and addition and removal of items + a few other things.
But you should be able to get the picture of my problem with the indexes not matching up to the array and displaying the wrong information in textboxes once a filter (search) is applied.
Also I would love to use Data bases. But that's not something I can work with and just to let you know this is a bit of a mock up example of a Windows Phone app as network restrictions have hindered our use of using the Windows Phone SDK. So let's just not worry so much about all that.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
honestly I dont see any errors.... I applied a filter of q, with no results.. so i cleared it.. then i applied fluffy as a filter.. everything matched.. then i cleared it.. and it still matching up...
what feild is it that you see as getting mixed up?
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Do you see what I mean?
At least I'm finding an upside to this problem. I'm finally being forced to get some other things sorted. I have almost hit a brick wall though and I'm approaching my deadline too.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Well I think I've more or less fixed the index problem now.
I have the subs
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
index = Nothing
gtfo_Messed_Up_Indexes(ListBox1.SelectedItem.ToString)
End Sub
and
Code:
Private Sub gtfo_Messed_Up_Indexes(x As String)
Dim a = 0
Dim flag = 0
While stockArray(a) IsNot Nothing And flag = 0
If stockArray(a)(0) = x Then
txtName.Text = stockArray(a)(0)
txtprice.Text = stockArray(a)(3)
txttype.Text = stockArray(a)(1)
txtquantity.Text = stockArray(a)(2)
txtneeded.Text = stockArray(a)(5)
txtlocation.Text = stockArray(a)(4)
txttags.Text = stockArray(a)(8)
txtdescription.Text = stockArray(a)(9)
txtreorderamount.Text = IIf(stockArray(a)(5) - stockArray(a)(2) < 0, 0, (stockArray(a)(5) - stockArray(ListBox1.SelectedIndex)(2)))
index = a
flag = 1
End If
a += 1
End While
End Sub
Now everything seems to be working correctly and I just need to work out how to add and remove items from this get a couples of labels working and save all of this back to .txt.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
well the problem was you were using the listbox1 index numbers as a reference to the indices in the array. When you change the items in the listbox1, now the index numbers are all different, and they do not match anymore.. so here's how I went around that:
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim listBox1Item As String = ListBox1.SelectedItem
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
If IsNothing(stockArray(arrayIndex)) Then Exit Sub
If listBox1Item = stockArray(arrayIndex)(0) Then
txtName.Text = stockArray(arrayIndex)(0)
txttype.Text = stockArray(arrayIndex)(1)
txtprice.Text = stockArray(arrayIndex)(3)
txtlocation.Text = stockArray(arrayIndex)(4)
txtquantity.Text = stockArray(arrayIndex)(2)
txtneeded.Text = stockArray(arrayIndex)(5)
txttags.Text = stockArray(arrayIndex)(8)
txtdescription.Text = stockArray(arrayIndex)(9)
Exit Sub
End If
Next
End Sub
Last edited by elielCT; Aug 18th, 2012 at 01:27 PM.
Reason: spell check
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
btw, I changed
Code:
If stockArray(i)(0).ToString.ToLower.Contains(x.ToLower) Or stockArray(i)(0).ToString.ToLower = x.ToLower Or stockArray(i)(8).ToString.ToLower = x.ToLower Then
to
Code:
If stockArray(i)(0).ToString.ToLower.Contains(x.ToLower) Or stockArray(i)(8).ToString.ToLower.Contains(x.ToLower) Then
because when you type "po" or "poison" it wouldn't show items that have these terms in their keywords.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Interesting... So this
Code:
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
If IsNothing(stockArray(arrayIndex)) Then Exit Sub
Eliminates the need for me to use something such as letting a=0 then running through a function to check if the array matches an input and if not a +=1 'ing it to cycle through?
I may be able to use this in a few places rather than copying my friends trick.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Ok, new problem now... While using the using that code to reference an 'index' which I have defined as the 'arrayindex' to delete items using
Code:
Array.Clear(stockArray, index, 1)
I am now getting not only the item I wish to delete removed from the array but also all items after it.
The above code runs on a button click and the index comes from (at the end)
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
If IsNothing(stockArray(arrayIndex)) Then Exit Sub
If ListBox1.SelectedItem = stockArray(arrayIndex)(0) Then
txtName.Text = stockArray(arrayIndex)(0)
txttype.Text = stockArray(arrayIndex)(1)
txtprice.Text = stockArray(arrayIndex)(3)
txtlocation.Text = stockArray(arrayIndex)(4)
txtquantity.Text = stockArray(arrayIndex)(2)
txtneeded.Text = stockArray(arrayIndex)(5)
txttags.Text = stockArray(arrayIndex)(6)
txtdescription.Text = stockArray(arrayIndex)(7)
txtreorderamount.Text = IIf(stockArray(arrayIndex)(5) - stockArray(arrayIndex)(2) < 0, 0, (stockArray(arrayIndex)(5) - stockArray(arrayIndex)(2)))
If stockArray(arrayIndex)(6).ToString.ToLower.Contains("dangerous") Then
panalerts.BackColor = Color.Red
lblAlerts.Text = "Dangerous"
Else
panalerts.BackColor = Color.Green
lblAlerts.Text = "Safe"
End If
index = arrayIndex
Exit Sub
End If
Next
End Sub
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Sorting out this index issue I believe will be the final thing for this program to be completed.
If rather than taking an index from the above function if there is any other way of referencing the current selected items index globally or the index of a new item once it's created that could work too.
That arrayindex thing isn't going so well for me when I try to use it anywhere else.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I rarely deal with arrays.. Especially in vb.net where it's got arraylist.. so unless someone has a better solution heres what i did:
I couldnt find a way to remove an item from an array, unless i missed something.. So instead i split it (omitting the one to delete), then put it back together.
note this was not tested.. It's almost 4AM here & i'm only up because i'm trying to finish a system so I can get a few bucks..
Code:
Private Sub deleteStockItem(ByVal itemIndex As Integer)
Dim frontOfArray(1000) As Array
Dim endOfArray(1000) As Array
ReDim frontOfArray(itemIndex)
ReDim endOfArray(stockArray.Count - (itemIndex + 1))
Array.ConstrainedCopy(stockArray, 0, frontOfArray, 0, itemIndex)
Array.ConstrainedCopy(stockArray, itemIndex + 1, endOfArray, 0, endOfArray.Count)
Array.Clear(stockArray, 0, stockArray.Count)
Array.Copy(frontOfArray, stockArray, frontOfArray.Count)
stockArray.Concat(endOfArray)
End Sub
Here i used the same method as before to get the index number, but now i gave him a permanent accessable home:
Code:
private Function getItemArrayIndex(ByVal itemName As String) As Integer
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
If IsNothing(stockArray(arrayIndex)) Then Return -1
If itemName = stockArray(arrayIndex)(0) Then
Return arrayIndex
End If
Next
Return -1
End Function
So we have a way to get the index number, so lets preserve some code space and put it to use:
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
If listBox1Item = stockArray(itemIndex)(0) Then
txtName.Text = stockArray(itemIndex)(0)
txttype.Text = stockArray(itemIndex)(1)
txtprice.Text = stockArray(itemIndex)(3)
txtlocation.Text = stockArray(itemIndex)(4)
txtquantity.Text = stockArray(itemIndex)(2)
txtneeded.Text = stockArray(itemIndex)(5)
txttags.Text = stockArray(itemIndex)(8)
txtdescription.Text = stockArray(itemIndex)(9)
Exit Sub
End If
End Sub
If any issues i'll look at it tomorrow.. i'm done! lmao
Last edited by elielCT; Aug 19th, 2012 at 03:03 AM.
Reason: Misspealled function
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Originally Posted by Valhalla's Wrath
Ok, new problem now... While using the using that code to reference an 'index' which I have defined as the 'arrayindex' to delete items using
Code:
Array.Clear(stockArray, index, 1)
I am now getting not only the item I wish to delete removed from the array but also all items after it.
The above code runs on a button click and the index comes from (at the end)
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
If IsNothing(stockArray(arrayIndex)) Then Exit Sub
If ListBox1.SelectedItem = stockArray(arrayIndex)(0) Then
txtName.Text = stockArray(arrayIndex)(0)
txttype.Text = stockArray(arrayIndex)(1)
txtprice.Text = stockArray(arrayIndex)(3)
txtlocation.Text = stockArray(arrayIndex)(4)
txtquantity.Text = stockArray(arrayIndex)(2)
txtneeded.Text = stockArray(arrayIndex)(5)
txttags.Text = stockArray(arrayIndex)(6)
txtdescription.Text = stockArray(arrayIndex)(7)
txtreorderamount.Text = IIf(stockArray(arrayIndex)(5) - stockArray(arrayIndex)(2) < 0, 0, (stockArray(arrayIndex)(5) - stockArray(arrayIndex)(2)))
If stockArray(arrayIndex)(6).ToString.ToLower.Contains("dangerous") Then
panalerts.BackColor = Color.Red
lblAlerts.Text = "Dangerous"
Else
panalerts.BackColor = Color.Green
lblAlerts.Text = "Safe"
End If
index = arrayIndex
Exit Sub
End If
Next
End Sub
Why are you looping to find the SelectedItem though? There's a SelectedIndex property for the ListBox unless i'm misunderstanding something? I'm still confused on what you're trying to do though here... I'll pretend like all the color changing you're doing in there doesn't exist for the time being because your code is convoluted as it stands quite frankly.
What you're doing is creating a jagged array, although in that if statement, it looks like you want to compare the first element of the string() element in the String()() jagged array. My idea, why not use a Dictionary(Of String, String()) then?
Where the Key is the value of the first element of each line split by "|", and the Value is the actual String.Split("|") array object?
Querying/finding data would be much simpler... Otherwise I would propose some LINQ, but before that, I still need to more clearly identify what it is you're trying to do.
I can get you set with what you want to do, I know this is a more than simple task for me, but understanding what members want to do when they post a thread on any forum I try to help on is half the battle still.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Originally Posted by elielCT
he doesnt want to use a db.. Or a dictionary, or arraylist...
That code is not looping to find a selected item..
its lookin for the selected item in a array...
Selectedindex property is being used..
Do u know how to remove an item from an array?
I honestly dont because I use arraylist's in vb.net.. I dont really see the benefit of an array...
I have not suggested an ArrayList, or a Database, I only suggested a Dictionary(Of Key, T) because it has some purpose based on my current understanding of OP's situation. I wouldn't ever advise ArrayLists though regardless, because they are slow and obsolete, List(Of T) SHOULD be used instead of an ArrayList. ArrayLists are carried over from older VB.
Originally Posted by MSDN ArrayList
ArrayList may not always offer the best performance for a given task. See the "Performance Considerations" section in the List<T> reference topic for a discussion of the relative performance of these classes.
The ArrayList is not guaranteed to be sorted. You must sort the ArrayList prior to performing operations (such as BinarySearch) that require the ArrayList to be sorted.
The capacity of a ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.
For very large ArrayList objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
The ArrayList collection accepts null as a valid value, allows duplicate elements.
Using multidimensional arrays as elements in an ArrayList collection is not supported.
That code is not looping to find a selected item..
I think you should re-look that.
Code:
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1 'THIS LINE
If IsNothing(stockArray(arrayIndex)) Then Exit Sub
If ListBox1.SelectedItem = stockArray(arrayIndex)(0) Then 'THIS LINE
And the SelectedItem property is being used there...
Code:
Do u know how to remove an item from an array?
This is the purpose for using a List(Of T) as it's much more versatile.
Code:
I dont really see the benefit of an array...
If utilized correctly they are good performance wise.
---------
EDIT: Okay i'll attempt to explain my understanding so far. Say you have a file:
Code:
Item1|Item2|Item3
Product1|Product2|Product3
Now in your ListBox you have:
Code:
Item1
Product1
And based on the SelectedItem, you want to display the line which pertains to that SelectedItem, in various TextBoxes?
So if Product1 was the SelectedItem, you'd want to display these strings in various TextBoxes?
Code:
Product1
Product2
Product3
Correct?
If so, there's one thing I should ask. Do the ListBox Item's and the File lines, correspond to each other? Are they in the same order?
So this line:
Code:
Product1|Product2|Product3
Would be in the same position index-wise as the ListBox item:
Code:
Product1
????
Last edited by AceInfinity; Aug 19th, 2012 at 03:25 AM.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
i dont know how to quote here yet..
but they only correspond when initially set.. the list box gets updated and no longer corresponds.. which is why i made a function to find the corresponding value to the selected item in the array..
with todays technology, im sure a fairly sized arraylist wont make much of a difference. Unless you need to specify a good chunk of memory for storage, an array is rather cumbersome..
and collectionlists are all slower than arrays, whether its a dictionary or otherwise..
I do prefer a dictionary, especially in this scenario.. Matter of fact in this case, dealing with inventory, that should belong in a db.. sqlce, sqlite....
start with index 0 in array and proceed through array:
Code:
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
if this address is empty stop execution and leave procedure:
Code:
If IsNothing(stockArray(arrayIndex)) Then Exit Sub
compare the selecteditem to the value in array:
Code:
If ListBox1.SelectedItem = stockArray(arrayIndex)(0) Then
....do something if they match
where oh where do you see a loop through listitems to get the selected value??
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Originally Posted by elielCT
its not like i never been helped...
Which is exactly what I agree with. Now whenever I am able to I will pass on whatever coding knowledge I have learnt and if I eventually get to software development at uni I will hopefully have a lot to share.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Well I think I have manged to completely mess up this code with the indexes. I've been getting all sorts of errors when I try to do things now.
In the attached file here you can see what I mean.
I've done a few things, probably stupid but it just isn't working. I've managed to get overflow errors and what VB suggested to be a divide by zero error without even dividing anything. I also need to get all my need to get all of my text boxes synced up to my array so that when I change what is with in them it changes in the array automatically. As in my txttags_TextChanged sub.
If these indexes would just work for me I think it would all be alright.
Everything's working except for these textboxes syncing with the array so changes can be saved and this index with the delete function.
I know you're asleep now elielCT.
But if anyone else can help my at all or you can take a quick look tomorrow it would be greatly appreciated. Tomorrow is my deadline to give this finished and I still have some writing to do about this program.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Ok well it looks like the remove item code did work after all. It's just my code trying to get my text boxes update the array once text is changes not working.
Did I mention that you are Boss elielCT?
Last edited by Valhalla's Wrath; Aug 19th, 2012 at 09:07 AM.
Reason: Forgot to mention something.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I haven't gotten it going for all text field yet. But when I change text in the tags text box now. It appears to stay changed once swap between items.
Only problem is though. Once I use my clear filter button now it highlights the last line of the below code and gives me the message "Index was outside the bounds of the array."
Hopefully I'm just over looking something small as it's getting late and I'm about to head to bed. But otherwise and suggestions on this are appreciated.
Code:
Private Sub txttags_TextChanged(sender As System.Object, e As System.EventArgs) Handles txttags.TextChanged
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
stockArray(itemIndex)(6) = txttags.Text
Here is my most recent version you now need to type in the case sensitive username "George" and password "Boss" to enter(or you can disable it). Don't even question the security please login is just for looks at the moment as theoretically the final build of this will be sending encrypted data to a server. As for the big red "Spy Cam" button, that's another story (involving several privacy laws).
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Originally Posted by elielCT
i dont know how to quote here yet..
but they only correspond when initially set.. the list box gets updated and no longer corresponds.. which is why i made a function to find the corresponding value to the selected item in the array..
with todays technology, im sure a fairly sized arraylist wont make much of a difference. Unless you need to specify a good chunk of memory for storage, an array is rather cumbersome..
and collectionlists are all slower than arrays, whether its a dictionary or otherwise..
I do prefer a dictionary, especially in this scenario.. Matter of fact in this case, dealing with inventory, that should belong in a db.. sqlce, sqlite....
start with index 0 in array and proceed through array:
Code:
For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
if this address is empty stop execution and leave procedure:
Code:
If IsNothing(stockArray(arrayIndex)) Then Exit Sub
compare the selecteditem to the value in array:
Code:
If ListBox1.SelectedItem = stockArray(arrayIndex)(0) Then
....do something if they match
where oh where do you see a loop through listitems to get the selected value??
with todays technology, im sure a fairly sized arraylist wont make much of a difference. Unless you need to specify a good chunk of memory for storage, an array is rather cumbersome..
and collectionlists are all slower than arrays, whether its a dictionary or otherwise..
It's not about todays technology, ArrayList's are slow. I admire your faith in them at today's day and age, but they are depreciated. If you're hardware stays the same and you compare dealing with data between a List(of T) and an ArrayList you'll notice a great deal in speed for some tasks. It's about optimization. ArrayList's aren't used anymore really.
where oh where do you see a loop through listitems to get the selected value??[/
You're looping to FIND a selected value in the array. If you find the value then something happens. That's what i'm trying to say... If there's any chance of avoiding a loop to find a particular value, then that should be your first choice over the loop, because usually looping is slower. (If you choose to disbelieve me I can give out an example to demonstrate this for the benefit of learning. Over time you learn things like this in programming, so I don't blame you, I was once like that.)
1) Loop through the array: For arrayIndex As Integer = 0 To (stockArray.Count - 1) Step 1
Note: Step 1 isn't needed because that
2) If value is Null then Exit Sub: If IsNothing(stockArray(arrayIndex)) Then Exit Sub
3) If value of the array is equal to the selected item, then we've found the selected value in the array: If ListBox1.SelectedItem = stockArray(arrayIndex)(0)
That's what I mean; "get" the selected value based on the current methods you're using.
We could retrieve it much faster with LINQ though too even:
vbnet Code:
Dim stock As String() = stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
'Now assign elements of stock to the textboxes
This is assumptuous that the selected item will be the first element of a string() in the jagged string()(), but if that's not always the case, we can work around that too.
Last edited by AceInfinity; Aug 19th, 2012 at 03:46 PM.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I will agree with you Ace that there are better ways of doing this using databases possibly or LINQ and I will in future. But just this time I have some so close already and don't have time to change. There is not point arguing the suns rise once it is already on the horizon.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Originally Posted by Valhalla's Wrath
I will agree with you Ace that there are better ways of doing this using databases possibly or LINQ and I will in future. But just this time I have some so close already and don't have time to change. There is not point arguing the suns rise once it is already on the horizon.
Try that LINQ I posted above, I can explain what it does if you need clarification as well.
vbnet Code:
Dim stock As String() = stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
'Now assign elements of stock to the textboxes
This basically replaces that entire loop you've got going on there.
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
You may not even have to assign them manually, if all your TextBox design names start with "TextBox" and then followed by a number, then you could just do this:
*This assumes all your Textboxes are on the main form. And the ones you want to add the text to, are in order based on their design number in "TextBox#" and that each of these Textboxes are named similarly by "TextBox#" in your designer.*
vbnet Code:
Dim stock As String() = stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
For i As Integer = 0 To stock.Length - 1
Me.Controls("TextBox" & (i + 1).ToString).Text = stock(i)
Next
I'm not arguing, i'm just trying to help out in this thread. It's good for people to try to understand good habits, and that's what i'm trying to point out here. Usually I see people making things harder than they need to be when they ask for help, but that's part of the reason why they request assistance.
If you want a way to loop through the stockArray and add items to text i'll switch to a solution using that method, but I'm trying to make things easier for you, that's all.
~Ace
Last edited by AceInfinity; Aug 19th, 2012 at 04:06 PM.