|
-
Jan 23rd, 2008, 01:04 PM
#1
Thread Starter
New Member
[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!
-
Jan 23rd, 2008, 01:19 PM
#2
Re: [2005] Removing SelectedItem from ListBox
this will remove the selectitem in departListBox from arriveListBox
vb Code:
Private Sub departListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles departListBox.SelectedIndexChanged
For x As Integer = departListBox.Items.Count - 1 To 0 Step -1
If departListBox.GetSelected(x) = True Then
arriveListBox.Items.RemoveAt(x)
End If
Next
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 23rd, 2008, 01:38 PM
#3
Thread Starter
New Member
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)
-
Jan 23rd, 2008, 01:41 PM
#4
Re: [2005] Removing SelectedItem from ListBox
arriveListBox.Items.RemoveAt(departListBox.SelectedIndex)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 23rd, 2008, 01:43 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 23rd, 2008, 02:07 PM
#6
Thread Starter
New Member
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'.
-
Jan 23rd, 2008, 02:10 PM
#7
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.
-
Jan 23rd, 2008, 02:23 PM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|