Results 1 to 8 of 8

Thread: [2005] Removing SelectedItem from ListBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    8

    [2005] Removing SelectedItem from ListBox

    Well im working on this school project and im stumped on this Do loop im trying to code. The project is constructing an airline like booking form where you have departing, arriving, and airline listboxes. My problem is getting the selection from the departing box not to appear in the arriving box, here is my code so far.

    Imports System.IO


    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
    Dim locationStreamReader As New StreamReader("f:\VB\Locations.txt")
    Dim airlineStreamReader As New StreamReader("f:\VB\Airlines.txt")
    Dim locationString As String
    Dim airlineString As String

    Do Until locationStreamReader.Peek = -1
    locationString = locationStreamReader.ReadLine
    departListBox.Items.Add(locationString)
    arriveListBox.Items.Add(locationString)
    Loop
    locationStreamReader.Close()

    Do Until airlineStreamReader.Peek = -1
    airlineString = airlineStreamReader.ReadLine
    airlineListBox.Items.Add(airlineString)
    Loop
    airlineStreamReader.Close()
    Catch
    MessageBox.Show("File doesn't exist.")
    End Try
    End Sub
    Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click
    quoteLabel.Text = "You are departing from " & departListBox.SelectedItem & _
    " you will be arriving at " & arriveListBox.SelectedItem & " and you will be flying on " & airlineListBox.SelectedItem
    End Sub
    Private Sub departListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles departListBox.SelectedIndexChanged
    arriveListBox.Items.Clear()
    Do Until departListBox.Items.Count = 0
    If departListBox.SelectedItem = True Then
    arriveListBox.SelectedItem.Remove()
    End If
    Loop
    End Sub
    Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
    Close()
    End Sub
    End Class

    Any help or suggestions will be greatly appreciated!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] Removing SelectedItem from ListBox

    this will remove the selectitem in departListBox from arriveListBox

    vb Code:
    1. Private Sub departListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles departListBox.SelectedIndexChanged
    2.     For x As Integer = departListBox.Items.Count - 1 To 0 Step -1
    3.          If departListBox.GetSelected(x) = True Then
    4.             arriveListBox.Items.RemoveAt(x)
    5.          End If
    6.     Next
    7. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    8

    Re: [2005] Removing SelectedItem from ListBox

    Thanks man but this just looks like I would put in the index of the item I wanted to get/remove manually. I need the removal to happen automatically from the arrive box when the user selects a location out of the depart box.(trying to remove the same location from the arrive box that is selected in the depart box)

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

    Re: [2005] Removing SelectedItem from ListBox

    arriveListBox.Items.RemoveAt(departListBox.SelectedIndex)

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] Removing SelectedItem from ListBox

    you've got to remember that after you've removed an item from the list, the two lists won't be synchronized, so it'll be more difficult to remove a second item

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    8

    Re: [2005] Removing SelectedItem from ListBox

    Something still isnt right instead of only taking out the selectedIndex it takes out the whole arrive list instead of taking out that one selection and leaving the rest. If this helps any in my list im importing there are 9 entries and ive tried, For x As Integer = departListBox.Items.Count = 0 To 9 Step 1 but I get this back System.ArgumentOutOfRangeException was unhandled
    Message="InvalidArgument=Value of '2' is not valid for 'index'.

  7. #7
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2005] Removing SelectedItem from ListBox

    You need to move backwards through the loop as in .paul.'s code. Once you remove an item, the list count is less than the counter you're looking at, so you'll quickly be trying to read an object that isn't there. Use Step-1 instead.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    8

    Re: [2005] Removing SelectedItem from ListBox

    Yes I understand that and thats why im so stumped on why it still isnt working correctly when I use pauls code the entire arrive list is removed when you select an item from the depart list? Im not a very big fan on For loops either that could be the reason that I cant get it to work correctly I perfer Do loops which I have been working on and have ended up with this:

    Do Until departListBox.Items.Count = 9
    If departListBox.SelectedIndex = -1 Then
    arriveListBox.Items.RemoveAt(departListBox.SelectedIndex)
    End If
    Loop

    But again ive hit a snag here
    Last edited by jwiles6186; Jan 23rd, 2008 at 04:26 PM.

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