Page 3 of 3 FirstFirst 123
Results 81 to 84 of 84

Thread: [RESOLVED] VB 2010: Array displayed in listbox to textboxes for editing then back to array&.txt?

  1. #81
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.

    Quote Originally Posted by elielCT View Post
    I tried to explain this before...

    Change the event handler for your textboxes
    Code:
    .TextChanged
    to
    Code:
    .LostFocus

    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:
    1. Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.     If ListBox1.SelectedIndex > -1 Then
    3.         Dim stock As String() = stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
    4.         For i As Integer = 0 To stock.Length - 1
    5.             Me.Controls("Txt" & (i + 1).ToString).Text = stock(i)
    6.         Next
    7.  
    8.         Dim listBox1Item As String = ListBox1.SelectedItem
    9.         Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
    10.    
    11.         txtreorderamount.Text = IIf(stockArray(itemIndex)(5) - stockArray(itemIndex)(2) < 0, 0, (stockArray(itemIndex)(5) - stockArray(itemIndex)(2)))
    12.         If stockArray(itemIndex)(6).ToString.ToLower.Contains("dangerous") Then
    13.             panalerts.BackColor = Color.Red
    14.             lblAlerts.Text = "Dangerous"
    15.         Else
    16.             panalerts.BackColor = Color.Green
    17.             lblAlerts.Text = "Safe"
    18.         End If
    19.  
    20.     End If
    21.  
    22. 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:
    1. Private Sub txt2_LostFocus(sender As System.Object, e As System.EventArgs) Handles txt2.LostFocus
    2.     Dim listBox1Item As String = ListBox1.SelectedItem
    3.     Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
    4.     stockArray(itemIndex)(1) = txt2.Text
    5. 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:
    1. Private Sub txt1_LostFocus(sender As System.Object, e As System.EventArgs) Handles txt1.LostFocus
    2.     Dim listBox1Item As String = ListBox1.SelectedItem
    3.     Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
    4.     stockArray(itemIndex)(0) = txt1.Text
    5.  
    6.     ListBox1.Items(ListBox1.SelectedIndex) = txt1.Text
    7.  
    8. 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.

  2. #82
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.

    I don't feel like cluttering up this post with quotting what Inferrd mentioned, but as for the StreamReader and even inclusive of StreamWriter, If you don't want to manually deal with Close/Dispose, then that's a good time to be using the Using statement

    I find it makes things a lot cleaner in most cases.

    ~Ace
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #83

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

    Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.

    Everything works like a charm.

    I didn't have time to clean up some things such as the hiding of the text boxes. But that doesn't cause issues.

    I do have to say a massive thank you to everyone that has helped. You have all been so patient and have taken the time to explain so many things to me. In fact I've already started to share some of that information around such as the difference between jagged arrays and multidimensional ones. I learnt off of them that what I was using was a multidimensional array and learnt how to use one and they were quite surprised to learn that they have been mistaken for so long also.

    Thank You!!!

  4. #84
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.

    Quote Originally Posted by Valhalla's Wrath View Post
    Everything works like a charm.

    I didn't have time to clean up some things such as the hiding of the text boxes. But that doesn't cause issues.

    I do have to say a massive thank you to everyone that has helped. You have all been so patient and have taken the time to explain so many things to me. In fact I've already started to share some of that information around such as the difference between jagged arrays and multidimensional ones. I learnt off of them that what I was using was a multidimensional array and learnt how to use one and they were quite surprised to learn that they have been mistaken for so long also.

    Thank You!!!
    Well it's good that the explanation was given! No more misunderstandings

    This is what I like to hear from people that I try to help out. It seems you're progressing, and if the interest is there, you can continue to progress from the things you've learned just from this one project.

    Cheers
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Page 3 of 3 FirstFirst 123

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
  •  



Click Here to Expand Forum to Full Width