-
Sep 27th, 2014, 10:06 PM
#1
Thread Starter
Junior Member
ListBox and If... Else Project
I got a program going but I want to make it better. We have to compare from Listbox1 to Listbox2 and if it meets specifications then it shows a messagebox correct (of course if it's wrong it shows a messagebox wrong). We learned about If...Else, For... Next, Do... While, and Do Until.
Option Strict On
Public Class Dogs
Private Sub Dogs(sender As Object, e As EventArgs) Handles MyBase.Load
DogCountryListBox.Items.Add("China")
DogCountryListBox.Items.Add("Canada")
DogCountryListBox.Items.Add("England")
DogCountryListBox.Items.Add("Germany")
DogBreedListBox.Items.Add("Labrador Retriever")
DogBreedListBox.Items.Add("Pug")
DogBreedListBox.Items.Add("Beagle")
DogBreedListBox.Items.Add("Doberman Pinscher")
End Sub
Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim InputString As String
If DogCountryListBox.SelectedIndex = -1 Then
MessageBox.Show("Please select a country.")
ElseIf DogBreedListBox.SelectedIndex = -1 Then
MessageBox.Show("Please select a breed.")
ElseIf DogCountryListBox.SelectedIndex <> DogBreedListBox.SelectedIndex Then
MessageBox.Show("Incorrect Country and breed. Please try again.")
Else
InputString = DogCountryListBox.SelectedItem.ToString() & " " & DogBreedListBox.SelectedItem.ToString()
MessageBox.Show(InputString & " is a correct selection")
End If
End Sub
Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
DogCountryListBox.SelectedIndex = -1
DogBreedListBox.SelectedIndex = -1
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
End Class
I want to be able to not just choose across from each other but to choose from China to Pug, Canada to Labrador Retriever and so on. I don't know how to go about doing that. My code just compares them across from each other and i'd like to make it more specific.
-
Sep 28th, 2014, 12:22 AM
#2
Re: ListBox and If... Else Project
I suppose a Dictionary should work,...
Code:
Public Class Form1
Private dogList As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' sort list boxes.
DogCountryListBox.Sorted = True
DogBreedListBox.Sorted = True
' add breed and country
dogList.Add("German Shepard", "Germany")
dogList.Add("Pug", "China")
dogList.Add("Golden Retriever", "Canada")
dogList.Add("Beagle", "England")
dogList.Add("Labrador", "Canada")
dogList.Add("Doberman", "Germany")
' add data to list boxes
For Each item In dogList
' add breed
DogBreedListBox.Items.Add(item.Key)
' only add country if not already in list!
If Not DogCountryListBox.Items.Contains(item.Value) Then
DogCountryListBox.Items.Add(item.Value)
End If
Next
End Sub
' check for match
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If DogCountryListBox.SelectedIndex = -1 Then
MessageBox.Show("Please select a country.")
ElseIf DogBreedListBox.SelectedIndex = -1 Then
MessageBox.Show("Please select a breed.")
Else
' get county
Dim country = dogList.Item(DogBreedListBox.SelectedItem.ToString)
' compare if county match.
If DogCountryListBox.SelectedItem.ToString = country Then
MessageBox.Show("MATCH!")
Else
MessageBox.Show("WRONG!")
End If
End If
End Sub
End Class
-
Sep 28th, 2014, 03:21 AM
#3
Thread Starter
Junior Member
Re: ListBox and If... Else Project
I ended up setting each one as a variable and assigning numbers to them, the lazy way. I was hoping I could figure out a loop or a if else combo. Thanks so much for helping me though. I need to check out that Contains(item.Value)
-
Sep 28th, 2014, 04:54 PM
#4
Lively Member
Re: ListBox and If... Else Project
It would be nice if you could use the [CODE] tags... It makes it easier to read your code.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|