-
[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. :(
Any ideas on how to work around this?
My full code as of current can be found here: http://pastebin.com/6fJ7utry
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
can you provide the solution?
I pasted the code, and couldnt get any reaction...
Now is this holding inventory? Because if it is, u may want to use a database instead..
And honestly it would be alot easier..
-
1 Attachment(s)
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.
Thank You.
Attachment 90607
-
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&.
Try typing in "Po" then click on either of the results. What is displayed in the textboxes will not be the name and data of the items clicked.
-
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.
Hopefully shouldn't be to hard.
-
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
-
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. :o
-
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
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
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&.
You are a legend. I'll try this out right away and hopefully it all works fine.
Thank You!!
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
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...
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
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.
Quote:
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.
Read: http://msdn.microsoft.com/en-us/libr...arraylist.aspx
Quote:
That code is not looping to find a selected item..
:confused: 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:
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:
????
-
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&.
the non-corresponding index value was where op was stuck...
hopefully this clears up the issue's a little.. next op wanted a way to reference the corresponding index again to delete item from array...
this was what i could think of given the restraints...
If u have a better solution I'm sure op would appreciate it..
NOTE: If im misspelling every other word.. sorry ium sleepy.. lol..
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Code:
Array.ConstrainedCopy(stockArray, itemIndex + 1, endOfArray, 0, endOfArray.Count)
Now pops up highlighted in yellow with this message.
Quote:
Source array was not long enough. Check srcIndex and length, and the array's lower bounds.
elieICT understands.
My data for reference is in a text file in the format.
Code:
name|category|stock|price|location|stocklevel required|tags|description|
name|category|stock|price|location|stocklevel required|tags|description|
name|category|stock|price|location|stocklevel required|tags|description|
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
concat wasnt working...
This should work:
Code:
Private Sub deleteStockItem(ByVal itemIndex As Integer)
Dim frontOfArray(1000) As Array
Dim endOfArray(1000) As Array
ReDim frontOfArray(itemIndex - 1)
ReDim endOfArray(stockArray.Count - itemIndex - 2) ' (+ 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)
Array.ConstrainedCopy(endOfArray, 0, stockArray, frontOfArray.Count, endOfArray.Count)
End Sub
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Now it deletes the first item from my list. I do hope I'm not keeping you wake..
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
to think i gotta be awake in 4 hours.. lol
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
elielCT
to think i gotta be awake in 4 hours.. lol
Yes. I really do owe you for doing this.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
-
1 Attachment(s)
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
no prob i enjoy... its not like i never been helped...
its working on my end...
im attaching the solution
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
That would be the file I sent you.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
yes, but with the modifications...
i select a product, click delete, then remove filter to refresh, and only the one that was selected is missing
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
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&.
Yeah I'm sure that isn't the right file. It's not modified.
-
1 Attachment(s)
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. :cry: 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.
Attachment 90657
-
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?
-
1 Attachment(s)
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).
Attachment 90661
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
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??
Quote:
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.
Quote:
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.
-
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&.
Quote:
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
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Could you please. The ".fists(function(objarr) objarr(0)."... Part in particular.
How could I use this to say for instance an item in an array is now equal to one from a textbox?
Edit: Thanks!
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
AceInfinity
Me.Controls("TextBox" & (i + 1).ToString).Text = stock(i)
I was going to suggest something like that.. but using a tag for the textbox corresponding to its place in the array....
I usually try to name my controls something that describes its purpose...
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
Valhalla's Wrath
Could you please. The ".fists(function(objarr) objarr(0)."... Part in particular.
How could I use this to say for instance an item in an array is now equal to one from a textbox?
Edit: Thanks!
Okay... Here we go:
vbnet Code:
stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
First you need to understand that stockArray is what we call a "jagged array". This means it is an array of an arrays.
So each element in stockArray is an array: The array returned by the Split() function you used on each line, which returns an array of strings.
Example: Inside stockArray(0) which is the very first element of stockArray, we have a string array. This string array is the first line split by "|" which is an array that the Split function returns. Hope this is clear.
Now... When we query data from stockArray, we're essentially looking at each string array value (the strings split by "|" for each line as a string array element) inside stockArray at first.
The First keyword I have there, means we're trying to find the First something based on a condition we're using.
So example: Say we have a class full of students. There is only 1 girl. If our condition is to find the First girl, then that's what we're doing here essentially, only comparing something with the ListBox.SelectedItem as a string value.
Function(objArr) - Where objArr is basically like our temporary variable name (it can be any name you want). Holds THE value of the String array in question; THE string returned from the Split() function for a particular line that you split by ("|"). Right now it doesn't matter which line, just know that we're evaluating SOME line from stockArray at this point in time.
Now from that String array in question (represented by objArr), which holds a collection of strings separated by "|", we're referencing the first element of that string array when we use objArr(0), and if it EQUALS ListBox1.SelectedItem.ToString. Then we know we found the particular split string array, which holds the strings we want to use in the TextBoxes.
(Tried my best to explain it.)
Quote:
How could I use this to say for instance an item in an array is now equal to one from a textbox?
Easy, but which array do you want to use here? Because remember stockArray is an array of an array, we need to reference at least one string array from INSIDE stockArray to compare with the values from the TextBox.
Quote:
Originally Posted by
elielCT
I was going to suggest something like that.. but using a tag for the textbox corresponding to its place in the array....
I usually try to name my controls something that describes its purpose...
That's VERY good :) It's a nice habit in my opinion. Because if you have lots of controls, things get convoluted fast.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
AceInfinity
your way of finding the selected item does work pretty good... alot less code and no loop...
:Thumbs up:
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
AceInfinity
Okay... Here we go:
vbnet Code:
stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
Easy, but which array do you want to use here? Because remember stockArray is an array of an array, we need to reference at least one string array from INSIDE stockArray to compare with the values from the textbox
Is a 'jagged array' what you would call a multidimensional array. In this case two dimensonal?
For to referance somehing such as a name in a textbox I've been using Stockarray('meant to be increasing index')(0) to referance the first item from the split item on each lin:thumb:e that corresponds to the index. So in all cases pretty much I reer to the first array as a varying integer and the second as the position in the second array of what I wish to change. I hope that's what you wanted to know.
I will also try out this code as soon as I can and post back with results. You explanation was very easy to understand. Thank You!
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
elielCT
AceInfinity
your way of finding the selected item does work pretty good... alot less code and no loop...
:Thumbs up:
Thankyou elielCT! :)
Quote:
Originally Posted by
Valhalla's Wrath
Is a 'jagged array' what you would call a multidimensional array. In this case two dimensonal?
For to referance somehing such as a name in a textbox I've been using Stockarray('meant to be increasing index')(0) to referance the first item from the split item on each lin:thumb:e that corresponds to the index. So in all cases pretty much I reer to the first array as a varying integer and the second as the position in the second array of what I wish to change. I hope that's what you wanted to know.
I will also try out this code as soon as I can and post back with results. You explanation was very easy to understand. Thank You!
No, a jagged array is NOT a multi-dimentional array. A jagged array is an array of an array. A Multidimentional array is not that.
For the demonstrations below, each pair of "{" "}" represent an array. When I put "ARRAY" I mean the element is an actual Array of elements.
Jagged Array Structure:
Code:
{{ARRAY}, {ARRAY}, {ARRAY}}
Multi-Dimentional Array Example:
Code:
{{{Object, Object}, {Object, Object}}, {{Object, Object}, {Object, Object}}
Multi-Dimentional array's are declared like so, for instance:
Code:
Dim arr As String(,,)
Jagged arrays are declared differently:
Code:
Dim arr As String()()
Quote:
For to referance somehing such as a name in a textbox I've been using Stockarray('meant to be increasing index')(0) to referance the first item from the split item on each line that corresponds to the index. So in all cases pretty much I reer to the first array as a varying integer and the second as the position in the second array of what I wish to change. I hope that's what you wanted to know.
This is what my LINQ does with no loop. :) I knew this is what you wanted to do because this is what that LINQ does.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Can't seem to implement the code properly. Any I think I've just forgotten everything. Can someone tell me again where I should put this code and what it's replacing in my existing code so that everything works.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
vbnet Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex > -1 Then
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
End If
End Sub
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
interesting.. when i first heard of LINQ i had assumed it was solely as a new way to query a db... So i wasnt interested at the moment & figured i'd get to it at some point..
So i made it a point it was definitely time...
Very interesting... A query all language......
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Ok but once I change an item after editing something my edit will not stay there. I change text and it doesn't stay changed.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
oh i meant to tell u.. uR using the ontextchanged method..
that is being executed whenever the form is loaded, and you type a character into the textbox..
try the lostfocus method..
it seemed to be working fine on my end...
in the original file you sent me tags was index number 8.. in ur new code it's 6...
did you change it?
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
elielCT
interesting.. when i first heard of LINQ i had assumed it was solely as a new way to query a db... So i wasnt interested at the moment & figured i'd get to it at some point..
So i made it a point it was definitely time...
Very interesting... A query all language......
I enjoy it, and it is very useful and fast for performance as well. I've come up with some pretty monsterous LINQueries from time to time.
Take a look at this example: http://www.vbforums.com/showthread.p...=1#post4199510
If you follow along with the thread, you'll see what that LINQ does, and I think it's pretty impressive the kind of things you can do with LINQ with a bit of creativity.
Quote:
Originally Posted by
Valhalla's Wrath
Ok but once I change an item after editing something my edit will not stay there. I change text and it doesn't stay changed.
You're editing the TextBox Text values after the data is parsed into them?
It won't stay there because you haven't saved that new data to the file. That's something different though. I can help you with saving the data to the file too.
I can come up with more LINQ to do that as well unless you're tired of it already? lol
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
EDIT: No idea why my post went through twice, that's happened 2 times now just this weekend.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
i thought he meant it wasnt staying in the array... i didnt see a mention of closing the form & having the issue...
once the form is loaded it doesnt reference the actual file anymore.. just the array...
at least from what i saw.. unless i missed something...
i thought he mightve been calling the wrong index num...
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
If that is possible that would be excellent. To be honest I don't really understand the LINQ so well.
But I only have 3 hours to get this working so any help with your more efficient method is greatly appreciated.
The only thing that it really need to so is just updates the vales within the array(file?) so that they stay there stay there after they are changed and only are saved to the stock.tct file once I press save which it currently already saves. But without being able to make alterations that's kind of pointless.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
vbnet Code:
Dim pathToFile As String = "D:\Path\input.txt"
'??? Whatever your last textbox value is for the last string to be put in, put the number below
Dim lastTB As Integer = 7 'Example: TextBox7 = 7
'I don't know how many textboxes you have...
Dim NewLineValue = Enumerable.Range(1, lastTB).Select(Function(i) Me.Controls("TextBox" & i.ToString).Text).Aggregate(Function(t1, t2) String.Format("{0}|{1}", t1, t2))
Dim FullText As String = String.Join(Environment.NewLine, File.ReadAllLines(pathToFile).Select(Function(s) If(s.Split("|"c)(0) = ListBox1.SelectedItem.ToString, NewLineValue, s)))
File.WriteAllText(pathToFile, FullText)
Here's to overwriting the file with the data from the TextBoxes on the specific line indicated by the selected item in the ListBox.
pathToFile - The path to the file you're using for the input data
LastTB - I don't know how many textboxes you have, but the number at the end of the name of the LAST textbox being used for showing the value from the line you would put that number in there for that variable.
So if I have TextBox1, TextBox2, TextBox3, TextBox4 for example, which displays the data from my original LINQ I gave you above, that value should be 4.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Essentially what we're doing above is this:
1) For the NewLineValue variable, we're 'selecting' the value of each TextBox's .Text value. Once that is done we have an IEnumerable(of String) object. From there, the Aggregate function combines each string in the IEnumerable(of String) collection adding the "|"s where necessary to have it in the format that the rest of your lines in that file are in.
So basically this could have also been done by doing something like:
Code:
TextBox1.Text & "|" & TextBox2.Text & "|" & TextBox3.Text & "|" & .... Ect...
So now we have the string value that we want to go into the file to replace the old one that we originally got the values from.
2) For the FullText variable, we're reading through each line in the file, and if the value of the first string element for that line being split by the "|" char is equal to the value of the text in the SelectedItem for the ListBox, instead of retrieving the line from that file, we select the NewLineValue instead, and joining this IEnumerable(of String) collection by Environment.Newline.
3) Then we write that FullText to the file, overwriting the pre-existing data, but really the only modified data was that one line.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I already have
Code:
Private Sub btnupdate_Click(sender As System.Object, e As System.EventArgs) Handles btnsave.Click
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("..\..\Stock.txt", False)
Dim i = 0
Dim text As String
While stockArray(i) IsNot Nothing
text = stockArray(i)(0) + "|" + stockArray(i)(1) + "|" + stockArray(i)(2) + "|" + stockArray(i)(3) + "|" + stockArray(i)(4) + "|" + stockArray(i)(5) + "|" + stockArray(i)(6) + "|" + stockArray(i)(7)
file.WriteLine(text)
i += 1
End While
file.Close()
End Sub
This to write the file. But before the file it written how can I make the data in the program stay there when I change between selected items? Unless your code actually does that as well? I will take a good look at all of this tonight, I promise. But I just need to get it working right now. :/
Also where was that code supposed to go?:ehh:
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
how can I make the data in the program stay there when I change between selected items
RemoveHandler for the ListBox1.SelectedIndex changed event.
Quote:
Also where was that code supposed to go?
Basically where you've just posted that code above. In the btnupdate_Click event.
If you want to not have the textbox text value's change after you save to the file, then you'll have to call to remove the handle for the SelectedIndex_Changed event after you're done writing to the file. So it would be the very last task of btnupdate_Click essentially.
vbnet Code:
RemoveHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
If you don't understand anything please ask.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Also that btnupdate sub should be called btnsave or something. I want changes to stay without pressing a button if possible?
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Remove the handles from this?