Results 1 to 24 of 24

Thread: VB Listbox not populating correctly

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    VB Listbox not populating correctly

    Can anyone help me? I fixed my code so it moves the items between files correctly but the listboxes populate with way more autos than there are. It should only be 5 in the in stock text box.
    Can someone point me in the right direction?

    Code:
    Public Class frmAssignment7
        'form load and create files 
        Private Sub Assignment_7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim swauto As IO.StreamWriter
            If Not (IO.File.Exists("AutosInStock.txt")) Then
                swauto = IO.File.CreateText("AutosInStock.txt")
    
                swauto.WriteLine("Ford Focus 2005")
                swauto.WriteLine("Honda Accord 1997")
                swauto.WriteLine("Mustang GT 2007")
                swauto.WriteLine("Mazda CX-5 2012")
                swauto.WriteLine("Mazda-3 2013")
    
                swauto.Close()
            End If
    
    
            If Not (IO.File.Exists("AutosSold.txt")) Then
                Dim swautoSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
                swautoSold.Close()
            End If
    
    
        End Sub
        
        'Move vehicle from in stock to sold
        Private Sub btnSell_Click(sender As Object, e As EventArgs) Handles btnSell.Click
    
            Dim autos As String = CStr(lstStock.SelectedItem)
            Dim AutoInStock As String = ""
            Dim item As String = ""
    
            Dim autoSold As IO.StreamWriter = IO.File.AppendText("AutosSold.txt")
            autoSold.WriteLine(autos)
            autoSold.Close()
    
            Dim sold() As String = IO.File.ReadAllLines("AutosSold.txt")
            lstSold.Items.Clear()
            lstSold.Items.AddRange(sold)
    
            Dim query = From auto In IO.File.ReadAllLines("AutosInStock.txt")
            Where auto > item
            Select auto
            IO.File.WriteAllLines("AutosInStock.txt", query.ToArray)
    
        End Sub
        'Moves vehicle from Sold back to In Stock
        Private Sub btnRepo_Click(sender As Object, e As EventArgs) Handles btnRepo.Click
            Dim repo As String = CStr(lstSold.SelectedItem)
            Dim item As String = ""
            Dim autoRepo As IO.StreamWriter = IO.File.AppendText("AutosInStock.txt")
            autoRepo.WriteLine(repo)
            autoRepo.Close()
            Dim repos() As String = IO.File.ReadAllLines("AutosInStock.txt")
            lstStock.Items.Clear()
            lstStock.Items.AddRange(repos)
    
            Dim query = From auto In IO.File.ReadAllLines("AutosSold.txt")
            Where auto < item
            Select auto
            IO.File.WriteAllLines("AutosSold.txt", query.ToArray)
    
            
        End Sub
       
    
        'Quits the program
        Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
            'Quit form
            Me.Close()
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB Listbox not populating correctly

    Presumably you're talking about this:
    Code:
    lstStock.Items.AddRange(repos)
    `repos` is an array loaded from a file so the ListBox will contain the same number of lines as are in the file. Have you bothered to test the Length of that array before adding its contents to the ListBox? If the array is longer than you expect then the ListBox is obviously completely irrelevant.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Re: VB Listbox not populating correctly

    Quote Originally Posted by jmcilhinney View Post
    Presumably you're talking about this:
    Code:
    lstStock.Items.AddRange(repos)
    `repos` is an array loaded from a file so the ListBox will contain the same number of lines as are in the file. Have you bothered to test the Length of that array before adding its contents to the ListBox? If the array is longer than you expect then the ListBox is obviously completely irrelevant.
    I'm new to VB and not sure how to do that. I know that there are only 5 items in the array because I am using swauto.WriteLine to add those specific vehicles.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    here's how I'd write it:

    Code:
    Public Class frmAssignment7
        'form load and create files 
        Private Sub Assignment_7_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            '--------------------------------------------------------
            'if stock file doesn't exist, create and populate file
            '--------------------------------------------------------
            Dim swauto As IO.StreamWriter
            If Not (IO.File.Exists("AutosInStock.txt")) Then
                swauto = IO.File.CreateText("AutosInStock.txt")
    
                swauto.WriteLine("Ford Focus 2005")
                swauto.WriteLine("Honda Accord 1997")
                swauto.WriteLine("Mustang GT 2007")
                swauto.WriteLine("Mazda CX-5 2012")
                swauto.WriteLine("Mazda-3 2013")
    
                swauto.Close()
            End If
            '--------------------------------------------------------
            'add stock cars to lstStock
            '--------------------------------------------------------
            lstStock.Items.AddRange(IO.File.ReadAllLines("AutosInStock.txt"))
            '--------------------------------------------------------
            'if sold file doesn't exist, create file
            '--------------------------------------------------------
            If Not (IO.File.Exists("AutosSold.txt")) Then
                Dim swautoSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
                swautoSold.Close()
            End If
            '--------------------------------------------------------
            'add sold cars to lstSold
            '--------------------------------------------------------
            lstSold.Items.AddRange(IO.File.ReadAllLines("AutosSold.txt"))
    
        End Sub
    
        'Move vehicle from in stock to sold
        Private Sub btnSell_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSell.Click
            '--------------------------------------------------------
            'get lstStock.SelectedItem
            '--------------------------------------------------------
            Dim auto As String = CStr(lstStock.SelectedItem)
            '--------------------------------------------------------
            'remove lstStock.SelectedItem
            '--------------------------------------------------------
            lstStock.Items.RemoveAt(lstStock.SelectedIndex)
            '--------------------------------------------------------
            'add auto to AutosSold.txt
            '--------------------------------------------------------
            Dim autoSold As IO.StreamWriter = IO.File.AppendText("AutosSold.txt")
            autoSold.WriteLine(auto)
            autoSold.Close()
            '--------------------------------------------------------
            'add auto to lstSold
            '--------------------------------------------------------
            lstSold.Items.Add(auto)
            '--------------------------------------------------------
            'update AutosInStock.txt
            '--------------------------------------------------------
            IO.File.WriteAllLines("AutosInStock.txt", lstStock.Items.Cast(Of String).ToArray)
    
        End Sub
        'Moves vehicle from Sold back to In Stock
        Private Sub btnRepo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRepo.Click
            '--------------------------------------------------------
            'get lstSold.SelectedItem
            '--------------------------------------------------------
            Dim repo As String = CStr(lstSold.SelectedItem)
            '--------------------------------------------------------
            'remove lstSold.SelectedItem
            '--------------------------------------------------------
            lstSold.Items.RemoveAt(lstSold.SelectedIndex)
            '--------------------------------------------------------
            'add auto to AutosInStock.txt
            '--------------------------------------------------------
            Dim autoRepo As IO.StreamWriter = IO.File.AppendText("AutosInStock.txt")
            autoRepo.WriteLine(repo)
            autoRepo.Close()
            '--------------------------------------------------------
            'add auto to lstStock
            '--------------------------------------------------------
            lstStock.Items.Add(repo)
            '--------------------------------------------------------
            'update AutosSold.txt
            '--------------------------------------------------------
            IO.File.WriteAllLines("AutosSold.txt", lstSold.Items.Cast(Of String).ToArray)
    
        End Sub
    
    
        'Quits the program
        Private Sub btnQuit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnQuit.Click
            'Quit form
            Me.Close()
        End Sub
    
    End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Re: VB Listbox not populating correctly

    Quote Originally Posted by .paul. View Post
    here's how I'd write it:

    Code:
    Public Class frmAssignment7
        'form load and create files 
        Private Sub Assignment_7_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            '--------------------------------------------------------
            'if stock file doesn't exist, create and populate file
            '--------------------------------------------------------
            Dim swauto As IO.StreamWriter
            If Not (IO.File.Exists("AutosInStock.txt")) Then
                swauto = IO.File.CreateText("AutosInStock.txt")
    
                swauto.WriteLine("Ford Focus 2005")
                swauto.WriteLine("Honda Accord 1997")
                swauto.WriteLine("Mustang GT 2007")
                swauto.WriteLine("Mazda CX-5 2012")
                swauto.WriteLine("Mazda-3 2013")
    
                swauto.Close()
            End If
            '--------------------------------------------------------
            'add stock cars to lstStock
            '--------------------------------------------------------
            lstStock.Items.AddRange(IO.File.ReadAllLines("AutosInStock.txt"))
            '--------------------------------------------------------
            'if sold file doesn't exist, create file
            '--------------------------------------------------------
            If Not (IO.File.Exists("AutosSold.txt")) Then
                Dim swautoSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
                swautoSold.Close()
            End If
            '--------------------------------------------------------
            'add sold cars to lstSold
            '--------------------------------------------------------
            lstSold.Items.AddRange(IO.File.ReadAllLines("AutosSold.txt"))
    
        End Sub
    
        'Move vehicle from in stock to sold
        Private Sub btnSell_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSell.Click
            '--------------------------------------------------------
            'get lstStock.SelectedItem
            '--------------------------------------------------------
            Dim auto As String = CStr(lstStock.SelectedItem)
            '--------------------------------------------------------
            'remove lstStock.SelectedItem
            '--------------------------------------------------------
            lstStock.Items.RemoveAt(lstStock.SelectedIndex)
            '--------------------------------------------------------
            'add auto to AutosSold.txt
            '--------------------------------------------------------
            Dim autoSold As IO.StreamWriter = IO.File.AppendText("AutosSold.txt")
            autoSold.WriteLine(auto)
            autoSold.Close()
            '--------------------------------------------------------
            'add auto to lstSold
            '--------------------------------------------------------
            lstSold.Items.Add(auto)
            '--------------------------------------------------------
            'update AutosInStock.txt
            '--------------------------------------------------------
            IO.File.WriteAllLines("AutosInStock.txt", lstStock.Items.Cast(Of String).ToArray)
    
        End Sub
        'Moves vehicle from Sold back to In Stock
        Private Sub btnRepo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRepo.Click
            '--------------------------------------------------------
            'get lstSold.SelectedItem
            '--------------------------------------------------------
            Dim repo As String = CStr(lstSold.SelectedItem)
            '--------------------------------------------------------
            'remove lstSold.SelectedItem
            '--------------------------------------------------------
            lstSold.Items.RemoveAt(lstSold.SelectedIndex)
            '--------------------------------------------------------
            'add auto to AutosInStock.txt
            '--------------------------------------------------------
            Dim autoRepo As IO.StreamWriter = IO.File.AppendText("AutosInStock.txt")
            autoRepo.WriteLine(repo)
            autoRepo.Close()
            '--------------------------------------------------------
            'add auto to lstStock
            '--------------------------------------------------------
            lstStock.Items.Add(repo)
            '--------------------------------------------------------
            'update AutosSold.txt
            '--------------------------------------------------------
            IO.File.WriteAllLines("AutosSold.txt", lstSold.Items.Cast(Of String).ToArray)
    
        End Sub
    
    
        'Quits the program
        Private Sub btnQuit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnQuit.Click
            'Quit form
            Me.Close()
        End Sub
    
    End Class
    You really understand your VB
    One question, is there a way to use LINQ to populate the listboxes for this program?

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: VB Listbox not populating correctly

    Why LINQ? LINQ is not magical. Where do you populate a listbox in your code? You are new to vb so should not be worrying about linq.... The LINQ you currently use is wrong as is.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    I have to agree there... Why LINQ? I've written the listbox populating in the most efficient way I know of. Changing it to use linq would be unnecessarily verbose...

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Re: VB Listbox not populating correctly

    I'm just trying to understand how LINQ works.
    If it's wrong, can you tell me how to make it right? I would just like to understand everything I can about VB as I go along.
    @Paul, I can see that your method is easiest and I appreciate.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    this line, using LINQ:

    Code:
    lstStock.Items.AddRange(IO.File.ReadAllLines("AutosInStock.txt"))
    could be:

    Code:
    lstStock.Items.AddRange((From line in IO.File.ReadAllLines("AutosInStock.txt") Select line).ToArray)
    but as you can hopefully see, it's not better to use LINQ for that purpose...

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: VB Listbox not populating correctly

    Quote Originally Posted by .paul. View Post
    this line, using LINQ:

    Code:
    lstStock.Items.AddRange(IO.File.ReadAllLines("AutosInStock.txt"))
    could be:

    Code:
    lstStock.Items.AddRange((From line in IO.File.ReadAllLines("AutosInStock.txt") Select line).ToArray)
    but as you can hopefully see, it's not better to use LINQ for that purpose...
    especially as it's doing the same under the hood.

  11. #11
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: VB Listbox not populating correctly

    Quote Originally Posted by mrsl80 View Post
    I'm just trying to understand how LINQ works.
    If it's wrong, can you tell me how to make it right? I would just like to understand everything I can about VB as I go along.
    @Paul, I can see that your method is easiest and I appreciate.
    Then learn how LINQ works. There are hundreds of tutorials. Us writing your code is not learning .

  12. #12
    New Member
    Join Date
    Feb 2014
    Location
    United States
    Posts
    15

    Re: VB Listbox not populating correctly

    Quote Originally Posted by ident View Post
    Us writing your code is not learning .
    No, it is not learning, but if we successfully annoy you to a certain point, you'll explain why certain ways are wrong, and that can help people learn. Possibly even quicker than tutorials if you can add some attitude into the explanation

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    Quote Originally Posted by Stressin' View Post
    No, it is not learning, but if we successfully annoy you to a certain point, you'll explain why certain ways are wrong, and that can help people learn. Possibly even quicker than tutorials if you can add some attitude into the explanation
    If you want attitude, ask ident. I prefer programming

  14. #14
    New Member
    Join Date
    Feb 2014
    Location
    United States
    Posts
    15

    Re: VB Listbox not populating correctly

    Quote Originally Posted by .paul. View Post
    If you want attitude, ask ident. I prefer programming
    Lol
    I imagined asking him, and then I'd never return to the scene. Kind of like a hit and run

    Personally mrsl80, I like using DataGridView for handling data, writing and reading. I feel that its a simple method, and clean looking. Now that I think of it, I could use a DataGridView with stream readers and writers for practically anything except for teleporting drinks from my fridge to my hand.

    With a DataGridView you could possibly even have extra columns if it seems convenient, like for manufacturer, model numbers. I'd stream write to a .txt, and use splits to populate each cell. Idk I guess what I'm basically saying is, you can never go wrong with a DataGridView.

    Edited
    When you say it adds more autos than you wanted, do you mean it is showing doubles of those 5? or is it just pulling other autos randomly out of a hat?

    Edited Again
    Oh, .paul. fixed you up. Well if you do not have anymore problems or questions please remember to mark thread as resolved.
    Last edited by Stressin'; Sep 18th, 2014 at 06:48 PM.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    Getting back on topic, LINQ can be used for many situations in programming. It's a very powerful and wide ranging part of the language.
    It's not something that can be taught in a few lines, but something you can learn and build upon with experience. I'm certainly not the best with LINQ in this forum. There's still much I could learn.

  16. #16
    New Member
    Join Date
    Feb 2014
    Location
    United States
    Posts
    15

    Re: VB Listbox not populating correctly

    Quote Originally Posted by .paul. View Post
    Getting back on topic, LINQ can be used for many situations in programming. It's a very powerful and wide ranging part of the language.
    It's not something that can be taught in a few lines, but something you can learn and build upon with experience. I'm certainly not the best with LINQ in this forum. There's still much I could learn.
    Reminds me of the question I had earlier. Which was.
    When is it appropriate to use LINQ?
    Edit..
    Never mind, I'd rather just look into it. Thanks anyways :P

  17. #17
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: VB Listbox not populating correctly

    <oldAndCrotchety>
    I don't like linq, now get off my lawn!
    </oldAndCrotchety>
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    Dim el as xelement =_
    <oldAndCrotchety>
    I don't like linq, now get off my lawn!
    </oldAndCrotchety>

    Msgbox(el...<oldAndCrotchety>.First.Value)

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB Listbox not populating correctly

    Quote Originally Posted by Stressin' View Post
    Reminds me of the question I had earlier. Which was.
    When is it appropriate to use LINQ?
    It's appropriate to use LINQ when doing so simplifies your code, making it shorter and/or more readable without reducing performance significantly. It's important to understand that LINQ will often actually make code run slower but often it will be by so little that it's insignificant, particularly when it makes code more maintainable.

    It's also important to understand that LINQ is an over-arching technology that has numerous "flavours", i.e. many different providers that each allow you to query a different type of data source using LINQ syntax.

    LINQ to Objects: allows you to query arrays and generic collections.
    LINQ to XML: allows you to query XML data.
    LINQ to SQL: allows you to query SQL Server data sources.
    LINQ to Entities: allows you to query Entity Framework data sources, which themselves may be connected to SQL Server or any other data source that supports EF.
    etc, etc.

    If you have created a LINQ to SQL or Entity Framework data source then you have no choice but to use LINQ for them. I've seen a number of people using L2S or EF and get confused as to when they are query their data source and when they are querying a simple collection and therefore using LINQ to Objects. The performance implications can be significant, e.g. I've seen queries that, instead of filtering at the database and retrieving only the required data, actually retrieve all the data from the database into a collection and then filter locally. The performance difference could be huge if you have a large database accessed over a web service.

  20. #20

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    11

    Re: VB Listbox not populating correctly

    Ok, my code is mainly correct now.
    I have one slight problem that I'm hoping someone can help me fix.
    The vehicles move just fine in the program and I have opened the text files to make sure they are moving correctly and they are.
    However, when I close the program and run it again, it does not keep the vehicles where they were.
    For instance, it starts with all 5 in the stock file and none in the sold file, even if I just moved some over the last time I ran it. How do I correct that?
    Code:
    Public Class frmAssignment7
        'form load and create files 
        Private Sub Assignment_7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
            Dim swauto As IO.StreamWriter
            If Not (IO.File.Exists("AutosInStock.txt")) Then
                swauto = IO.File.CreateText("AutosInStock.txt")
    
                'populate AutosInStock text file
                swauto.WriteLine("Ford Focus 2005")
                swauto.WriteLine("Honda Accord 1997")
                swauto.WriteLine("Mustang GT 2007")
                swauto.WriteLine("Mazda CX-5 2012")
                swauto.WriteLine("Mazda-3 2013")
    
                swauto.Close()
            End If
            lstStock.Items.AddRange(IO.File.ReadAllLines("AutosInStock.txt"))
    
            If Not (IO.File.Exists("AutosSold.txt")) Then
                Dim swautoSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
                swautoSold.Close()
            End If
            lstStock.Items.AddRange(IO.File.ReadAllLines("AutosSold.txt"))
    
    
        End Sub
    
        'Move vehicle from in stock to sold
        Private Sub btnSell_Click(sender As Object, e As EventArgs) Handles btnSell.Click
    
            If lstStock.SelectedIndex = -1 Then
                MessageBox.Show("Please select a vehicle")
            Else
    
    
              
    
                Dim item As String = CStr(lstStock.SelectedItem)
                Dim query = From auto In IO.File.ReadAllLines("AutosInStock.txt")
                        Where auto <> item
                        Select auto
                IO.File.WriteAllLines("AutosInStock.txt", query.ToArray)
    
    
    
                
                Dim selectedItems(lstStock.SelectedItems.Count - 1) As String
                lstStock.SelectedItems.CopyTo(selectedItems, 0)
                For Each selectedItem In selectedItems
                    lstSold.Items.Add(selectedItem)
                    lstStock.Items.Remove(selectedItem)
                Next
    
    
    
    
                Dim sw As IO.StreamWriter = IO.File.AppendText("AutosSold.txt")
                sw.WriteLine(item)
                sw.Close()
                
    
            End If
    
        End Sub
        'Moves vehicle from Sold back to In Stock
        Private Sub btnRepo_Click(sender As Object, e As EventArgs) Handles btnRepo.Click
            If lstSold.SelectedIndex = -1 Then
                MessageBox.Show("Please select a vehicle")
            Else
               
    
                Dim item As String = CStr(lstSold.SelectedItem)
    
                Dim query = From auto In IO.File.ReadAllLines("AutosSold.txt")
                Where auto <> item
                Select auto
                IO.File.WriteAllLines("AutosSold.txt", query.ToArray)
    
                Dim selectedItems(lstSold.SelectedItems.Count - 1) As String
                lstSold.SelectedItems.CopyTo(selectedItems, 0)
    
    
                For Each selectedItem In selectedItems
                    lstStock.Items.Add(selectedItem)
                    lstSold.Items.Remove(selectedItem)
                Next
    
    
                Dim sw As IO.StreamWriter = IO.File.AppendText("AutosInStock.txt")
                sw.WriteLine(item)
                sw.Close()
                
            End If
    
        End Sub
    
    
        'Quits the program
        Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
            'Quit form
            Me.Close()
        End Sub
        
    End Class

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    the code I gave you works exactly how you wanted it to...

  22. #22
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: VB Listbox not populating correctly

    Your code is mostly correct? Who told you this.

    Let's look at a few lines

    1) we add a file to a listbox. You dont bother to check it exists.
    2)You then check a file exists that wont do to create an empty file to add it to a listbox... Why?
    3) You then readalllines of tis empty textfile... err there are no lines.
    4) Now you use LINQ. Why you using SELECT with WHERE. Why?
    5) You have now read the same text file 4 times.

    vb Code:
    1. lstStock.Items.AddRange(IO.File.ReadAllLines("AutosInStock.txt"))
    2.  
    3.         If Not (IO.File.Exists("AutosSold.txt")) Then
    4.             Dim swautoSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
    5.             swautoSold.Close()
    6.         End If
    7.         lstStock.Items.AddRange(IO.File.ReadAllLines("AutosSold.txt"))

  23. #23
    New Member
    Join Date
    Feb 2014
    Location
    United States
    Posts
    15

    Re: VB Listbox not populating correctly

    What is this text file supposed to have in it?
    Code:
          If Not (IO.File.Exists("AutosSold.txt")) Then
                Dim swautoSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
                swautoSold.Close()
            End If
            lstStock.Items.AddRange(IO.File.ReadAllLines("AutosSold.txt"))

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB Listbox not populating correctly

    Quote Originally Posted by ident View Post
    Your code is mostly correct? Who told you this.

    Let's look at a few lines

    1) we add a file to a listbox. You dont bother to check it exists.
    2)You then check a file exists that wont do to create an empty file to add it to a listbox... Why?
    3) You then readalllines of tis empty textfile... err there are no lines.
    4) Now you use LINQ. Why you using SELECT with WHERE. Why?
    5) You have now read the same text file 4 times.

    vb Code:
    1. lstStock.Items.AddRange(IO.File.ReadAllLines("AutosInStock.txt"))
    2.  
    3.         If Not (IO.File.Exists("AutosSold.txt")) Then
    4.             Dim swautoSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
    5.             swautoSold.Close()
    6.         End If
    7.         lstStock.Items.AddRange(IO.File.ReadAllLines("AutosSold.txt"))
    1/ we know it exists, because the previous 11 lines ensure that it exists.
    2/ it could exist in my code
    3/ err... it might not be an empty file. if there is an empty file, it's a failsafe for the readalllines
    4/ good point
    5/ you might want to check your arithmatic there

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