|
-
Aug 20th, 2012, 10:44 PM
#36
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 Originally Posted by elielCT
I tried to explain this before...
Change the event handler for your textboxes to
LostFocus is a far better idea. Requires less hacks and is very easy to implement given you don't have time for a rewrite.
So, with your code from post#67:
First, remove TextBox2 through TextBox10 from the designer and remove all references to them from the User form code. Also remove all the .Hide and .Show code.
Next, you have an error in your ListBox1_SelectedIndexChanged sub. You have the End If in the wrong place. It should be where you have the commented out End If. Also, get rid of the Exit Sub.
vb.net 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("Txt" & (i + 1).ToString).Text = stock(i)
Next
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
txtreorderamount.Text = IIf(stockArray(itemIndex)(5) - stockArray(itemIndex)(2) < 0, 0, (stockArray(itemIndex)(5) - stockArray(itemIndex)(2)))
If stockArray(itemIndex)(6).ToString.ToLower.Contains("dangerous") Then
panalerts.BackColor = Color.Red
lblAlerts.Text = "Dangerous"
Else
panalerts.BackColor = Color.Green
lblAlerts.Text = "Safe"
End If
End If
End Sub
For textboxes txt2 through txt8 and txtreorderamount, switch from handling the TextChanged event to handling the LostFocus event as suggested by elielCT, e.g.
vb.net Code:
Private Sub txt2_LostFocus(sender As System.Object, e As System.EventArgs) Handles txt2.LostFocus
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
stockArray(itemIndex)(1) = txt2.Text
End Sub
Similarly for txt1 textbox (the Name textbox), but also rewrite the value to the ListBox, otherwise the stock array and the listbox will be out of synch, your code will no longer find the listbox item in the (updated) stock array, and you'll get the errors you have been seeing:
vb.net Code:
Private Sub txt1_LostFocus(sender As System.Object, e As System.EventArgs) Handles txt1.LostFocus
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
stockArray(itemIndex)(0) = txt1.Text
ListBox1.Items(ListBox1.SelectedIndex) = txt1.Text
End Sub
There is lots of room for improvement (depending on how much time you have left) such as the use of buttons to update the array from the values in the textboxes as mentioned by both elielCT and AceInfinity.
You could reduce the amount of repeated code if you so wished, and also give your subs decent names (e.g. NameText_LostFocus instead of txt1_LostFocus and btnAppFilter_Click instead of Button1_Click). Start all your sub and function names with a capital letter (Pascal casing) or at least be consistent.
A few minor observations:
You don't close and dispose the StreamReader object in sub readFile (you could use a Using reader As... End Using block instead of the Dim, and then you don't need to worry about the closing/disposing of the StreamReader).
You should clear the textboxes, the restock label and the alerts label just after the call to ListBox1.Items.Clear() in your Search sub . At the moment, filtering repopulates the listbox, doesn't select an item in it, but leaves the textboxes populated from any previously selected listbox item.
Two of your labels display "Null" when the app is first started.
Clicking the "Apply" button while the search textbox still contains "Touch Here To Search" gave an unexpected result. You could disable the button until that message has been removed.
Tags for this Thread
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
|