Page 2 of 3 FirstFirst 123 LastLast
Results 41 to 80 of 84

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

  1. #41
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

    Quote Originally Posted by AceInfinity View Post
    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...

  2. #42
    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
    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:
    1. 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.)

    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 View Post
    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.
    <<<------------
    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. #43
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    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:

  4. #44

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    Quote Originally Posted by AceInfinity View Post
    Okay... Here we go:
    vbnet Code:
    1. 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 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.

    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!

  5. #45
    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 elielCT View Post
    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 View Post
    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 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.

    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()()
    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.
    Last edited by AceInfinity; Aug 19th, 2012 at 05:54 PM.
    <<<------------
    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
    <<<------------

  6. #46

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

    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.

  7. #47
    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&.

    vbnet 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("TextBox" & (i + 1).ToString).Text = stock(i)
    6.         Next
    7.     End If
    8. End Sub
    <<<------------
    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
    <<<------------

  8. #48
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    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......

  9. #49

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

    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.

  10. #50
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    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?

  11. #51
    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 elielCT View Post
    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 View Post
    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
    <<<------------
    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
    <<<------------

  12. #52
    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&.

    EDIT: No idea why my post went through twice, that's happened 2 times now just this weekend.
    <<<------------
    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
    <<<------------

  13. #53
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    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...

  14. #54

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

    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.

  15. #55
    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&.

    vbnet Code:
    1. Dim pathToFile As String = "D:\Path\input.txt"
    2. '??? Whatever your last textbox value is for the last string to be put in, put the number below
    3. Dim lastTB As Integer = 7 'Example: TextBox7 = 7
    4.  
    5. 'I don't know how many textboxes you have...
    6.  
    7. 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))
    8. Dim FullText As String = String.Join(Environment.NewLine, File.ReadAllLines(pathToFile).Select(Function(s) If(s.Split("|"c)(0) = ListBox1.SelectedItem.ToString, NewLineValue, s)))
    9. 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.
    Last edited by AceInfinity; Aug 19th, 2012 at 10:54 PM.
    <<<------------
    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
    <<<------------

  16. #56
    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&.

    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.
    <<<------------
    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
    <<<------------

  17. #57

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

    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?

  18. #58
    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&.

    how can I make the data in the program stay there when I change between selected items
    RemoveHandler for the ListBox1.SelectedIndex changed event.

    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:
    1. RemoveHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
    <<<------------
    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
    <<<------------

  19. #59
    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&.

    If you don't understand anything please ask.
    <<<------------
    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
    <<<------------

  20. #60

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

    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?

  21. #61
    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
    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?
    When do you want the changes to stay, because after you remove the handler you'll have to use AddHandler to make it change the textbox values when you select items in the ListBox again.

    I showed you how here: http://www.vbforums.com/showthread.p...=1#post4219861
    <<<------------
    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
    <<<------------

  22. #62

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    Well I really just want the changes in the text boxes to update in the array and stay there once text is changed. could that be done under a sub with a text changed handle? How?

  23. #63

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    (Double post)

  24. #64
    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&.

    You want to update them in the array? What array? We're reading from a file here, and parsing our own Array object from the values in the file. I think that's the misunderstanding here.

    If you want to keep and use an array with the elements, you'll have to create a member variable, as all these local variables which are being used to store the split() string values and all that within Subs aren't going to be 'remembered' once that Sub's done doing what it needs to do.
    <<<------------
    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
    <<<------------

  25. #65

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    Can't get the code working right now. It's not liking file.writealltext or file.readalllines for some reason.

  26. #66
    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
    Can't get the code working right now. It's not liking file.writealltext or file.readalllines for some reason.
    You have to be more specific so I can help. Why isn't it working?

    What error does it give you? And depending on the error try using breakpoints to see what's going on..
    <<<------------
    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
    <<<------------

  27. #67

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    I have to say a big thank you to every ones help. You have both helped me so much elieICT and Ace that it is amazing. You have also been so patient with me for which I am very appreciative of. Also I have to say a thank you to everyone who has even been bothered to read everything I have to say even if they couldn't help.

    However I just have one very, very, very last problem. (I got a 1 day extension to fix up everything in this)

    I couldn't get that last bit of code you pasted working. I can't remember the exact errors now but I didn't really understand everything it was doing. However I managed to make my own sort of work around which will allow me to edit item details in the text boxes and keeps them changed after the item has been deselected but doesn't add the item to a text file until I click save. Which is what I wanted it to do. The only problem is, while this works for every other text box. It does not work for my text box holding item names.

    The code for which I am using is

    Code:
    Private Sub txt1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txt1.TextChanged
            Dim listBox1Item As String = ListBox1.SelectedItem
            Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
            stockArray(itemIndex)(0) = txt1.Text
            Search("")
    
        End Sub
    The code for all my other textchanged events on other text boxes is the same but for some reason or another it just doesn't like it when I have this in there. It crashes with the error "Index was outside the bounds of the array." and the line " stockArray(itemIndex)(1) = txt2.Text" highlighted in another sub.

    Now I know all of that could probably be done with LINQ or how you've been trying to explain things to me. But can we make it work this way just this once.

    I'll attach the current version of my program in a second.

    Edit: Here it is. I can't remember if I did or didn't 'comment' the last sub of code "txt1_TextChanged" but when this is active that is when problems start occurring.

    Cannot edit name of item.zip
    Last edited by Valhalla's Wrath; Aug 20th, 2012 at 09:22 AM.

  28. #68

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    I really am sorry for all of these consistent request for help I have been having. But getting this to work is something really important to me. In half an hour I need to go to sleep and if I can't work this one out I'll only have about 45 minutes to look at it again (6 hours from now) tomorrow before times up.

    If anyone could give me one last push and possibly not confuse me with to much new or altered code they would literally be a life saver. I hate to admit it but at the moment I am feeling kind of desperate and very anxious that I won't be able to resolve this.
    Last edited by Valhalla's Wrath; Aug 20th, 2012 at 08:49 AM.

  29. #69

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    Ok well I'm almost using changing my name values upsets this now.
    Code:
    Dim stock As String() = stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
    As it is getting highlighted in yellow and gives this error "Object variable or With block variable not set."

  30. #70
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

    "Index was outside the bounds of the array." and the line " stockArray(itemIndex)(1) = txt2.Text"
    I tried to explain this before...

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

    Code:
     "Object variable or With block variable not set."
    is probably due to a change elsewhere..
    Only time I could reproduce that was if the listbox was empty..

    the problem I'm sure is not in that line of code itself.. But rather it's hanging up on that line because its trying to do something, and one of the components it relies on is missing (like when i had no items in the list box)

    on a separate note.. I noticed the sub ListBox1_SelectedIndexChanged will throw an out of bounds exception if you click the listbox where there is no item. easy fix...

    first line of code could check for a non-item index. So:
    Code:
    If sender.SelectedIndex < 1 Then Exit Sub

  31. #71

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    48

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

    So editing the name field works fine for you with that?

    I'll give it a go. But I only get one last chance to make things work once I go to submit.

  32. #72
    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
    I have to say a big thank you to every ones help. You have both helped me so much elieICT and Ace that it is amazing. You have also been so patient with me for which I am very appreciative of. Also I have to say a thank you to everyone who has even been bothered to read everything I have to say even if they couldn't help.

    However I just have one very, very, very last problem. (I got a 1 day extension to fix up everything in this)

    I couldn't get that last bit of code you pasted working. I can't remember the exact errors now but I didn't really understand everything it was doing. However I managed to make my own sort of work around which will allow me to edit item details in the text boxes and keeps them changed after the item has been deselected but doesn't add the item to a text file until I click save. Which is what I wanted it to do. The only problem is, while this works for every other text box. It does not work for my text box holding item names.

    The code for which I am using is

    Code:
    Private Sub txt1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txt1.TextChanged
            Dim listBox1Item As String = ListBox1.SelectedItem
            Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
            stockArray(itemIndex)(0) = txt1.Text
            Search("")
    
        End Sub
    The code for all my other textchanged events on other text boxes is the same but for some reason or another it just doesn't like it when I have this in there. It crashes with the error "Index was outside the bounds of the array." and the line " stockArray(itemIndex)(1) = txt2.Text" highlighted in another sub.

    Now I know all of that could probably be done with LINQ or how you've been trying to explain things to me. But can we make it work this way just this once.

    I'll attach the current version of my program in a second.

    Edit: Here it is. I can't remember if I did or didn't 'comment' the last sub of code "txt1_TextChanged" but when this is active that is when problems start occurring.

    Cannot edit name of item.zip
    If you can't deal with the RemoveHandler and AddHandler or that is too much of a hassle, the EASIEST way to work around this would be to use a Button Click event to put the Selected ListBox Item values into the TextBoxes. That way, when you select a new item, the values don't change. It's based on something more significant and more purposely initiated when you use a button instead of the selectedindex changed event...

    Please, do not use the Text_Changed event for this though... I wouldn't recommend doing that. My reasoning for that is because for every text change in the textbox, that event fires, even if you don't mean to have it run through that Sub's code. If you are happy with it though (due to the limited time you have especially taken into consideration), then it'll do, but you need to first make sure that it's all built solid, because with an event like that being used to execute certain code, sometimes there's huge possibilities of bugs that can result; in the form of many Exceptions, and undefined behaviors.

    But as elielCT mentioned, if you don't have anything selected in the ListBox, the selectedindex will return -1. Although there is an error in what he's suggested to you (just to point this out):
    Code:
    If sender.SelectedIndex < 1 Then Exit Sub
    0 is less than one, BUT, is still a valid index.

    Use < 0 instead. Also what I see there is some implicit casting. sender is an Object, but in reality, it's an Object that represents something else usually.
    Last edited by AceInfinity; Aug 20th, 2012 at 08:19 PM.
    <<<------------
    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
    <<<------------

  33. #73
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

    LMGDAO!!!

    Nice Catch!!

    dont know how i missed that... smh

    >>>>

    I suspect the TextChanged event handler may very well be causing the other error..

    Hes pacified it, but its still being fired.. he can have a pseudo onTextChanged event fire with the LostFocus... Or with a button.. Which does seem to be the correct way of updating something like this.. I did mention all this before...

    ________________

    <offTopic>I've been reading into those lambda functions... Pretty interesting.. Already started implementing some on a project i've been working on... </offTopic>

  34. #74
    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 elielCT View Post
    LMGDAO!!!

    Nice Catch!!

    dont know how i missed that... smh

    >>>>

    I suspect the TextChanged event handler may very well be causing the other error..

    Hes pacified it, but its still being fired.. he can have a pseudo onTextChanged event fire with the LostFocus... Or with a button.. Which does seem to be the correct way of updating something like this.. I did mention all this before...

    ________________

    <offTopic>I've been reading into those lambda functions... Pretty interesting.. Already started implementing some on a project i've been working on... </offTopic>
    I would be in agreement with a button, but that's my opinion
    <<<------------
    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
    <<<------------

  35. #75
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

    i've never ran into an issue referencing sender or e... If there is something I should be aware of please share..

    Code:
    Private Sub CCCToolStipItems_Click(sender As System.Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
    The way it seems to me, the procedure is handling an event fired by an item being clicked in the toolstrip.. So sender is always going to be the object ToolStrip1.. The event is always going to be a toolstripitemevent argument belonging to toolstrip1 items...

    these items have properties, just as sender does.. we know who and what the sender is... i'd understand the error in using these parameters outside of a controlled event handler, where the typeof sender isn't guaranteed... in which case a good description of what objects are valid objects to be passed is in order...

    in this scenario this event has a property of .ClickedItem...

    What is your logic in being against this?

  36. #76
    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 elielCT View Post
    i've never ran into an issue referencing sender or e... If there is something I should be aware of please share..

    Code:
    Private Sub CCCToolStipItems_Click(sender As System.Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
    The way it seems to me, the procedure is handling an event fired by an item being clicked in the toolstrip.. So sender is always going to be the object ToolStrip1.. The event is always going to be a toolstripitemevent argument belonging to toolstrip1 items...

    these items have properties, just as sender does.. we know who and what the sender is... i'd understand the error in using these parameters outside of a controlled event handler, where the typeof sender isn't guaranteed... in which case a good description of what objects are valid objects to be passed is in order...

    in this scenario this event has a property of .ClickedItem...

    What is your logic in being against this?
    Code:
    sender As System.Object
    sender is a type of System.Object. It doesn't have a property of .ClickedItem because Object doesn't have those properties. Turn on Option Strict and see for yourself It IS an Object by your example that represents a ToolStrip, but that's not the current type at runtime. What you're doing is a phenomena called unboxing (implicitly).

    System.Object does not have those properties: http://msdn.microsoft.com/en-us/libr...em.object.aspx

    ToolStripItem does.

    Example for a Button_Click:
    vbnet Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     MessageBox.Show(sender.GetType().Name)
    3. End Sub

    It won't show any form of System.Windows.Forms.Control type. And you won't be able to reference the Button's Text property or anything, because System.Object doesn't have those properties.

    You have to cast it:
    vbnet Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     DirectCast(sender, Button).Text = "value"
    3. End Sub

    vbnet Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     MessageBox.Show(DirectCast(sender, Button).GetType().Name)
    3. End Sub

    ~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
    <<<------------

  37. #77
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

    or are you saying as good practice goes, sender should be directly cast into a type toolstrip before using his methods or properties?

    If --- that is the case, directly declaring toolstrip type in the parameter would seem to make more sense..

    ??

  38. #78
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

    oh ok... (just to correct u -> it was e that had those properties, but its because that wasnt declared as a generic type_).... I do catch ur drift, and I did notice sender doesn't have those available until runtime...

    I should've caught that bad habit.. It makes perfect sense...

  39. #79
    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 elielCT View Post
    or are you saying as good practice goes, sender should be directly cast into a type toolstrip before using his methods or properties?

    If --- that is the case, directly declaring toolstrip type in the parameter would seem to make more sense..

    ??
    No, not really because then it wouldn't have a signature that would properly match the delegate sub for an EventHandler...

    Quote Originally Posted by elielCT View Post
    oh ok... (just to correct u -> it was e that had those properties, but its because that wasnt declared as a generic type_).... I do catch ur drift, and I did notice sender doesn't have those available until runtime...

    I should've caught that bad habit.. It makes perfect sense...
    Option Strict On at the top of every project source code you write will straighten out all bad habits with improper or implicit type casting (hopefully).
    <<<------------
    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
    <<<------------

  40. #80
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

    Thanx.....

Page 2 of 3 FirstFirst 123 LastLast

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